blob: 57b5f9b668399c871cbdd33aaa3e4581f7b250db [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 Vasko8ee71392024-07-30 09:54:52 +0200599 int comment;
Michal Vasko69730152020-10-09 16:30:07 +0200600
Michal Vasko7fbc8162018-09-17 10:35:16 +0200601 /* word buffer - dynamically allocated */
602 *word_b = NULL;
603
604 /* word pointer - just a pointer to data */
605 *word_p = NULL;
606
607 *word_len = 0;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100608 while (!str_end) {
Radek Krejci33090f92020-12-17 20:12:46 +0100609 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200610 case '\'':
611 case '\"':
612 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200613 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100614 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200615 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200616 ret = LY_EVALID;
617 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200618 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200619 if (flags) {
Michal Vasko72c6d642024-02-27 14:59:01 +0100620 (*flags) |= (ctx->in->current[0] == '\'') ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200621 }
Michal Vasko1e4b1162024-01-19 09:16:49 +0100622
Michal Vaskob68ea142021-04-26 13:46:00 +0200623 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200624 if (!*word_p) {
625 /* do not return NULL word */
626 *word_p = "";
627 }
Michal Vasko1e4b1162024-01-19 09:16:49 +0100628 str_end = 1;
629 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200630 case '/':
Michal Vasko8ee71392024-07-30 09:54:52 +0200631 comment = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100632 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200633 /* one-line comment */
Michal Vasko8ee71392024-07-30 09:54:52 +0200634 comment = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100635 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200636 /* block comment */
Michal Vasko8ee71392024-07-30 09:54:52 +0200637 comment = 2;
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 Vasko8ee71392024-07-30 09:54:52 +0200642
643 if (comment) {
644 if (*word_len) {
645 /* invalid comment sequence (RFC 7950 sec. 6.1.3.) */
646 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Invalid comment sequence \"%.2s\" in an unquoted string.", ctx->in->current);
647 ret = LY_EVALID;
648 goto error;
649 }
650
651 /* skip the comment */
652 MOVE_INPUT(ctx, 2);
653 LY_CHECK_GOTO(ret = skip_comment(ctx, comment), error);
654 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655 break;
656 case ' ':
657 if (*word_len) {
658 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100659 str_end = 1;
660 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200661 }
Radek Krejci33090f92020-12-17 20:12:46 +0100662 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200663 break;
664 case '\t':
665 if (*word_len) {
666 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100667 str_end = 1;
668 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200669 }
670 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100671 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200672
Radek Krejci33090f92020-12-17 20:12:46 +0100673 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200674 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200675 case '\r':
676 if (ctx->in->current[1] != '\n') {
677 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200678 ret = LY_EVALID;
679 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200680 }
681 MOVE_INPUT(ctx, 1);
682 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200683 case '\n':
684 if (*word_len) {
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 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100689 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100690 MOVE_INPUT(ctx, 1);
691
Michal Vasko7fbc8162018-09-17 10:35:16 +0200692 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200693 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200694 break;
695 case ';':
696 case '{':
697 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
698 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100699 str_end = 1;
700 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200701 }
702
Radek Krejci33090f92020-12-17 20:12:46 +0100703 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200704 ret = LY_EVALID;
705 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200706 case '}':
707 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100708 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200709 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200710 ret = LY_EVALID;
711 goto error;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100712 case '\0':
713 /* unexpected EOF */
714 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
715 ret = LY_EVALID;
716 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200717 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200718 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 +0200719 break;
720 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 }
722
Radek Krejci44ceedc2018-10-02 15:54:31 +0200723 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200724 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200725 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200726 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200727 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200728 *word_p = *word_b;
729 }
730
731 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200732
733error:
734 free(*word_b);
735 *word_b = NULL;
736 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737}
738
Michal Vaskoea5abea2018-09-18 13:10:54 +0200739/**
740 * @brief Get another YANG keyword from the raw data.
741 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200742 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200743 * @param[out] kw YANG keyword read.
744 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
745 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200746 * @return LY_ERR values.
747 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200748LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200749get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200750{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200751 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200753 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200754
755 if (word_p) {
756 *word_p = NULL;
757 *word_len = 0;
758 }
759
760 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100761 while (ctx->in->current[0]) {
762 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200763 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100764 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200765 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100766 MOVE_INPUT(ctx, 2);
767 LY_CHECK_RET(skip_comment(ctx, 1));
768 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200769 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100770 MOVE_INPUT(ctx, 2);
771 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200772 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200773 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200774 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200775 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200776 }
Radek Krejci13028282019-06-11 14:56:48 +0200777 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200778 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200779 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100780 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200781 ctx->indent = 0;
782 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200783 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200784 /* skip whitespaces (optsep) */
785 ++ctx->indent;
786 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200787 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200788 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100789 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200790 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100791 case '\r':
792 /* possible CRLF endline */
793 if (ctx->in->current[1] == '\n') {
794 break;
795 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100796 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200797 default:
798 /* either a keyword start or an invalid character */
799 goto keyword_start;
800 }
801
Radek Krejci33090f92020-12-17 20:12:46 +0100802 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200803 }
804
805keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100806 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100807 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200808
aPiecek93582ed2021-05-25 14:49:06 +0200809 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
810 goto success;
811 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
812 ctx->depth++;
813 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100814 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200815 return LY_EINVAL;
816 }
817 goto success;
818 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
819 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200820 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821 }
822
Radek Krejcid6b76452019-09-03 17:03:03 +0200823 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200824 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100825 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200826 case '\r':
827 if (ctx->in->current[1] != '\n') {
828 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
829 return LY_EVALID;
830 }
831 MOVE_INPUT(ctx, 1);
832 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200833 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200834 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200835 case ' ':
836 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200838 case ':':
839 /* keyword is not actually a keyword, but prefix of an extension.
840 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
841 * and we will be checking the keyword (extension instance) itself */
842 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100843 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200844 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200845 case '{':
Michal Vasko946b70c2023-07-19 08:57:31 +0200846 case ';':
847 /* allowed only for input and output statements which are without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200848 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200849 break;
850 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100851 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200852 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100853 MOVE_INPUT(ctx, 1);
Michal Vaskof1918e12024-07-15 10:14:17 +0200854 /* fall through */
855 case '\0':
Radek Krejci33090f92020-12-17 20:12:46 +0100856 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200857 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200858 return LY_EVALID;
859 }
860 } else {
861 /* still can be an extension */
862 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200863
Radek Krejci156ccaf2018-10-15 15:49:17 +0200864extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100865 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200866 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
867 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200868 uint32_t c = 0;
869
Radek Krejci33090f92020-12-17 20:12:46 +0100870 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
871 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200872 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200873 /* check character validity */
Michal Vaskod0625d72022-10-06 15:02:50 +0200874 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100875 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876 }
Radek Krejci33090f92020-12-17 20:12:46 +0100877 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200878 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200879 return LY_EVALID;
880 }
881
882 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200883 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100884 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200885 return LY_EVALID;
886 }
887
Radek Krejcid6b76452019-09-03 17:03:03 +0200888 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200889 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200890
Radek Krejci626df482018-10-11 15:06:31 +0200891success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892 if (word_p) {
893 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100894 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895 }
896
897 return LY_SUCCESS;
898}
899
Michal Vaskoea5abea2018-09-18 13:10:54 +0200900/**
901 * @brief Parse extension instance substatements.
902 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200903 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200904 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200905 * @param[in] word Extension instance substatement name (keyword).
906 * @param[in] word_len Extension instance substatement name length.
907 * @param[in,out] child Children of this extension instance to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200908 * @return LY_ERR values.
909 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200911parse_ext_substmt(struct lysp_yang_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200912 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200913{
914 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100915 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200916 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200917 struct lysp_stmt *stmt, *par_child;
918
919 stmt = calloc(1, sizeof *stmt);
920 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
921
Radek Krejcibb9b1982019-04-08 14:24:59 +0200922 /* insert into parent statements */
923 if (!*child) {
924 *child = stmt;
925 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200926 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200927 par_child->next = stmt;
928 }
929
Michal Vaskofc2cd072021-02-24 13:17:17 +0100930 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200931 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932
933 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100934 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200935 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200936 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200937 }
938
Radek Krejci8df109d2021-04-23 12:19:08 +0200939 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100940 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100941 stmt->kw = kw;
942
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200943 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
944 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
945 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200946 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200947
948cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200949 return ret;
950}
951
Michal Vaskoea5abea2018-09-18 13:10:54 +0200952/**
953 * @brief Parse extension instance.
954 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200955 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200956 * @param[in] ext_name Extension instance substatement name (keyword).
957 * @param[in] ext_name_len Extension instance substatement name length.
Michal Vasko193dacd2022-10-13 08:43:05 +0200958 * @param[in] parent Current statement parent.
959 * @param[in] parent_stmt Type of @p parent statement.
960 * @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 +0200961 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200962 * @return LY_ERR values.
963 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200965parse_ext(struct lysp_yang_ctx *ctx, const char *ext_name, size_t ext_name_len, const void *parent,
966 enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200967{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100968 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200969 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200970 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200971 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200972 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200974 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200975
Michal Vaskofc2cd072021-02-24 13:17:17 +0100976 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200977 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%.*s\" without the mandatory prefix.",
978 (int)ext_name_len, ext_name);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100979 return LY_EVALID;
980 }
981
982 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200983 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984
985 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100986 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200987 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200988 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989 }
990
Michal Vaskofc2cd072021-02-24 13:17:17 +0100991 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200992 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200993 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100994 e->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200995 e->parent = (void *)parent;
996 e->parent_stmt = parent_stmt;
997 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100998
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200999 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko35274cf2024-07-04 12:01:00 +02001000 switch (kw) {
1001 case LY_STMT_EXTENSION_INSTANCE:
1002 LY_CHECK_GOTO(parse_ext(ctx, word, word_len, e, LY_STMT_EXTENSION_INSTANCE, 0, &e->exts), cleanup);
1003 break;
1004 default:
1005 /* just store all the statements */
1006 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
1007 break;
1008 }
1009 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, e->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001010 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001011
1012cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001013 return ret;
1014}
1015
Michal Vaskoea5abea2018-09-18 13:10:54 +02001016/**
1017 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
1018 * description, etc...
1019 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001020 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001021 * @param[in] parent Current statement parent.
1022 * @param[in] parent_stmt Type of statement in @p value.
1023 * @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 +02001024 * @param[in,out] value Place to store the parsed value.
1025 * @param[in] arg Type of the YANG keyword argument (of the value).
Michal Vasko72c6d642024-02-27 14:59:01 +01001026 * @param[out] flags Optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
1027 * [schema node flags](@ref snodeflags))
Michal Vaskoea5abea2018-09-18 13:10:54 +02001028 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001029 * @return LY_ERR values.
1030 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001032parse_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 +01001033 const char **value, enum yang_arg arg, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001034{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001035 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001037 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001038 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039
1040 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001041 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001042 return LY_EVALID;
1043 }
1044
1045 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001046 LY_CHECK_RET(get_argument(ctx, arg, flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001047
1048 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +02001049 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001050
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001051 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001052 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001053 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001054 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent, parent_stmt, parent_stmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055 break;
1056 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001057 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 return LY_EVALID;
1059 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001060 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001062
1063cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064 return ret;
1065}
1066
Michal Vaskoea5abea2018-09-18 13:10:54 +02001067/**
1068 * @brief Parse the yang-version statement.
1069 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001070 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001071 * @param[in,out] mod Module to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001072 * @return LY_ERR values.
1073 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001074static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001075parse_yangversion(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001076{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001077 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001079 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001080 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081
Michal Vasko193dacd2022-10-13 08:43:05 +02001082 if (mod->version) {
David Sedlákb3077192019-06-19 10:55:37 +02001083 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001084 return LY_EVALID;
1085 }
1086
1087 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001088 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089
Radek Krejci96e48da2020-09-04 13:18:06 +02001090 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001091 mod->version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001092 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001093 mod->version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001095 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001096 free(buf);
1097 return LY_EVALID;
1098 }
1099 free(buf);
1100
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001101 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001103 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001104 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_YANG_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001105 break;
1106 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001107 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001108 return LY_EVALID;
1109 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001110 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001111 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001112
1113cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001114 return ret;
1115}
1116
Michal Vaskoea5abea2018-09-18 13:10:54 +02001117/**
1118 * @brief Parse the belongs-to statement.
1119 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001120 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001121 * @param[in,out] submod Submodule to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001122 * @return LY_ERR values.
1123 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001125parse_belongsto(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001127 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001128 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001129 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001130 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001131
Michal Vasko193dacd2022-10-13 08:43:05 +02001132 if (submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001133 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001134 return LY_EVALID;
1135 }
1136
Michal Vaskoc3781c32020-10-06 14:04:08 +02001137 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001138 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001139 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001140 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 +01001141 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001142 free(buf);
1143 return LY_EVALID;
1144 }
1145 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001146
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001147 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001148 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001149 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01001150 LY_CHECK_RET(parse_text_field(ctx, submod->prefix, LY_STMT_PREFIX, 0, &submod->prefix, Y_IDENTIF_ARG, NULL,
1151 &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001153 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001154 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_BELONGS_TO, 0, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155 break;
1156 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001157 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158 return LY_EVALID;
1159 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001160 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001162
Michal Vasko7fbc8162018-09-17 10:35:16 +02001163 /* mandatory substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +02001164 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001165 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001166 return LY_EVALID;
1167 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001168
1169cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001170 return ret;
1171}
1172
Michal Vaskoea5abea2018-09-18 13:10:54 +02001173/**
1174 * @brief Parse the revision-date statement.
1175 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001176 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001177 * @param[in,out] rev Buffer to store the parsed value in.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001178 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001179 * @return LY_ERR values.
1180 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001182parse_revisiondate(struct lysp_yang_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001184 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001186 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001187 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001188
1189 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001190 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191 return LY_EVALID;
1192 }
1193
1194 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001195 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196
1197 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001198 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001199 free(buf);
1200 return LY_EVALID;
1201 }
1202
1203 /* store value and spend buf if allocated */
1204 strncpy(rev, word, word_len);
1205 free(buf);
1206
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001207 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001209 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001210 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 break;
1212 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001213 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 return LY_EVALID;
1215 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001216 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001218
1219cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220 return ret;
1221}
1222
Michal Vaskoea5abea2018-09-18 13:10:54 +02001223/**
1224 * @brief Parse the include statement.
1225 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001226 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001227 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001228 * @param[in,out] includes Parsed includes to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001229 * @return LY_ERR values.
1230 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001232parse_include(struct lysp_yang_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233{
Radek Krejcid33273d2018-10-25 14:55:52 +02001234 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001235 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001236 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001237 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001238 struct lysp_include *inc;
1239
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001240 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241
1242 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001243 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001244
Michal Vasko12ef5362022-09-16 15:13:58 +02001245 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001246
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001247 /* submodules share the namespace with the module names, so there must not be
1248 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001249 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001250 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001251 return LY_EVALID;
1252 }
1253
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001254 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001256 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001257 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko72c6d642024-02-27 14:59:01 +01001258 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 +02001259 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001260 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001261 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko72c6d642024-02-27 14:59:01 +01001262 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 +02001263 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001264 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001265 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001267 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001268 LY_CHECK_RET(parse_ext(ctx, word, word_len, inc, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001269 break;
1270 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001271 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001272 return LY_EVALID;
1273 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001274 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001275 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001276
1277cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001278 return ret;
1279}
1280
Michal Vaskoea5abea2018-09-18 13:10:54 +02001281/**
1282 * @brief Parse the import statement.
1283 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001284 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001285 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001286 * @param[in,out] imports Parsed imports to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001287 * @return LY_ERR values.
1288 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001289static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001290parse_import(struct lysp_yang_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001291{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001292 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001293 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001294 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001295 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296 struct lysp_import *imp;
1297
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001298 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001299
1300 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001301 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001302 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001304 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001305 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001306 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01001307 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 +02001308 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001309 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001310 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001311 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko72c6d642024-02-27 14:59:01 +01001312 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 +02001313 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001314 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001315 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko72c6d642024-02-27 14:59:01 +01001316 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 +02001317 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001318 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001319 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001321 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001322 LY_CHECK_RET(parse_ext(ctx, word, word_len, imp, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001323 break;
1324 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001325 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001326 return LY_EVALID;
1327 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001328 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001330
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001332 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001333
Michal Vasko12ef5362022-09-16 15:13:58 +02001334cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001335 return ret;
1336}
1337
Michal Vaskoea5abea2018-09-18 13:10:54 +02001338/**
1339 * @brief Parse the revision statement.
1340 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001341 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001342 * @param[in,out] revs Parsed revisions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001343 * @return LY_ERR values.
1344 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001346parse_revision(struct lysp_yang_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001347{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001348 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001349 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001350 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001351 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001352 struct lysp_revision *rev;
1353
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001354 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001355
1356 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001357 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001358
1359 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001360 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001361 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362 return LY_EVALID;
1363 }
1364
Radek Krejcib7db73a2018-10-24 14:18:40 +02001365 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366 free(buf);
1367
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001368 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001369 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001370 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001371 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 +02001372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001373 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001374 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 +02001375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001376 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001377 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001378 break;
1379 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001380 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 return LY_EVALID;
1382 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001383 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001384 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001385
1386cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001387 return ret;
1388}
1389
Michal Vaskoea5abea2018-09-18 13:10:54 +02001390/**
1391 * @brief Parse a generic text field that can have more instances such as base.
1392 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001393 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001394 * @param[in] parent Current statement parent.
1395 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001396 * @param[in,out] texts Parsed values to add to.
1397 * @param[in] arg Type of the expected argument.
1398 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001399 * @return LY_ERR values.
1400 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001402parse_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 +02001403 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001404{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001405 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001406 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001407 const char **item;
1408 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001409 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001410
1411 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001412 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001413
1414 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001415 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001416
Michal Vasko12ef5362022-09-16 15:13:58 +02001417 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001418 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001419 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001420 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001421 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 +02001422 break;
1423 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001424 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001425 return LY_EVALID;
1426 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001427 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001428 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001429
1430cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431 return ret;
1432}
1433
Michal Vaskoea5abea2018-09-18 13:10:54 +02001434/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001435 * @brief Parse a generic text field that can have more instances such as base.
1436 *
1437 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001438 * @param[in] parent_stmt Type of statement stored in @p qnames.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001439 * @param[in,out] qnames Parsed qnames to add to.
1440 * @param[in] arg Type of the expected argument.
1441 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001442 * @return LY_ERR values.
1443 */
1444static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001445parse_qnames(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_qname **qnames, enum yang_arg arg,
1446 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +02001447{
1448 LY_ERR ret = LY_SUCCESS;
1449 char *buf, *word;
1450 struct lysp_qname *item;
1451 size_t word_len;
1452 enum ly_stmt kw;
1453
1454 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001455 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001456
1457 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001458 LY_CHECK_RET(get_argument(ctx, arg, &item->flags, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001459
Michal Vasko12ef5362022-09-16 15:13:58 +02001460 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001461 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001462 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001463 switch (kw) {
1464 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001465 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 +02001466 break;
1467 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001468 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001469 return LY_EVALID;
1470 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001471 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001472 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001473
1474cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001475 return ret;
1476}
1477
1478/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001479 * @brief Parse the config statement.
1480 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001481 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001482 * @param[in,out] flags Flags to add to.
1483 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001484 * @return LY_ERR values.
1485 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001487parse_config(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001489 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001490 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001491 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001492 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493
1494 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001495 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 return LY_EVALID;
1497 }
1498
1499 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001500 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501
Radek Krejcif13b87b2020-12-01 22:02:17 +01001502 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001504 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505 *flags |= LYS_CONFIG_R;
1506 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001507 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 free(buf);
1509 return LY_EVALID;
1510 }
1511 free(buf);
1512
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001513 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001515 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001516 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 break;
1518 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001519 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 return LY_EVALID;
1521 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001522 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001523 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001524
1525cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526 return ret;
1527}
1528
Michal Vaskoea5abea2018-09-18 13:10:54 +02001529/**
1530 * @brief Parse the mandatory statement.
1531 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001532 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001533 * @param[in,out] flags Flags to add to.
1534 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001535 * @return LY_ERR values.
1536 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001538parse_mandatory(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001539{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001540 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001541 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001542 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001543 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544
1545 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001546 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547 return LY_EVALID;
1548 }
1549
1550 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001551 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552
Radek Krejcif13b87b2020-12-01 22:02:17 +01001553 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001555 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556 *flags |= LYS_MAND_FALSE;
1557 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001558 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559 free(buf);
1560 return LY_EVALID;
1561 }
1562 free(buf);
1563
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001564 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001566 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001567 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 break;
1569 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001570 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 return LY_EVALID;
1572 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001573 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001575
1576cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 return ret;
1578}
1579
Michal Vaskoea5abea2018-09-18 13:10:54 +02001580/**
1581 * @brief Parse a restriction such as range or length.
1582 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001583 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001584 * @param[in] restr_kw Type of this particular restriction.
1585 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001586 * @return LY_ERR values.
1587 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001589parse_restr(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001590{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001591 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001592 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001593 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001594 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001595
1596 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001597 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, &restr->arg.flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001598
Michal Vasko193dacd2022-10-13 08:43:05 +02001599 CHECK_NONEMPTY(ctx, word_len, lyplg_ext_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001600 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001601 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001602 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001604 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001605 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 +02001606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001607 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001608 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 +02001609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001610 case LY_STMT_ERROR_APP_TAG:
Michal Vasko72c6d642024-02-27 14:59:01 +01001611 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 +02001612 &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001614 case LY_STMT_ERROR_MESSAGE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001615 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 +02001616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001617 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001618 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001619 break;
1620 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001621 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622 return LY_EVALID;
1623 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001624 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001626
1627cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628 return ret;
1629}
1630
Michal Vaskoea5abea2018-09-18 13:10:54 +02001631/**
1632 * @brief Parse a restriction that can have more instances such as must.
1633 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001634 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001635 * @param[in] restr_kw Type of this particular restriction.
1636 * @param[in,out] restrs Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001637 * @return LY_ERR values.
1638 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001640parse_restrs(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641{
1642 struct lysp_restr *restr;
1643
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001644 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001645 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001646}
1647
Michal Vaskoea5abea2018-09-18 13:10:54 +02001648/**
1649 * @brief Parse the status statement.
1650 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001651 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001652 * @param[in,out] flags Flags to add to.
1653 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001654 * @return LY_ERR values.
1655 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001656static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001657parse_status(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001659 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001660 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001661 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001662 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663
1664 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001665 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666 return LY_EVALID;
1667 }
1668
1669 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001670 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671
Radek Krejcif13b87b2020-12-01 22:02:17 +01001672 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001674 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001675 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001676 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001677 *flags |= LYS_STATUS_OBSLT;
1678 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001679 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001680 free(buf);
1681 return LY_EVALID;
1682 }
1683 free(buf);
1684
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001685 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001687 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001688 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001689 break;
1690 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001691 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001692 return LY_EVALID;
1693 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001694 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001695 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001696
1697cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001698 return ret;
1699}
1700
Michal Vaskoea5abea2018-09-18 13:10:54 +02001701/**
1702 * @brief Parse the when statement.
1703 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001704 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001705 * @param[in,out] when_p When pointer to parse to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001706 * @return LY_ERR values.
1707 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001708LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001709parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001711 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001712 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001713 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001714 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001716 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001717
1718 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001719 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001720 return LY_EVALID;
1721 }
1722
1723 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001724 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001725
1726 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001727 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001728 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001729 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001730
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001731 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001732 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001733 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001734 ret = parse_text_field(ctx, when->dsc, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, NULL, &when->exts);
1735 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001737 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001738 ret = parse_text_field(ctx, when->ref, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, NULL, &when->exts);
1739 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001741 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001742 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 +02001743 break;
1744 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001745 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001746 ret = LY_EVALID;
1747 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001748 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001749 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001750 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001751
1752cleanup:
1753 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001754 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001755 free(when);
1756 } else {
1757 *when_p = when;
1758 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759 return ret;
1760}
1761
Michal Vaskoea5abea2018-09-18 13:10:54 +02001762/**
1763 * @brief Parse the anydata or anyxml statement.
1764 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001765 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001766 * @param[in] any_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001767 * @param[in] parent Node parent.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001768 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001769 * @return LY_ERR values.
1770 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001771LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001772parse_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 +02001773{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001774 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001775 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001776 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001778 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779
David Sedlák60adc092019-08-06 15:57:02 +02001780 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001781 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001782
Radek Krejci39b7fc22021-02-26 23:29:18 +01001783 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001784 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001785
Michal Vasko7fbc8162018-09-17 10:35:16 +02001786 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001787 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001788 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001789
1790 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001791 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001792 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001793 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001794 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001795 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001796 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001797 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 +02001798 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001799 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001800 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001801 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001802 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001803 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001804 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001805 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001806 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001807 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001808 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001809 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 +02001810 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001811 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001812 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001813 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001814 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001815 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001817 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001818 LY_CHECK_RET(parse_ext(ctx, word, word_len, any, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 break;
1820 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001821 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001822 return LY_EVALID;
1823 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001824 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001826
1827cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 return ret;
1829}
1830
Michal Vaskoea5abea2018-09-18 13:10:54 +02001831/**
1832 * @brief Parse the value or position statement. Substatement of type enum statement.
1833 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001834 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001835 * @param[in] val_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001836 * @param[in,out] enm Structure to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001837 * @return LY_ERR values.
1838 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001839LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001840parse_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 +02001841{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001842 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001843 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001844 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001845 long long num = 0;
1846 unsigned long long unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001847 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848
Michal Vasko193dacd2022-10-13 08:43:05 +02001849 if (enm->flags & LYS_SET_VALUE) {
1850 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001851 ret = LY_EVALID;
1852 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001853 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001854 enm->flags |= LYS_SET_VALUE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001855
1856 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001857 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001858
Radek Krejcid6b76452019-09-03 17:03:03 +02001859 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 +02001860 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001861 ret = LY_EVALID;
1862 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001863 }
1864
1865 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001866 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001867 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001868 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001869 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001870 ret = LY_EVALID;
1871 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001872 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001873 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001874 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001875 if (unum > UINT64_C(4294967295)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001876 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001877 ret = LY_EVALID;
1878 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001879 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001880 }
1881 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001882 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001883 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001884 ret = LY_EVALID;
1885 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001886 }
1887 if (errno == ERANGE) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001888 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001889 ret = LY_EVALID;
1890 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001892 if (val_kw == LY_STMT_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001893 enm->value = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001894 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02001895 enm->value = unum;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001898 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001900 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001901 ret = parse_ext(ctx, word, word_len, enm, val_kw, 0, &enm->exts);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001902 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001903 break;
1904 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001905 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001906 ret = LY_EVALID;
1907 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001909 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910 }
Radek Krejci8b764662018-11-14 14:15:13 +01001911
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001912cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001913 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001914 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001915}
1916
Michal Vaskoea5abea2018-09-18 13:10:54 +02001917/**
1918 * @brief Parse the enum or bit statement. Substatement of type statement.
1919 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001920 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001921 * @param[in] enum_kw Type of this particular keyword.
1922 * @param[in,out] enums Enums or bits to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001923 * @return LY_ERR values.
1924 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001926parse_type_enum(struct lysp_yang_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001928 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001930 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001931 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932 struct lysp_type_enum *enm;
1933
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001934 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001935
1936 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001937 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 +02001938 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001939 ret = lysp_check_enum_name((struct lysp_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001940 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001941 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001942
Michal Vasko12ef5362022-09-16 15:13:58 +02001943 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001944 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(enum_kw), enm->name);
Radek Krejci8b764662018-11-14 14:15:13 +01001945
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001946 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001947 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001948 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001949 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 +02001950 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001951 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001952 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001953 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001954 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001955 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001956 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 +02001957 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001958 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001959 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001961 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001962 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1963 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1964 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
David Sedlák9fb515f2019-07-11 10:33:58 +02001965 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001966 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001967 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1968 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1969 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001970 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001971 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001972 LY_CHECK_RET(parse_ext(ctx, word, word_len, enm, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973 break;
1974 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001975 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976 return LY_EVALID;
1977 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001978 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001980
1981cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001982 return ret;
1983}
1984
Michal Vaskoea5abea2018-09-18 13:10:54 +02001985/**
1986 * @brief Parse the fraction-digits statement. Substatement of type statement.
1987 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001988 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001989 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001990 * @return LY_ERR values.
1991 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001993parse_type_fracdigits(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001994{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001995 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001996 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001997 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001998 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001999 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002000
Michal Vasko193dacd2022-10-13 08:43:05 +02002001 if (type->fraction_digits) {
David Sedlákb3077192019-06-19 10:55:37 +02002002 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 return LY_EVALID;
2004 }
2005
2006 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002007 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008
2009 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002010 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002011 ret = LY_EVALID;
2012 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002013 }
2014
2015 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002016 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002018 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002019 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002020 ret = LY_EVALID;
2021 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002023 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002024 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002025 ret = LY_EVALID;
2026 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002028 type->fraction_digits = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002030 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002031 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002032 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002033 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 +02002034 break;
2035 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002036 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002037 ret = LY_EVALID;
2038 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002040 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002042
2043cleanup:
2044 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045 return ret;
2046}
2047
Michal Vaskoea5abea2018-09-18 13:10:54 +02002048/**
2049 * @brief Parse the require-instance statement. Substatement of type statement.
2050 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002051 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002052 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002053 * @return LY_ERR values.
2054 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002056parse_type_reqinstance(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002057{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002058 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002059 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002060 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002061 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062
Michal Vasko193dacd2022-10-13 08:43:05 +02002063 if (type->flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002064 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002065 return LY_EVALID;
2066 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002067 type->flags |= LYS_SET_REQINST;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068
2069 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002070 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071
Radek Krejcif13b87b2020-12-01 22:02:17 +01002072 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002073 type->require_instance = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002074 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002075 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002076 ret = LY_EVALID;
2077 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002079
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002080 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002082 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002083 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 +02002084 break;
2085 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002086 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002087 ret = LY_EVALID;
2088 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002089 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002090 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002091 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002092
2093cleanup:
2094 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095 return ret;
2096}
2097
Michal Vaskoea5abea2018-09-18 13:10:54 +02002098/**
2099 * @brief Parse the modifier statement. Substatement of type pattern statement.
2100 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002101 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002102 * @param[in,out] restr Restriction to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002103 * @return LY_ERR values.
2104 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002106parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002108 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002109 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002110 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002111 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112
Michal Vasko193dacd2022-10-13 08:43:05 +02002113 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002114 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 return LY_EVALID;
2116 }
2117
2118 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002119 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120
Radek Krejcif13b87b2020-12-01 22:02:17 +01002121 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002122 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002123 ret = LY_EVALID;
2124 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126
2127 /* replace the value in the dictionary */
Michal Vasko193dacd2022-10-13 08:43:05 +02002128 buf = malloc(strlen(restr->arg.str) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002129 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02002130 strcpy(buf, restr->arg.str);
2131 lydict_remove(PARSER_CTX(ctx), restr->arg.str);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132
Radek Krejcif13b87b2020-12-01 22:02:17 +01002133 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2134 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko183ea332022-11-08 10:50:03 +01002135 ret = lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002136 buf = NULL;
Michal Vasko183ea332022-11-08 10:50:03 +01002137 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002138
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002139 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002140 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002141 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002142 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 +02002143 break;
2144 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002145 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002146 ret = LY_EVALID;
2147 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002149 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002150 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002151
2152cleanup:
2153 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154 return ret;
2155}
2156
Michal Vaskoea5abea2018-09-18 13:10:54 +02002157/**
2158 * @brief Parse the pattern statement. Substatement of type statement.
2159 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002160 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002161 * @param[in,out] patterns Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002162 * @return LY_ERR values.
2163 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002164static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002165parse_type_pattern(struct lysp_yang_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002167 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002169 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002170 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 struct lysp_restr *restr;
2172
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002173 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002174
2175 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01002176 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, &restr->arg.flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177
2178 /* add special meaning first byte */
2179 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002180 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181 word = buf;
2182 } else {
2183 buf = malloc(word_len + 2);
2184 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002185 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002186 if (word_len) {
2187 memmove(buf + 1, word, word_len);
2188 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002189 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002190 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002191 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002192 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002193
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002194 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002195 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002196 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002197 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 +02002198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002199 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002200 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 +02002201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002202 case LY_STMT_ERROR_APP_TAG:
Michal Vasko72c6d642024-02-27 14:59:01 +01002203 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 +02002204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002205 case LY_STMT_ERROR_MESSAGE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002206 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 +02002207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002208 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002209 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko193dacd2022-10-13 08:43:05 +02002210 LY_CHECK_RET(parse_type_pattern_modifier(ctx, restr));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002212 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002213 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 break;
2215 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002216 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002217 return LY_EVALID;
2218 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002219 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002221
2222cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002223 return ret;
2224}
2225
Michal Vaskoea5abea2018-09-18 13:10:54 +02002226/**
2227 * @brief Parse the type statement.
2228 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002229 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002230 * @param[in,out] type Type to wrote to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002231 * @return LY_ERR values.
2232 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002234parse_type(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002236 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002238 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002239 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002240 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241 struct lysp_type *nest_type;
2242
2243 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002244 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002245 return LY_EVALID;
2246 }
2247
2248 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002249 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002250 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002252 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002253 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002254
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002255 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002257 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002258 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 +01002259 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002261 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002262 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002263 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002265 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002266 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002267 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002269 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002270 LY_CHECK_RET(parse_type_fracdigits(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002271 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002272 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002273 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002274 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002275 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276 return LY_EVALID;
2277 }
2278 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002279 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002280
Radek Krejci33090f92020-12-17 20:12:46 +01002281 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002282 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002284 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002285 if (type->path) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002286 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002287 return LY_EVALID;
2288 }
2289
aPiecek4bb1e372021-05-07 11:01:00 +02002290 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2291 * corresponding structure, so in case of failure, the lysp_module_free function will take
2292 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2293 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2294 */
Michal Vasko72c6d642024-02-27 14:59:01 +01002295 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 +02002296 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002297 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002298 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002299 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002300 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002301 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002302 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002303 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002304 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002305 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002306 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002308 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002310 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 return LY_EVALID;
2312 }
2313 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002314 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002315
Radek Krejci33090f92020-12-17 20:12:46 +01002316 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002317 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002319 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002320 LY_CHECK_RET(parse_type_reqinstance(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002321 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002322 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002323 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002324 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002325 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002326 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002328 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002329 LY_CHECK_RET(parse_ext(ctx, word, word_len, type, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 break;
2331 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002332 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002333 return LY_EVALID;
2334 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002335 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002336 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002337
2338cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002339 return ret;
2340}
2341
Michal Vaskoea5abea2018-09-18 13:10:54 +02002342/**
2343 * @brief Parse the leaf statement.
2344 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002345 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002346 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002347 * @return LY_ERR values.
2348 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002349LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002350parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002351{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002352 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002353 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002354 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002355 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002356 struct lysp_node_leaf *leaf;
2357
David Sedlák60adc092019-08-06 15:57:02 +02002358 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002359 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002361 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002364 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002365 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366
2367 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002368 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002370 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002371 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002373 case LY_STMT_DEFAULT:
Michal Vasko72c6d642024-02-27 14:59:01 +01002374 LY_CHECK_RET(parse_text_field(ctx, &leaf->dflt, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG,
2375 &leaf->dflt.flags, &leaf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002376 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002378 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002379 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 +02002380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002381 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002382 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002384 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002385 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002387 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002388 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002390 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002391 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 +02002392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002393 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002394 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002396 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002397 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002399 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002400 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 +02002401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002402 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002403 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002405 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002406 LY_CHECK_RET(parse_ext(ctx, word, word_len, leaf, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002407 break;
2408 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002409 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002410 return LY_EVALID;
2411 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002412 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002414
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415 /* mandatory substatements */
2416 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002417 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 return LY_EVALID;
2419 }
2420
Michal Vasko12ef5362022-09-16 15:13:58 +02002421cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 return ret;
2423}
2424
Michal Vaskoea5abea2018-09-18 13:10:54 +02002425/**
2426 * @brief Parse the max-elements statement.
2427 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002428 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002429 * @param[in,out] max Value to write to.
2430 * @param[in,out] flags Flags to write to.
2431 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002432 * @return LY_ERR values.
2433 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002434LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002435parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002437 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002438 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002439 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002440 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002441 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002442
2443 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002444 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445 return LY_EVALID;
2446 }
2447 *flags |= LYS_SET_MAX;
2448
2449 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002450 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002451
2452 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002453 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002454 ret = LY_EVALID;
2455 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002456 }
2457
Radek Krejci7f9b6512019-09-18 13:11:09 +02002458 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002459 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002460 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002461 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002462 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002463 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002464 ret = LY_EVALID;
2465 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002466 }
2467 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002468 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002469 ret = LY_EVALID;
2470 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002471 }
2472
2473 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002474 } else {
2475 /* unbounded */
2476 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002479 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002481 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002482 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 +02002483 break;
2484 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002485 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002486 ret = LY_EVALID;
2487 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002488 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002489 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002491
2492cleanup:
2493 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002494 return ret;
2495}
2496
Michal Vaskoea5abea2018-09-18 13:10:54 +02002497/**
2498 * @brief Parse the min-elements statement.
2499 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002500 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002501 * @param[in,out] min Value to write to.
2502 * @param[in,out] flags Flags to write to.
2503 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002504 * @return LY_ERR values.
2505 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002506LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002507parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002509 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002510 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002511 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002512 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002513 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002514
2515 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002516 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 return LY_EVALID;
2518 }
2519 *flags |= LYS_SET_MIN;
2520
2521 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002522 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523
2524 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002525 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002526 ret = LY_EVALID;
2527 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 }
2529
2530 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002531 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002532 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002533 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002534 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002535 ret = LY_EVALID;
2536 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 }
2538 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002539 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002540 ret = LY_EVALID;
2541 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542 }
2543 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002544
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002545 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002547 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002548 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 +02002549 break;
2550 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002551 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002552 ret = LY_EVALID;
2553 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002555 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002557
2558cleanup:
2559 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 return ret;
2561}
2562
Michal Vaskoea5abea2018-09-18 13:10:54 +02002563/**
2564 * @brief Parse the ordered-by statement.
2565 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002566 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002567 * @param[in,out] llist List or leaf-list to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002568 * @return LY_ERR values.
2569 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002571parse_orderedby(struct lysp_yang_ctx *ctx, struct lysp_node *llist)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002573 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002574 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002575 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002576 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577
Michal Vasko193dacd2022-10-13 08:43:05 +02002578 if (llist->flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002579 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002580 return LY_EVALID;
2581 }
2582
2583 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002584 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002585
Radek Krejcif13b87b2020-12-01 22:02:17 +01002586 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002587 llist->flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002588 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002589 llist->flags |= LYS_ORDBY_USER;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002591 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002592 ret = LY_EVALID;
2593 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002595
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002596 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002598 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002599 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 +02002600 break;
2601 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002602 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002603 ret = LY_EVALID;
2604 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002605 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002606 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002608
2609cleanup:
2610 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611 return ret;
2612}
2613
Michal Vaskoea5abea2018-09-18 13:10:54 +02002614/**
2615 * @brief Parse the leaf-list statement.
2616 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002617 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002618 * @param[in,out] siblings Siblings to add to.
2619 *
2620 * @return LY_ERR values.
2621 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002622LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002623parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002625 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002627 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 struct lysp_node_leaflist *llist;
2630
David Sedlák60adc092019-08-06 15:57:02 +02002631 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002632 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002634 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002637 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002638 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639
2640 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002641 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002643 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002644 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002646 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002647 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002648 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002650 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002651 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 +02002652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002653 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002654 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002656 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002657 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002659 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002660 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002662 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002663 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002664 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002665 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02002666 LY_CHECK_RET(parse_orderedby(ctx, &llist->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002668 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002669 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 +02002670 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002671 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002672 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002674 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002675 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002676 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002677 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002678 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 +02002679 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002680 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002681 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002683 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002684 LY_CHECK_RET(parse_ext(ctx, word, word_len, llist, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685 break;
2686 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002687 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688 return LY_EVALID;
2689 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002690 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002692
Michal Vasko7fbc8162018-09-17 10:35:16 +02002693 /* mandatory substatements */
2694 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002695 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002696 return LY_EVALID;
2697 }
2698
Michal Vasko12ef5362022-09-16 15:13:58 +02002699cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 return ret;
2701}
2702
Michal Vaskoea5abea2018-09-18 13:10:54 +02002703/**
2704 * @brief Parse the refine statement.
2705 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002706 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002707 * @param[in,out] refines Refines to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002708 * @return LY_ERR values.
2709 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002711parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002713 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002714 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002715 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002716 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002717 struct lysp_refine *rf;
2718
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002719 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720
2721 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002722 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002723 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002724 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002725
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002726 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002728 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002729 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002730 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002731 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002732 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002734 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002735 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 +02002736 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002737 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002738 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002739 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002740 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002741 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002742 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002743 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002744 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002745 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002746 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002747 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002748 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002750 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002751 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002753 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002754 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 +02002755 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002756 case LY_STMT_PRESENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002757 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 +02002758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002759 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002760 LY_CHECK_RET(parse_ext(ctx, word, word_len, rf, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 break;
2762 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002763 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764 return LY_EVALID;
2765 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002766 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002768
2769cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 return ret;
2771}
2772
Michal Vaskoea5abea2018-09-18 13:10:54 +02002773/**
2774 * @brief Parse the typedef statement.
2775 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002776 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002777 * @param[in,out] typedefs Typedefs to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002778 * @return LY_ERR values.
2779 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002780static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002781parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002782{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002783 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002785 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002786 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 struct lysp_tpdf *tpdf;
2788
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002789 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002790
2791 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002792 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002793 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794
2795 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002796 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002798 case LY_STMT_DEFAULT:
Michal Vasko72c6d642024-02-27 14:59:01 +01002799 LY_CHECK_RET(parse_text_field(ctx, &tpdf->dflt, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG,
2800 &tpdf->dflt.flags, &tpdf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002801 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002803 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002804 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 +02002805 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002806 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002807 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 +02002808 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002809 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002810 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002812 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002813 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002815 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002816 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 +02002817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002818 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002819 LY_CHECK_RET(parse_ext(ctx, word, word_len, tpdf, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 break;
2821 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002822 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823 return LY_EVALID;
2824 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002825 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002827
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 /* mandatory substatements */
2829 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002830 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 return LY_EVALID;
2832 }
2833
Radek Krejcibbe09a92018-11-08 09:36:54 +01002834 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002835 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002836 assert(ctx->main_ctx);
2837 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002838 }
2839
Michal Vasko12ef5362022-09-16 15:13:58 +02002840cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002841 return ret;
2842}
2843
Michal Vaskoea5abea2018-09-18 13:10:54 +02002844/**
2845 * @brief Parse the input or output statement.
2846 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002847 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002848 * @param[in] kw Type of this particular keyword
2849 * @param[in,out] inout_p Input/output pointer to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002850 * @return LY_ERR values.
2851 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002852static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002853parse_inout(struct lysp_yang_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002854 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002856 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002858 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002859 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002860 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002862 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002863 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 return LY_EVALID;
2865 }
2866
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002867 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002868 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2869 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002870 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002871
2872 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002873 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002874 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002875 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002876 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002877 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002878 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002879 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002881 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002882 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002884 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002885 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002886 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002887 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002888 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002890 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002891 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002893 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002894 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002896 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002897 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002899 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002900 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002902 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002903 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002904 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002906 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002907 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002908 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002909 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002910 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_p, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002911 break;
2912 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002913 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002914 return LY_EVALID;
2915 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002916 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002918
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002919cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002920 return ret;
2921}
2922
Michal Vaskoea5abea2018-09-18 13:10:54 +02002923/**
2924 * @brief Parse the action statement.
2925 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002926 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002927 * @param[in,out] actions Actions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002928 * @return LY_ERR values.
2929 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002930LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002931parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002933 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002934 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002935 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002936 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002937 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002938
Radek Krejci2a9fc652021-01-22 17:44:34 +01002939 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002940
2941 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002942 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002943 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002944 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002945 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002947 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002949 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002950 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 +02002951 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002952 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002953 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002954 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002955 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002956 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 +02002957 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002958 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002959 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 break;
2961
Radek Krejcid6b76452019-09-03 17:03:03 +02002962 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002963 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002965 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002966 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967 break;
2968
Radek Krejcid6b76452019-09-03 17:03:03 +02002969 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002970 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002971 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002972 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002973 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002975 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002976 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 +02002977 break;
2978 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002979 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002980 return LY_EVALID;
2981 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002982 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002984
2985 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2986 if (!act->input.nodetype) {
2987 act->input.nodetype = LYS_INPUT;
2988 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002989 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002990 }
2991 if (!act->output.nodetype) {
2992 act->output.nodetype = LYS_OUTPUT;
2993 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002994 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002995 }
2996
Michal Vasko12ef5362022-09-16 15:13:58 +02002997cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 return ret;
2999}
3000
Michal Vaskoea5abea2018-09-18 13:10:54 +02003001/**
3002 * @brief Parse the notification statement.
3003 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003004 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003005 * @param[in,out] notifs Notifications to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003006 * @return LY_ERR values.
3007 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003008LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003009parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003011 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003013 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003014 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003015 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016
Radek Krejci2a9fc652021-01-22 17:44:34 +01003017 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018
3019 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003020 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003021 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003022 notif->nodetype = LYS_NOTIF;
3023 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003025 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003027 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003028 LY_CHECK_RET(parse_text_field(ctx, notif->dsc, LY_STMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, NULL,
3029 &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003031 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003032 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003034 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003035 LY_CHECK_RET(parse_text_field(ctx, notif->ref, LY_STMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, NULL,
3036 &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003037 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003038 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003039 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040 break;
3041
Radek Krejcid6b76452019-09-03 17:03:03 +02003042 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003043 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02003044 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003045 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003046 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003048 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01003049 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003051 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003052 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003054 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003055 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003057 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003058 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003059 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003060 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003061 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003063 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003064 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065 break;
3066
Radek Krejcid6b76452019-09-03 17:03:03 +02003067 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003068 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003069 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003070 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003071 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003072 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003074 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003075 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003076 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003077 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003078 LY_CHECK_RET(parse_ext(ctx, word, word_len, notif, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 break;
3080 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003081 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003082 return LY_EVALID;
3083 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003084 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003086
Michal Vasko12ef5362022-09-16 15:13:58 +02003087cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003088 return ret;
3089}
3090
Michal Vaskoea5abea2018-09-18 13:10:54 +02003091/**
3092 * @brief Parse the grouping statement.
3093 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003094 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003095 * @param[in,out] groupings Groupings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003096 * @return LY_ERR values.
3097 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003098LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003099parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003100{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003101 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003103 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003104 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003105 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003106
Radek Krejci2a9fc652021-01-22 17:44:34 +01003107 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108
3109 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003110 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003111 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003112 grp->nodetype = LYS_GROUPING;
3113 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003114
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003115 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003116 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003117 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003118 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 +02003119 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003120 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003121 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 +02003122 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003123 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003124 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125 break;
3126
Radek Krejcid6b76452019-09-03 17:03:03 +02003127 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003128 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003129 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003130 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003131 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003133 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003134 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003136 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003137 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003138 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003139 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003140 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003142 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003143 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003145 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003146 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003148 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003149 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003150 break;
3151
Radek Krejcid6b76452019-09-03 17:03:03 +02003152 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003153 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003155 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003156 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003157 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003159 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003160 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003162 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003163 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003164 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003166 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003167 LY_CHECK_RET(parse_ext(ctx, word, word_len, grp, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003168 break;
3169 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003170 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 return LY_EVALID;
3172 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003173 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003175
aPiecek63e080d2021-06-29 13:53:28 +02003176 /* store data for collision check */
3177 if (parent) {
3178 assert(ctx->main_ctx);
3179 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3180 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003181
Michal Vasko12ef5362022-09-16 15:13:58 +02003182cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 return ret;
3184}
3185
Michal Vaskoea5abea2018-09-18 13:10:54 +02003186/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003187 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003188 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003189 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003190 * @param[in,out] augments Augments to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003191 * @return LY_ERR values.
3192 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003193LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003194parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003196 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003197 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003198 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003199 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003200 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201
Radek Krejci2a9fc652021-01-22 17:44:34 +01003202 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203
3204 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003205 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003206 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003207 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003208 aug->nodetype = LYS_AUGMENT;
3209 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003211 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003213 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003214 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 +02003215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003216 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003217 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003219 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003220 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 +02003221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003222 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003223 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003225 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003226 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 break;
3228
Radek Krejcid6b76452019-09-03 17:03:03 +02003229 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003230 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003231 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003232 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003233 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003234 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003235 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003236 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003238 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003239 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003241 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003242 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003244 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003245 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003247 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003248 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003250 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003251 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003253 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003254 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255 break;
3256
Radek Krejcid6b76452019-09-03 17:03:03 +02003257 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003258 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003259 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003261 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003262 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003263 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003265 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003266 LY_CHECK_RET(parse_ext(ctx, word, word_len, aug, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267 break;
3268 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003269 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270 return LY_EVALID;
3271 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003272 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003274
Michal Vasko12ef5362022-09-16 15:13:58 +02003275cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276 return ret;
3277}
3278
Michal Vaskoea5abea2018-09-18 13:10:54 +02003279/**
3280 * @brief Parse the uses statement.
3281 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003282 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003283 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003284 * @return LY_ERR values.
3285 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003286LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003287parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003289 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003290 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003291 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003292 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003293 struct lysp_node_uses *uses;
3294
David Sedlák60adc092019-08-06 15:57:02 +02003295 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003296 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003298 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003299
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003301 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003302 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003303
3304 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003305 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003307 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003308 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 +02003309 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003310 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003311 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003313 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003314 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 +02003315 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003316 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003317 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003319 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003320 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321 break;
3322
Radek Krejcid6b76452019-09-03 17:03:03 +02003323 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003324 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003326 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003327 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003328 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003329 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003330 LY_CHECK_RET(parse_ext(ctx, word, word_len, uses, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331 break;
3332 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003333 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003334 return LY_EVALID;
3335 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003336 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003338
Michal Vasko12ef5362022-09-16 15:13:58 +02003339cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340 return ret;
3341}
3342
Michal Vaskoea5abea2018-09-18 13:10:54 +02003343/**
3344 * @brief Parse the case statement.
3345 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003346 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003347 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003348 * @return LY_ERR values.
3349 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003350LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003351parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003353 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003354 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003355 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003356 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357 struct lysp_node_case *cas;
3358
David Sedlák60adc092019-08-06 15:57:02 +02003359 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003360 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003362 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363
Michal Vasko7fbc8162018-09-17 10:35:16 +02003364 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003365 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003366 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367
3368 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003369 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003371 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003372 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 +02003373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003374 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003375 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003377 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003378 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 +02003379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003380 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003381 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003383 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003384 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003385 break;
3386
Radek Krejcid6b76452019-09-03 17:03:03 +02003387 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003388 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003389 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003390 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003391 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003393 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003394 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003396 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003397 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003399 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003400 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003402 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003403 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003405 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003406 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003407 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003408 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003409 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003411 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003412 LY_CHECK_RET(parse_ext(ctx, word, word_len, cas, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413 break;
3414 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003415 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416 return LY_EVALID;
3417 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003418 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003419 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003420
3421cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003422 return ret;
3423}
3424
Michal Vaskoea5abea2018-09-18 13:10:54 +02003425/**
3426 * @brief Parse the choice statement.
3427 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003428 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003429 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003430 * @return LY_ERR values.
3431 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003432LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003433parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003434{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003435 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003437 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003438 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003439 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003440
David Sedlák60adc092019-08-06 15:57:02 +02003441 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003442 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003443 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003444 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003447 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003448 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449
3450 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003451 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003454 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003456 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003457 LY_CHECK_RET(parse_text_field(ctx, choice->dsc, LY_STMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, NULL,
3458 &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003461 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003463 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003464 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003466 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003467 LY_CHECK_RET(parse_text_field(ctx, choice->ref, LY_STMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, NULL,
3468 &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003469 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003470 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003471 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003473 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003474 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003476 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003477 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 +01003478 &choice->dflt.flags, &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003479 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 break;
3481
Radek Krejcid6b76452019-09-03 17:03:03 +02003482 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003483 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003484 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003485 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003486 LY_CHECK_RET(parse_any(ctx, kw, (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_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003489 LY_CHECK_RET(parse_case(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_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003492 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003493 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003495 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003496 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003498 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003499 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003501 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003502 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003504 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003505 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003507 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003508 LY_CHECK_RET(parse_ext(ctx, word, word_len, choice, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 break;
3510 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003511 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512 return LY_EVALID;
3513 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003514 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003515 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003516
3517cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 return ret;
3519}
3520
Michal Vaskoea5abea2018-09-18 13:10:54 +02003521/**
3522 * @brief Parse the container statement.
3523 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003524 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003525 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003526 * @return LY_ERR values.
3527 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003528LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003529parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530{
3531 LY_ERR ret = 0;
3532 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003533 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003534 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535 struct lysp_node_container *cont;
3536
David Sedlák60adc092019-08-06 15:57:02 +02003537 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003538 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003540 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003543 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003544 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545
3546 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003547 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003549 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003550 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003552 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003553 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 +02003554 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003555 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003556 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003558 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003559 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 +02003560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003561 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003562 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003564 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003565 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003567 case LY_STMT_PRESENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003568 LY_CHECK_RET(parse_text_field(ctx, cont->presence, LY_STMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, NULL,
3569 &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570 break;
3571
Radek Krejcid6b76452019-09-03 17:03:03 +02003572 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003573 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003574 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003575 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003576 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003578 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003579 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003581 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003582 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003583 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003584 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003585 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003587 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003588 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003590 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003591 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003593 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003594 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 break;
3596
Radek Krejcid6b76452019-09-03 17:03:03 +02003597 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003598 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003600 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003601 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003603 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003604 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003605 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003607 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003608 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003610 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003611 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003612 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003614 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003615 LY_CHECK_RET(parse_ext(ctx, word, word_len, cont, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003616 break;
3617 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003618 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003619 return LY_EVALID;
3620 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003621 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003623
Michal Vasko12ef5362022-09-16 15:13:58 +02003624cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 return ret;
3626}
3627
Michal Vaskoea5abea2018-09-18 13:10:54 +02003628/**
3629 * @brief Parse the list statement.
3630 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003631 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003632 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003633 * @return LY_ERR values.
3634 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003635LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003636parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003638 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003640 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003641 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003642 struct lysp_node_list *list;
3643
David Sedlák60adc092019-08-06 15:57:02 +02003644 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003645 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003647 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003648
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003650 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003651 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652
3653 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003654 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003655 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003656 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003657 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003659 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003660 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 +02003661 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003662 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003663 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003665 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003666 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 +02003667 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003668 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003669 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003670 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003671 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003672 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003674 case LY_STMT_KEY:
Michal Vasko72c6d642024-02-27 14:59:01 +01003675 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 +02003676 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003677 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003678 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003680 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003681 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003683 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003684 LY_CHECK_RET(parse_orderedby(ctx, &list->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003686 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003687 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688 break;
3689
Radek Krejcid6b76452019-09-03 17:03:03 +02003690 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003691 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003692 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003693 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003694 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003695 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003696 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003697 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003698 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003699 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003700 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003702 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003703 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003705 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003706 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003707 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003708 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003709 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003710 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003711 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003712 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003713 break;
3714
Radek Krejcid6b76452019-09-03 17:03:03 +02003715 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003716 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003718 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003719 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003721 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003722 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003723 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003725 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003726 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003727 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003728 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003729 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003730 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003731 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003732 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003733 LY_CHECK_RET(parse_ext(ctx, word, word_len, list, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 break;
3735 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003736 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003737 return LY_EVALID;
3738 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003739 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003740 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003741
Michal Vasko12ef5362022-09-16 15:13:58 +02003742cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743 return ret;
3744}
3745
Michal Vaskoea5abea2018-09-18 13:10:54 +02003746/**
3747 * @brief Parse the yin-element statement.
3748 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003749 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003750 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003751 * @return LY_ERR values.
3752 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003753static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003754parse_yinelement(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003756 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003758 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003759 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760
Michal Vasko193dacd2022-10-13 08:43:05 +02003761 if (ext->flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003762 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003763 return LY_EVALID;
3764 }
3765
3766 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003767 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768
Radek Krejcif13b87b2020-12-01 22:02:17 +01003769 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003770 ext->flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003771 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003772 ext->flags |= LYS_YINELEM_FALSE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003773 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003774 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 free(buf);
3776 return LY_EVALID;
3777 }
3778 free(buf);
3779
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003780 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003782 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003783 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_YIN_ELEMENT, 0, &ext->exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003784 LY_CHECK_RET(ret);
3785 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003787 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 return LY_EVALID;
3789 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003790 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003792
3793cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794 return ret;
3795}
3796
Michal Vaskoea5abea2018-09-18 13:10:54 +02003797/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003798 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003799 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003800 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003801 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003802 * @return LY_ERR values.
3803 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003804static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003805parse_argument(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003806{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003807 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003809 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003810 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003811
Michal Vasko193dacd2022-10-13 08:43:05 +02003812 if (ext->argname) {
David Sedlákb3077192019-06-19 10:55:37 +02003813 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 return LY_EVALID;
3815 }
3816
3817 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003818 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko193dacd2022-10-13 08:43:05 +02003819 INSERT_WORD_GOTO(ctx, buf, ext->argname, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003820
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003821 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003822 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003823 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003824 LY_CHECK_RET(parse_yinelement(ctx, ext));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003825 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003826 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003827 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_ARGUMENT, 0, &ext->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 break;
3829 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003830 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003831 return LY_EVALID;
3832 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003833 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003835
3836cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003837 return ret;
3838}
3839
Michal Vaskoea5abea2018-09-18 13:10:54 +02003840/**
3841 * @brief Parse the extension statement.
3842 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003843 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003844 * @param[in,out] extensions Extensions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003845 * @return LY_ERR values.
3846 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003848parse_extension(struct lysp_yang_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003849{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003850 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003851 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003852 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003853 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003854 struct lysp_ext *ex;
3855
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003856 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003857
3858 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003859 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003860 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003862 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003864 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003865 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 +02003866 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003867 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003868 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 +02003869 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003870 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003871 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003873 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003874 LY_CHECK_RET(parse_argument(ctx, ex));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003875 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003876 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003877 LY_CHECK_RET(parse_ext(ctx, word, word_len, ex, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003878 break;
3879 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003880 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 return LY_EVALID;
3882 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003883 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003885
3886cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 return ret;
3888}
3889
Michal Vaskoea5abea2018-09-18 13:10:54 +02003890/**
3891 * @brief Parse the deviate statement.
3892 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003893 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003894 * @param[in,out] deviates Deviates to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003895 * @return LY_ERR values.
3896 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003897LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003898parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003899{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003900 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003901 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003902 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003903 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003904 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003905 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003906 struct lysp_deviate_add *d_add = NULL;
3907 struct lysp_deviate_rpl *d_rpl = NULL;
3908 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003909 const char **d_units = NULL;
3910 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003911 struct lysp_restr **d_musts = NULL;
3912 uint16_t *d_flags = 0;
3913 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914
3915 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003916 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917
Radek Krejcif13b87b2020-12-01 22:02:17 +01003918 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003920 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003922 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003923 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003924 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925 dev_mod = LYS_DEV_DELETE;
3926 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003927 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003928 ret = LY_EVALID;
3929 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931
3932 /* create structure */
3933 switch (dev_mod) {
3934 case LYS_DEV_NOT_SUPPORTED:
3935 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003936 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 break;
3938 case LYS_DEV_ADD:
3939 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003940 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003941 d = (struct lysp_deviate *)d_add;
3942 d_units = &d_add->units;
3943 d_uniques = &d_add->uniques;
3944 d_dflts = &d_add->dflts;
3945 d_musts = &d_add->musts;
3946 d_flags = &d_add->flags;
3947 d_min = &d_add->min;
3948 d_max = &d_add->max;
3949 break;
3950 case LYS_DEV_REPLACE:
3951 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003952 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953 d = (struct lysp_deviate *)d_rpl;
3954 d_units = &d_rpl->units;
3955 d_flags = &d_rpl->flags;
3956 d_min = &d_rpl->min;
3957 d_max = &d_rpl->max;
3958 break;
3959 case LYS_DEV_DELETE:
3960 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003961 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962 d = (struct lysp_deviate *)d_del;
3963 d_units = &d_del->units;
3964 d_uniques = &d_del->uniques;
3965 d_dflts = &d_del->dflts;
3966 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967 break;
3968 default:
3969 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003970 LOGINT(PARSER_CTX(ctx));
3971 ret = LY_EINT;
3972 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003973 }
3974 d->mod = dev_mod;
3975
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003976 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003977 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003978 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003979 switch (dev_mod) {
3980 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003981 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003982 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003983 ret = LY_EVALID;
3984 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003986 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 break;
3988 }
3989 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003990 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991 switch (dev_mod) {
3992 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02003993 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003994 ret = LY_EVALID;
3995 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 case LYS_DEV_REPLACE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003997 ret = parse_text_field(ctx, &d_rpl->dflt, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG,
3998 &d_rpl->dflt.flags, &d->exts);
Michal Vasko193dacd2022-10-13 08:43:05 +02003999 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004000 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001 break;
4002 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004003 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 +02004004 break;
4005 }
4006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004007 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004008 switch (dev_mod) {
4009 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004010 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004011 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004012 ret = LY_EVALID;
4013 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004014 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004015 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016 break;
4017 }
4018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004019 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004020 switch (dev_mod) {
4021 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004022 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004023 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004024 ret = LY_EVALID;
4025 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004026 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004027 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004028 break;
4029 }
4030 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004031 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004032 switch (dev_mod) {
4033 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004034 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004035 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004036 ret = LY_EVALID;
4037 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004038 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004039 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040 break;
4041 }
4042 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004043 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004044 switch (dev_mod) {
4045 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004046 case LYS_DEV_REPLACE:
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:
Michal Vasko12ef5362022-09-16 15:13:58 +02004051 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004052 break;
4053 }
4054 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004055 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 switch (dev_mod) {
4057 case LYS_DEV_NOT_SUPPORTED:
4058 case LYS_DEV_ADD:
4059 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004060 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004061 ret = LY_EVALID;
4062 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004063 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004064 if (d_rpl->type) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004065 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004066 ret = LY_EVALID;
4067 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004068 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004069 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004070 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4071 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004072 break;
4073 }
4074 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004075 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076 switch (dev_mod) {
4077 case LYS_DEV_NOT_SUPPORTED:
4078 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004079 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004080 ret = LY_EVALID;
4081 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004082 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004083 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 +02004084 break;
4085 }
4086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004087 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004088 switch (dev_mod) {
4089 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02004090 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004091 ret = LY_EVALID;
4092 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 default:
Michal Vasko72c6d642024-02-27 14:59:01 +01004094 ret = parse_text_field(ctx, *d_units, LY_STMT_UNITS, 0, d_units, Y_STR_ARG, NULL, &d->exts);
4095 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004096 break;
4097 }
4098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004099 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004100 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 +02004101 break;
4102 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004103 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004104 ret = LY_EVALID;
4105 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004107 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004108 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004109
4110cleanup:
4111 free(buf);
4112 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004113 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004114 free(d);
4115 } else {
4116 /* insert into siblings */
4117 LY_LIST_INSERT(deviates, d, next);
4118 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004119 return ret;
4120}
4121
Michal Vaskoea5abea2018-09-18 13:10:54 +02004122/**
4123 * @brief Parse the deviation statement.
4124 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004125 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004126 * @param[in,out] deviations Deviations to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004127 * @return LY_ERR values.
4128 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004129LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004130parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004132 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004133 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004134 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004135 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004136 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004137 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004138
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004139 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004140
4141 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004142 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004143 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004144 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004145
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004146 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004147 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004148 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004149 ret = parse_text_field(ctx, dev->dsc, LY_STMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, NULL, &dev->exts);
4150 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004152 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004153 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004155 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004156 ret = parse_text_field(ctx, dev->ref, LY_STMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, NULL, &dev->exts);
4157 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004159 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004160 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 +02004161 break;
4162 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004163 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004164 ret = LY_EVALID;
4165 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004167 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004169
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 /* mandatory substatements */
4171 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004172 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004173 ret = LY_EVALID;
4174 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175 }
4176
Michal Vasko12ef5362022-09-16 15:13:58 +02004177cleanup:
4178 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004179 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004180 LY_ARRAY_DECREMENT_FREE(*deviations);
4181 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004182 return ret;
4183}
4184
Michal Vaskoea5abea2018-09-18 13:10:54 +02004185/**
4186 * @brief Parse the feature statement.
4187 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004188 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004189 * @param[in,out] features Features to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004190 * @return LY_ERR values.
4191 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004192LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004193parse_feature(struct lysp_yang_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004194{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004195 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004197 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004198 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 struct lysp_feature *feat;
4200
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004201 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004202
4203 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004204 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004205 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004206
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004207 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004208 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004209 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004210 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 +02004211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004212 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004213 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004215 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004216 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 +02004217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004218 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004219 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004221 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004222 LY_CHECK_RET(parse_ext(ctx, word, word_len, feat, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004223 break;
4224 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004225 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004226 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004228 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004229 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004230
4231cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004232 return ret;
4233}
4234
Michal Vaskoea5abea2018-09-18 13:10:54 +02004235/**
4236 * @brief Parse the identity statement.
4237 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004238 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004239 * @param[in,out] identities Identities to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004240 * @return LY_ERR values.
4241 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004242LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004243parse_identity(struct lysp_yang_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004245 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004246 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004247 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004248 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004249 struct lysp_ident *ident;
4250
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004251 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004252
4253 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004254 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004255 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004256
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004257 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004258 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004259 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004260 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 +02004261 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004262 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004263 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004264 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004266 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004267 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 +02004268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004269 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004270 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004272 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004273 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
Michal Vasko72c6d642024-02-27 14:59:01 +01004274 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
4275 "Identity can be derived from multiple base identities only in YANG 1.1 modules");
Radek Krejci10113652018-11-14 16:56:50 +01004276 return LY_EVALID;
4277 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004278 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 +02004279 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004280 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004281 LY_CHECK_RET(parse_ext(ctx, word, word_len, ident, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004282 break;
4283 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004284 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004285 return LY_EVALID;
4286 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004287 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004288 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004289
4290cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004291 return ret;
4292}
4293
Michal Vaskoea5abea2018-09-18 13:10:54 +02004294/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004295 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004296 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004297 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004298 * @param[in,out] mod Module to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004299 * @return LY_ERR values.
4300 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004301LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004302parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004303{
4304 LY_ERR ret = 0;
4305 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004306 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004307 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004308 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004309 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004310
Michal Vaskoc3781c32020-10-06 14:04:08 +02004311 mod->is_submod = 0;
4312
4313 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004314 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004315 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004316
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004317 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004318
Radek Krejcie3846472018-10-15 15:24:51 +02004319#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004320 if (mod_stmt > SECTION) {\
4321 LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;\
4322 } mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004323
Michal Vasko7fbc8162018-09-17 10:35:16 +02004324 switch (kw) {
4325 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004326 case LY_STMT_NAMESPACE:
4327 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004328 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004330 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004331 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004332 break;
4333 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004334 case LY_STMT_INCLUDE:
4335 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004336 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004337 break;
4338 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004339 case LY_STMT_ORGANIZATION:
4340 case LY_STMT_CONTACT:
4341 case LY_STMT_DESCRIPTION:
4342 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004343 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004344 break;
4345
4346 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004347 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004348 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004349 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004350 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004351 case LY_STMT_ANYDATA:
4352 case LY_STMT_ANYXML:
4353 case LY_STMT_AUGMENT:
4354 case LY_STMT_CHOICE:
4355 case LY_STMT_CONTAINER:
4356 case LY_STMT_DEVIATION:
4357 case LY_STMT_EXTENSION:
4358 case LY_STMT_FEATURE:
4359 case LY_STMT_GROUPING:
4360 case LY_STMT_IDENTITY:
4361 case LY_STMT_LEAF:
4362 case LY_STMT_LEAF_LIST:
4363 case LY_STMT_LIST:
4364 case LY_STMT_NOTIFICATION:
4365 case LY_STMT_RPC:
4366 case LY_STMT_TYPEDEF:
4367 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004368 mod_stmt = Y_MOD_BODY;
4369 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004370 case LY_STMT_EXTENSION_INSTANCE:
4371 /* no place in the statement order defined */
4372 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004373 default:
4374 /* error handled in the next switch */
4375 break;
4376 }
Radek Krejcie3846472018-10-15 15:24:51 +02004377#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004378
Radek Krejcie3846472018-10-15 15:24:51 +02004379 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004380 switch (kw) {
4381 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004382 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004383 LY_CHECK_RET(parse_yangversion(ctx, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004385 case LY_STMT_NAMESPACE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004386 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 +02004387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01004389 LY_CHECK_RET(parse_text_field(ctx, mod->mod->prefix, LY_STMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG,
4390 NULL, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004391 break;
4392
4393 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004395 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004397 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004398 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004399 break;
4400
4401 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004402 case LY_STMT_ORGANIZATION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004403 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 +02004404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004405 case LY_STMT_CONTACT:
Michal Vasko72c6d642024-02-27 14:59:01 +01004406 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 +02004407 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004408 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004409 LY_CHECK_RET(parse_text_field(ctx, mod->mod->dsc, LY_STMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, NULL,
4410 &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004412 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004413 LY_CHECK_RET(parse_text_field(ctx, mod->mod->ref, LY_STMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, NULL,
4414 &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004415 break;
4416
4417 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004418 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004419 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004420 break;
4421
4422 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004423 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004424 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004425 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004426 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004427 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004429 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004430 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004432 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004433 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004435 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004436 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004438 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004439 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004441 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004442 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004444 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004445 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004446 break;
4447
Radek Krejcid6b76452019-09-03 17:03:03 +02004448 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004449 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004450 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004451 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004452 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004453 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004454 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004455 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004456 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004457 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004458 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004459 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004460 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004461 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004462 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004463 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004464 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004466 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004467 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004468 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004469 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004470 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004471 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004472 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004473 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004475 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004476 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004477 break;
4478
4479 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004480 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004481 return LY_EVALID;
4482 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004483 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004484 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004485
Michal Vasko7fbc8162018-09-17 10:35:16 +02004486 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004487 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004488 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004489 return LY_EVALID;
4490 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004491 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004492 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004493 }
4494
Radek Krejcie9e987e2018-10-31 12:50:27 +01004495 /* submodules share the namespace with the module names, so there must not be
4496 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004497 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004498 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004499 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004500 return LY_EVALID;
4501 }
4502
Michal Vasko12ef5362022-09-16 15:13:58 +02004503cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004504 return ret;
4505}
4506
4507/**
4508 * @brief Parse submodule substatements.
4509 *
4510 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004511 * @param[out] submod Parsed submodule structure.
4512 *
4513 * @return LY_ERR values.
4514 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004515LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004516parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004517{
4518 LY_ERR ret = 0;
4519 char *buf, *word;
4520 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004521 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004522 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004523 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004524
Michal Vaskoc3781c32020-10-06 14:04:08 +02004525 submod->is_submod = 1;
4526
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004527 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004528 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004529 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004530
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004531 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004532
4533#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004534 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 +01004535
4536 switch (kw) {
4537 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004538 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004539 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004541 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004542 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4543 break;
4544 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004545 case LY_STMT_INCLUDE:
4546 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004547 CHECK_ORDER(Y_MOD_LINKAGE);
4548 break;
4549 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004550 case LY_STMT_ORGANIZATION:
4551 case LY_STMT_CONTACT:
4552 case LY_STMT_DESCRIPTION:
4553 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004554 CHECK_ORDER(Y_MOD_META);
4555 break;
4556
4557 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004558 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004559 CHECK_ORDER(Y_MOD_REVISION);
4560 break;
4561 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004562 case LY_STMT_ANYDATA:
4563 case LY_STMT_ANYXML:
4564 case LY_STMT_AUGMENT:
4565 case LY_STMT_CHOICE:
4566 case LY_STMT_CONTAINER:
4567 case LY_STMT_DEVIATION:
4568 case LY_STMT_EXTENSION:
4569 case LY_STMT_FEATURE:
4570 case LY_STMT_GROUPING:
4571 case LY_STMT_IDENTITY:
4572 case LY_STMT_LEAF:
4573 case LY_STMT_LEAF_LIST:
4574 case LY_STMT_LIST:
4575 case LY_STMT_NOTIFICATION:
4576 case LY_STMT_RPC:
4577 case LY_STMT_TYPEDEF:
4578 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004579 mod_stmt = Y_MOD_BODY;
4580 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004581 case LY_STMT_EXTENSION_INSTANCE:
4582 /* no place in the statement order defined */
4583 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004584 default:
4585 /* error handled in the next switch */
4586 break;
4587 }
4588#undef CHECK_ORDER
4589
4590 prev_kw = kw;
4591 switch (kw) {
4592 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004593 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004594 LY_CHECK_RET(parse_yangversion(ctx, (struct lysp_module *)submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004595 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004596 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02004597 LY_CHECK_RET(parse_belongsto(ctx, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004598 break;
4599
4600 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004601 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004602 if (submod->version == LYS_VERSION_1_1) {
4603 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4604 submod->name);
4605 }
Radek Krejci33090f92020-12-17 20:12:46 +01004606 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004607 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004608 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004609 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004610 break;
4611
4612 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004613 case LY_STMT_ORGANIZATION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004614 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 +01004615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004616 case LY_STMT_CONTACT:
Michal Vasko72c6d642024-02-27 14:59:01 +01004617 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 +01004618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004619 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004620 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 +01004621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004622 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004623 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 +01004624 break;
4625
4626 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004627 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004628 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004629 break;
4630
4631 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004632 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004633 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004634 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004635 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004636 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004638 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004639 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004640 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004641 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004642 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004643 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004644 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004645 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004647 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004648 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004650 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004651 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004653 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004654 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004655 break;
4656
Radek Krejcid6b76452019-09-03 17:03:03 +02004657 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004658 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004659 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004660 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004661 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004662 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004663 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004664 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004665 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004666 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004667 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004668 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004669 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004670 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004671 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004672 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004673 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004674 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004675 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004676 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004677 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004678 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004679 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004680 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004681 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004682 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004683 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004684 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004685 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004686 break;
4687
4688 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004689 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004690 return LY_EVALID;
4691 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004692 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004693 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004694
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004695 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004696 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004697 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004698 return LY_EVALID;
4699 }
4700
4701 /* submodules share the namespace with the module names, so there must not be
4702 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004703 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004704 /* main modules may have different revisions */
4705 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004706 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004707 return LY_EVALID;
4708 }
4709
Michal Vasko12ef5362022-09-16 15:13:58 +02004710cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004711 return ret;
4712}
4713
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004714/**
4715 * @brief Skip any redundant characters, namely whitespaces and comments.
4716 *
4717 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004718 * @return LY_SUCCESS on success.
4719 * @return LY_EVALID on invalid comment.
4720 */
4721static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004722skip_redundant_chars(struct lysp_yang_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004723{
4724 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004725 while (ctx->in->current[0]) {
4726 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004727 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004728 ly_in_skip(ctx->in, 2);
4729 LY_CHECK_RET(skip_comment(ctx, 1));
4730 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004731 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004732 ly_in_skip(ctx->in, 2);
4733 LY_CHECK_RET(skip_comment(ctx, 2));
4734 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004735 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004736 if (ctx->in->current[0] == '\n') {
4737 LY_IN_NEW_LINE(ctx->in);
4738 }
Radek Krejci33090f92020-12-17 20:12:46 +01004739 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004740 } else {
4741 break;
4742 }
4743 }
4744
4745 return LY_SUCCESS;
4746}
4747
Radek Krejcid4557c62018-09-17 11:42:09 +02004748LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004749yang_parse_submodule(struct lysp_yang_ctx **context, struct ly_ctx *ly_ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004750 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004751{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004752 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004753 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004754 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004755 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004756 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004757 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004758
aPiecek56be92a2021-07-01 07:18:10 +02004759 assert(context && ly_ctx && main_ctx && in && submod);
4760
David Sedlák1b623122019-08-05 15:27:49 +02004761 /* create context */
4762 *context = calloc(1, sizeof **context);
4763 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004764 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004765 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004766 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004767
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004768 mod_p = calloc(1, sizeof *mod_p);
4769 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004770 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004771 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004772
4773 /* use main context parsed mods adding the current one */
4774 (*context)->parsed_mods = main_ctx->parsed_mods;
4775 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004776
Michal Vasko7a266772024-01-23 11:02:38 +01004777 ly_log_location(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004778
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004779 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004780 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004781 LY_CHECK_GOTO(ret, cleanup);
4782
Michal Vasko7fbc8162018-09-17 10:35:16 +02004783 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004784 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004785 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004786
Radek Krejcid6b76452019-09-03 17:03:03 +02004787 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004788 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004789 ret = LY_EINVAL;
4790 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004791 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004792 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
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 }
4796
Michal Vasko7fbc8162018-09-17 10:35:16 +02004797 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004798 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004799 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004800
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004801 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004802 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004803 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004804 if (in->current[0]) {
4805 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004806 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004807 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004808 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004809
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004810 mod_p->parsing = 0;
4811 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004812
Radek Krejcibbe09a92018-11-08 09:36:54 +01004813cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +01004814 ly_log_location_revert(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004815 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004816 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004817 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004818 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004819 }
4820
4821 return ret;
4822}
4823
4824LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004825yang_parse_module(struct lysp_yang_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004826{
4827 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004828 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004829 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004830 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004831 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004832 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004833
David Sedlák1b623122019-08-05 15:27:49 +02004834 /* create context */
4835 *context = calloc(1, sizeof **context);
4836 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004837 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004838 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004839 (*context)->in = in;
Michal Vaskod0625d72022-10-06 15:02:50 +02004840 (*context)->main_ctx = (struct lysp_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004841
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004842 mod_p = calloc(1, sizeof *mod_p);
4843 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4844 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004845 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004846
Michal Vasko7a266772024-01-23 11:02:38 +01004847 ly_log_location(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004848
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004849 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004850 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004851 LY_CHECK_GOTO(ret, cleanup);
4852
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004853 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004854 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004855 LY_CHECK_GOTO(ret, cleanup);
4856
Radek Krejcid6b76452019-09-03 17:03:03 +02004857 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004858 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 +01004859 ret = LY_EINVAL;
4860 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004861 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004862 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004863 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004864 goto cleanup;
4865 }
4866
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004867 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004868 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004869 LY_CHECK_GOTO(ret, cleanup);
4870
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004871 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004872 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004873 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004874 if (in->current[0]) {
4875 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004876 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004877 goto cleanup;
4878 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004879
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004880 mod->parsed = mod_p;
4881
4882cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +01004883 ly_log_location_revert(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004884 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004885 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004886 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004887 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004888 }
4889
Michal Vasko7fbc8162018-09-17 10:35:16 +02004890 return ret;
4891}