blob: 4423950d8a52ed52b4ed205c36fa1a7eae6eb543 [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 */
527 break;
528 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100529 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200530 break;
531 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100532 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200533 break;
534 default:
535 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200536 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537 return LY_EVALID;
538 }
Radek Krejci33090f92020-12-17 20:12:46 +0100539 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200540 break;
541 default:
542 return LY_EINT;
543 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200544 }
545
546string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200547 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200548 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200549 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200550 return LY_EVALID;
551 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200552 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100553
554#undef STRING_ENDED
555#undef STRING_SINGLE_QUOTED
556#undef STRING_DOUBLE_QUOTED
557#undef STRING_DOUBLE_QUOTED_ESCAPED
558#undef STRING_PAUSED_NEXTSTRING
559#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560}
561
Michal Vaskoea5abea2018-09-18 13:10:54 +0200562/**
563 * @brief Get another YANG string from the raw data.
564 *
Michal Vasko72c6d642024-02-27 14:59:01 +0100565 * @param[in] ctx Yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200566 * @param[in] arg Type of YANG keyword argument expected.
Michal Vasko72c6d642024-02-27 14:59:01 +0100567 * @param[out] flags Optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
Michal Vaskob68ea142021-04-26 13:46:00 +0200568 * [schema node flags](@ref snodeflags))
Michal Vasko72c6d642024-02-27 14:59:01 +0100569 * @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 +0200570 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
Michal Vasko72c6d642024-02-27 14:59:01 +0100571 * set to NULL. Otherwise equal to @p word_p.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200572 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200573 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200574 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200575LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200576get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200577 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200578{
Michal Vaskob68ea142021-04-26 13:46:00 +0200579 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200580 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200581 uint8_t prefix = 0;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100582 ly_bool str_end = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200583
Michal Vasko7fbc8162018-09-17 10:35:16 +0200584 /* word buffer - dynamically allocated */
585 *word_b = NULL;
586
587 /* word pointer - just a pointer to data */
588 *word_p = NULL;
589
590 *word_len = 0;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100591 while (!str_end) {
Radek Krejci33090f92020-12-17 20:12:46 +0100592 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200593 case '\'':
594 case '\"':
595 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200596 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100597 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200598 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200599 ret = LY_EVALID;
600 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200601 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200602 if (flags) {
Michal Vasko72c6d642024-02-27 14:59:01 +0100603 (*flags) |= (ctx->in->current[0] == '\'') ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200604 }
Michal Vasko1e4b1162024-01-19 09:16:49 +0100605
Michal Vaskob68ea142021-04-26 13:46:00 +0200606 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200607 if (!*word_p) {
608 /* do not return NULL word */
609 *word_p = "";
610 }
Michal Vasko1e4b1162024-01-19 09:16:49 +0100611 str_end = 1;
612 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200613 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100614 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200615 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100616 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200617 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100618 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200619 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100620 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200621 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200622 } else {
623 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200624 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 +0200625 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200626 break;
627 case ' ':
628 if (*word_len) {
629 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100630 str_end = 1;
631 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200632 }
Radek Krejci33090f92020-12-17 20:12:46 +0100633 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200634 break;
635 case '\t':
636 if (*word_len) {
637 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100638 str_end = 1;
639 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200640 }
641 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100642 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200643
Radek Krejci33090f92020-12-17 20:12:46 +0100644 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200645 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200646 case '\r':
647 if (ctx->in->current[1] != '\n') {
648 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200649 ret = LY_EVALID;
650 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200651 }
652 MOVE_INPUT(ctx, 1);
653 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200654 case '\n':
655 if (*word_len) {
656 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100657 str_end = 1;
658 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100660 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100661 MOVE_INPUT(ctx, 1);
662
Michal Vasko7fbc8162018-09-17 10:35:16 +0200663 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200664 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200665 break;
666 case ';':
667 case '{':
668 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
669 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100670 str_end = 1;
671 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 }
673
Radek Krejci33090f92020-12-17 20:12:46 +0100674 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200675 ret = LY_EVALID;
676 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200677 case '}':
678 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100679 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200680 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200681 ret = LY_EVALID;
682 goto error;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100683 case '\0':
684 /* unexpected EOF */
685 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
686 ret = LY_EVALID;
687 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200689 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 +0200690 break;
691 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200692 }
693
Radek Krejci44ceedc2018-10-02 15:54:31 +0200694 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200696 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200697 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200698 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699 *word_p = *word_b;
700 }
701
702 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200703
704error:
705 free(*word_b);
706 *word_b = NULL;
707 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200708}
709
Michal Vaskoea5abea2018-09-18 13:10:54 +0200710/**
711 * @brief Get another YANG keyword from the raw data.
712 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200713 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200714 * @param[out] kw YANG keyword read.
715 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
716 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200717 * @return LY_ERR values.
718 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200719LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200720get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200722 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200723 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200724 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200725
726 if (word_p) {
727 *word_p = NULL;
728 *word_len = 0;
729 }
730
731 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100732 while (ctx->in->current[0]) {
733 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200734 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100735 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100737 MOVE_INPUT(ctx, 2);
738 LY_CHECK_RET(skip_comment(ctx, 1));
739 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200740 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100741 MOVE_INPUT(ctx, 2);
742 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200743 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200744 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200745 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200746 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200747 }
Radek Krejci13028282019-06-11 14:56:48 +0200748 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200749 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200750 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100751 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200752 ctx->indent = 0;
753 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200754 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200755 /* skip whitespaces (optsep) */
756 ++ctx->indent;
757 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200758 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100760 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200761 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100762 case '\r':
763 /* possible CRLF endline */
764 if (ctx->in->current[1] == '\n') {
765 break;
766 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100767 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200768 default:
769 /* either a keyword start or an invalid character */
770 goto keyword_start;
771 }
772
Radek Krejci33090f92020-12-17 20:12:46 +0100773 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200774 }
775
776keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100777 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100778 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200779
aPiecek93582ed2021-05-25 14:49:06 +0200780 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
781 goto success;
782 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
783 ctx->depth++;
784 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100785 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200786 return LY_EINVAL;
787 }
788 goto success;
789 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
790 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200791 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200792 }
793
Radek Krejcid6b76452019-09-03 17:03:03 +0200794 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200795 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100796 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200797 case '\r':
798 if (ctx->in->current[1] != '\n') {
799 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
800 return LY_EVALID;
801 }
802 MOVE_INPUT(ctx, 1);
803 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200804 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200805 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200806 case ' ':
807 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200808 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200809 case ':':
810 /* keyword is not actually a keyword, but prefix of an extension.
811 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
812 * and we will be checking the keyword (extension instance) itself */
813 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100814 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200815 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200816 case '{':
Michal Vasko946b70c2023-07-19 08:57:31 +0200817 case ';':
818 /* allowed only for input and output statements which are without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200819 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200820 break;
821 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100822 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200823 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100824 MOVE_INPUT(ctx, 1);
825 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200826 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200827 return LY_EVALID;
828 }
829 } else {
830 /* still can be an extension */
831 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200832
Radek Krejci156ccaf2018-10-15 15:49:17 +0200833extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100834 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200835 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
836 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200837 uint32_t c = 0;
838
Radek Krejci33090f92020-12-17 20:12:46 +0100839 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
840 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200841 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200842 /* check character validity */
Michal Vaskod0625d72022-10-06 15:02:50 +0200843 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100844 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845 }
Radek Krejci33090f92020-12-17 20:12:46 +0100846 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200847 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200848 return LY_EVALID;
849 }
850
851 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200852 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100853 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200854 return LY_EVALID;
855 }
856
Radek Krejcid6b76452019-09-03 17:03:03 +0200857 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200858 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200859
Radek Krejci626df482018-10-11 15:06:31 +0200860success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200861 if (word_p) {
862 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100863 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200864 }
865
866 return LY_SUCCESS;
867}
868
Michal Vaskoea5abea2018-09-18 13:10:54 +0200869/**
870 * @brief Parse extension instance substatements.
871 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200872 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200873 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200874 * @param[in] word Extension instance substatement name (keyword).
875 * @param[in] word_len Extension instance substatement name length.
876 * @param[in,out] child Children of this extension instance to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200877 * @return LY_ERR values.
878 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200879static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200880parse_ext_substmt(struct lysp_yang_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200881 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200882{
883 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100884 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200885 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 struct lysp_stmt *stmt, *par_child;
887
888 stmt = calloc(1, sizeof *stmt);
889 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
890
Radek Krejcibb9b1982019-04-08 14:24:59 +0200891 /* insert into parent statements */
892 if (!*child) {
893 *child = stmt;
894 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200895 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200896 par_child->next = stmt;
897 }
898
Michal Vaskofc2cd072021-02-24 13:17:17 +0100899 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200900 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200901
902 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100903 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200904 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200905 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200906 }
907
Radek Krejci8df109d2021-04-23 12:19:08 +0200908 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100909 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100910 stmt->kw = kw;
911
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200912 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
913 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
914 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200915 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200916
917cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918 return ret;
919}
920
Michal Vaskoea5abea2018-09-18 13:10:54 +0200921/**
922 * @brief Parse extension instance.
923 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200924 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200925 * @param[in] ext_name Extension instance substatement name (keyword).
926 * @param[in] ext_name_len Extension instance substatement name length.
Michal Vasko193dacd2022-10-13 08:43:05 +0200927 * @param[in] parent Current statement parent.
928 * @param[in] parent_stmt Type of @p parent statement.
929 * @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 +0200930 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200931 * @return LY_ERR values.
932 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200934parse_ext(struct lysp_yang_ctx *ctx, const char *ext_name, size_t ext_name_len, const void *parent,
935 enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100937 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200938 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200939 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200940 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200941 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200942
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200943 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200944
Michal Vaskofc2cd072021-02-24 13:17:17 +0100945 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200946 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%.*s\" without the mandatory prefix.",
947 (int)ext_name_len, ext_name);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100948 return LY_EVALID;
949 }
950
951 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200952 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200953
954 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100955 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200956 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200957 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200958 }
959
Michal Vaskofc2cd072021-02-24 13:17:17 +0100960 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200961 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200962 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100963 e->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200964 e->parent = (void *)parent;
965 e->parent_stmt = parent_stmt;
966 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100967
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200968 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
969 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
970 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200971 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200972
973cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200974 return ret;
975}
976
Michal Vaskoea5abea2018-09-18 13:10:54 +0200977/**
978 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
979 * description, etc...
980 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200981 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +0200982 * @param[in] parent Current statement parent.
983 * @param[in] parent_stmt Type of statement in @p value.
984 * @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 +0200985 * @param[in,out] value Place to store the parsed value.
986 * @param[in] arg Type of the YANG keyword argument (of the value).
Michal Vasko72c6d642024-02-27 14:59:01 +0100987 * @param[out] flags Optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
988 * [schema node flags](@ref snodeflags))
Michal Vaskoea5abea2018-09-18 13:10:54 +0200989 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200990 * @return LY_ERR values.
991 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200993parse_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 +0100994 const char **value, enum yang_arg arg, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100996 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200997 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200998 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200999 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001000
1001 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001002 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001003 return LY_EVALID;
1004 }
1005
1006 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001007 LY_CHECK_RET(get_argument(ctx, arg, flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008
1009 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +02001010 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001012 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001013 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001014 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001015 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent, parent_stmt, parent_stmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001016 break;
1017 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001018 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001019 return LY_EVALID;
1020 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001021 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001022 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001023
1024cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025 return ret;
1026}
1027
Michal Vaskoea5abea2018-09-18 13:10:54 +02001028/**
1029 * @brief Parse the yang-version statement.
1030 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001031 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001032 * @param[in,out] mod Module to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001033 * @return LY_ERR values.
1034 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001035static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001036parse_yangversion(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001038 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001040 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001041 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001042
Michal Vasko193dacd2022-10-13 08:43:05 +02001043 if (mod->version) {
David Sedlákb3077192019-06-19 10:55:37 +02001044 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001045 return LY_EVALID;
1046 }
1047
1048 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001049 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001050
Radek Krejci96e48da2020-09-04 13:18:06 +02001051 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001052 mod->version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001053 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001054 mod->version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001056 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001057 free(buf);
1058 return LY_EVALID;
1059 }
1060 free(buf);
1061
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001062 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001063 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001064 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001065 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_YANG_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001066 break;
1067 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001068 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069 return LY_EVALID;
1070 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001071 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001072 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001073
1074cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075 return ret;
1076}
1077
Michal Vaskoea5abea2018-09-18 13:10:54 +02001078/**
1079 * @brief Parse the belongs-to statement.
1080 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001081 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001082 * @param[in,out] submod Submodule to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001083 * @return LY_ERR values.
1084 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001085static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001086parse_belongsto(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001087{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001088 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001090 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001091 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001092
Michal Vasko193dacd2022-10-13 08:43:05 +02001093 if (submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001094 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001095 return LY_EVALID;
1096 }
1097
Michal Vaskoc3781c32020-10-06 14:04:08 +02001098 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001099 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001100 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001101 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 +01001102 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001103 free(buf);
1104 return LY_EVALID;
1105 }
1106 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001107
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001108 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001109 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001110 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01001111 LY_CHECK_RET(parse_text_field(ctx, submod->prefix, LY_STMT_PREFIX, 0, &submod->prefix, Y_IDENTIF_ARG, NULL,
1112 &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001114 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001115 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_BELONGS_TO, 0, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116 break;
1117 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001118 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001119 return LY_EVALID;
1120 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001121 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001122 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001123
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124 /* mandatory substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +02001125 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001126 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127 return LY_EVALID;
1128 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001129
1130cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001131 return ret;
1132}
1133
Michal Vaskoea5abea2018-09-18 13:10:54 +02001134/**
1135 * @brief Parse the revision-date statement.
1136 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001137 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001138 * @param[in,out] rev Buffer to store the parsed value in.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001139 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001140 * @return LY_ERR values.
1141 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001143parse_revisiondate(struct lysp_yang_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001145 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001146 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001147 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001148 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149
1150 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001151 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001152 return LY_EVALID;
1153 }
1154
1155 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001156 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157
1158 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001159 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160 free(buf);
1161 return LY_EVALID;
1162 }
1163
1164 /* store value and spend buf if allocated */
1165 strncpy(rev, word, word_len);
1166 free(buf);
1167
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001168 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001170 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001171 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172 break;
1173 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001174 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175 return LY_EVALID;
1176 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001177 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001179
1180cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181 return ret;
1182}
1183
Michal Vaskoea5abea2018-09-18 13:10:54 +02001184/**
1185 * @brief Parse the include statement.
1186 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001187 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001188 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001189 * @param[in,out] includes Parsed includes to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001190 * @return LY_ERR values.
1191 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001193parse_include(struct lysp_yang_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194{
Radek Krejcid33273d2018-10-25 14:55:52 +02001195 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001197 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001198 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001199 struct lysp_include *inc;
1200
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001201 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001202
1203 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001204 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001205
Michal Vasko12ef5362022-09-16 15:13:58 +02001206 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001207
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001208 /* submodules share the namespace with the module names, so there must not be
1209 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001210 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001211 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001212 return LY_EVALID;
1213 }
1214
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001215 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001216 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001217 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001218 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko72c6d642024-02-27 14:59:01 +01001219 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 +02001220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001221 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001222 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko72c6d642024-02-27 14:59:01 +01001223 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 +02001224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001225 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001226 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001228 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001229 LY_CHECK_RET(parse_ext(ctx, word, word_len, inc, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230 break;
1231 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001232 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233 return LY_EVALID;
1234 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001235 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001237
1238cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239 return ret;
1240}
1241
Michal Vaskoea5abea2018-09-18 13:10:54 +02001242/**
1243 * @brief Parse the import statement.
1244 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001245 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001246 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001247 * @param[in,out] imports Parsed imports to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001248 * @return LY_ERR values.
1249 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001250static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001251parse_import(struct lysp_yang_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001253 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001255 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001256 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 struct lysp_import *imp;
1258
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001259 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001260
1261 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001262 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001263 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001264
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001265 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001267 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01001268 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 +02001269 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001271 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001272 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko72c6d642024-02-27 14:59:01 +01001273 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 +02001274 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001275 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001276 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko72c6d642024-02-27 14:59:01 +01001277 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 +02001278 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001279 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001280 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001281 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001282 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001283 LY_CHECK_RET(parse_ext(ctx, word, word_len, imp, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001284 break;
1285 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001286 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001287 return LY_EVALID;
1288 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001289 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001290 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001291
Michal Vasko7fbc8162018-09-17 10:35:16 +02001292 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001293 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001294
Michal Vasko12ef5362022-09-16 15:13:58 +02001295cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296 return ret;
1297}
1298
Michal Vaskoea5abea2018-09-18 13:10:54 +02001299/**
1300 * @brief Parse the revision statement.
1301 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001302 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001303 * @param[in,out] revs Parsed revisions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001304 * @return LY_ERR values.
1305 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001306static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001307parse_revision(struct lysp_yang_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001308{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001309 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001311 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001312 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001313 struct lysp_revision *rev;
1314
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001315 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316
1317 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001318 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319
1320 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001321 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001322 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001323 return LY_EVALID;
1324 }
1325
Radek Krejcib7db73a2018-10-24 14:18:40 +02001326 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327 free(buf);
1328
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001329 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001331 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001332 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 +02001333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001334 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001335 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 +02001336 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001337 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001338 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 break;
1340 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001341 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001342 return LY_EVALID;
1343 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001344 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001346
1347cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 return ret;
1349}
1350
Michal Vaskoea5abea2018-09-18 13:10:54 +02001351/**
1352 * @brief Parse a generic text field that can have more instances such as base.
1353 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001354 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001355 * @param[in] parent Current statement parent.
1356 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001357 * @param[in,out] texts Parsed values to add to.
1358 * @param[in] arg Type of the expected argument.
1359 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001360 * @return LY_ERR values.
1361 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001363parse_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 +02001364 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001365{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001366 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001367 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001368 const char **item;
1369 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001370 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371
1372 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001373 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374
1375 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001376 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001377
Michal Vasko12ef5362022-09-16 15:13:58 +02001378 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001379 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001380 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001381 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001382 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 +02001383 break;
1384 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001385 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 return LY_EVALID;
1387 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001388 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001389 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001390
1391cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001392 return ret;
1393}
1394
Michal Vaskoea5abea2018-09-18 13:10:54 +02001395/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001396 * @brief Parse a generic text field that can have more instances such as base.
1397 *
1398 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001399 * @param[in] parent_stmt Type of statement stored in @p qnames.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001400 * @param[in,out] qnames Parsed qnames to add to.
1401 * @param[in] arg Type of the expected argument.
1402 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001403 * @return LY_ERR values.
1404 */
1405static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001406parse_qnames(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_qname **qnames, enum yang_arg arg,
1407 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +02001408{
1409 LY_ERR ret = LY_SUCCESS;
1410 char *buf, *word;
1411 struct lysp_qname *item;
1412 size_t word_len;
1413 enum ly_stmt kw;
1414
1415 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001416 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001417
1418 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001419 LY_CHECK_RET(get_argument(ctx, arg, &item->flags, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001420
Michal Vasko12ef5362022-09-16 15:13:58 +02001421 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001422 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001423 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001424 switch (kw) {
1425 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001426 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 +02001427 break;
1428 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001429 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001430 return LY_EVALID;
1431 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001432 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001433 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001434
1435cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001436 return ret;
1437}
1438
1439/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001440 * @brief Parse the config statement.
1441 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001442 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001443 * @param[in,out] flags Flags to add to.
1444 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001445 * @return LY_ERR values.
1446 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001447static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001448parse_config(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001450 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001451 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001452 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001453 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001454
1455 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001456 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001457 return LY_EVALID;
1458 }
1459
1460 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001461 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001462
Radek Krejcif13b87b2020-12-01 22:02:17 +01001463 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001464 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001465 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466 *flags |= LYS_CONFIG_R;
1467 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001468 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001469 free(buf);
1470 return LY_EVALID;
1471 }
1472 free(buf);
1473
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001474 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001476 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001477 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478 break;
1479 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001480 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481 return LY_EVALID;
1482 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001483 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001485
1486cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487 return ret;
1488}
1489
Michal Vaskoea5abea2018-09-18 13:10:54 +02001490/**
1491 * @brief Parse the mandatory statement.
1492 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001493 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001494 * @param[in,out] flags Flags to add to.
1495 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001496 * @return LY_ERR values.
1497 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001499parse_mandatory(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001501 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001502 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001503 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001504 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505
1506 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001507 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 return LY_EVALID;
1509 }
1510
1511 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001512 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513
Radek Krejcif13b87b2020-12-01 22:02:17 +01001514 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001516 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 *flags |= LYS_MAND_FALSE;
1518 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001519 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 free(buf);
1521 return LY_EVALID;
1522 }
1523 free(buf);
1524
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001525 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001527 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001528 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001529 break;
1530 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001531 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001532 return LY_EVALID;
1533 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001534 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001536
1537cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538 return ret;
1539}
1540
Michal Vaskoea5abea2018-09-18 13:10:54 +02001541/**
1542 * @brief Parse a restriction such as range or length.
1543 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001544 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001545 * @param[in] restr_kw Type of this particular restriction.
1546 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001547 * @return LY_ERR values.
1548 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001549static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001550parse_restr(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001552 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001554 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001555 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556
1557 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001558 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, &restr->arg.flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559
Michal Vasko193dacd2022-10-13 08:43:05 +02001560 CHECK_NONEMPTY(ctx, word_len, lyplg_ext_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001561 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001562 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001563 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001565 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001566 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 +02001567 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001568 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001569 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 +02001570 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001571 case LY_STMT_ERROR_APP_TAG:
Michal Vasko72c6d642024-02-27 14:59:01 +01001572 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 +02001573 &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001575 case LY_STMT_ERROR_MESSAGE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001576 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 +02001577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001578 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001579 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 break;
1581 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001582 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001583 return LY_EVALID;
1584 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001585 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001587
1588cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001589 return ret;
1590}
1591
Michal Vaskoea5abea2018-09-18 13:10:54 +02001592/**
1593 * @brief Parse a restriction that can have more instances such as must.
1594 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001595 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001596 * @param[in] restr_kw Type of this particular restriction.
1597 * @param[in,out] restrs Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001598 * @return LY_ERR values.
1599 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001601parse_restrs(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001602{
1603 struct lysp_restr *restr;
1604
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001605 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001606 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607}
1608
Michal Vaskoea5abea2018-09-18 13:10:54 +02001609/**
1610 * @brief Parse the status statement.
1611 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001612 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001613 * @param[in,out] flags Flags to add to.
1614 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001615 * @return LY_ERR values.
1616 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001617static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001618parse_status(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001619{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001620 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001622 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001623 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624
1625 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001626 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627 return LY_EVALID;
1628 }
1629
1630 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001631 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001632
Radek Krejcif13b87b2020-12-01 22:02:17 +01001633 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001635 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001637 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 *flags |= LYS_STATUS_OBSLT;
1639 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001640 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641 free(buf);
1642 return LY_EVALID;
1643 }
1644 free(buf);
1645
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001646 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001648 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001649 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001650 break;
1651 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001652 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653 return LY_EVALID;
1654 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001655 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001656 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001657
1658cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001659 return ret;
1660}
1661
Michal Vaskoea5abea2018-09-18 13:10:54 +02001662/**
1663 * @brief Parse the when statement.
1664 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001665 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001666 * @param[in,out] when_p When pointer to parse to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001667 * @return LY_ERR values.
1668 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001669LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001670parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001672 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001674 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001675 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001676 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001677 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001678
1679 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001680 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681 return LY_EVALID;
1682 }
1683
1684 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001685 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686
1687 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001688 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001689 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001690 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001691
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001692 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001694 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001695 ret = parse_text_field(ctx, when->dsc, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, NULL, &when->exts);
1696 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001698 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001699 ret = parse_text_field(ctx, when->ref, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, NULL, &when->exts);
1700 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001701 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001702 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001703 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 +02001704 break;
1705 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001706 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001707 ret = LY_EVALID;
1708 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001709 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001710 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001711 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001712
1713cleanup:
1714 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001715 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001716 free(when);
1717 } else {
1718 *when_p = when;
1719 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001720 return ret;
1721}
1722
Michal Vaskoea5abea2018-09-18 13:10:54 +02001723/**
1724 * @brief Parse the anydata or anyxml statement.
1725 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001726 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001727 * @param[in] any_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001728 * @param[in] parent Node parent.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001729 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001730 * @return LY_ERR values.
1731 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001732LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001733parse_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 +02001734{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001735 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001737 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001739 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740
David Sedlák60adc092019-08-06 15:57:02 +02001741 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001742 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001743
Radek Krejci39b7fc22021-02-26 23:29:18 +01001744 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001745 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001746
Michal Vasko7fbc8162018-09-17 10:35:16 +02001747 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001748 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001749 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001750
1751 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001752 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001754 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001755 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001757 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001758 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 +02001759 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001760 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001761 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001763 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001764 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001766 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001767 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001769 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001770 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 +02001771 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001772 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001773 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001775 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001776 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001778 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001779 LY_CHECK_RET(parse_ext(ctx, word, word_len, any, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001780 break;
1781 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001782 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001783 return LY_EVALID;
1784 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001785 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001786 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001787
1788cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001789 return ret;
1790}
1791
Michal Vaskoea5abea2018-09-18 13:10:54 +02001792/**
1793 * @brief Parse the value or position statement. Substatement of type enum statement.
1794 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001795 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001796 * @param[in] val_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001797 * @param[in,out] enm Structure to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001798 * @return LY_ERR values.
1799 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001800LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001801parse_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 +02001802{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001803 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001804 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001805 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001806 long long num = 0;
1807 unsigned long long unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001808 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001809
Michal Vasko193dacd2022-10-13 08:43:05 +02001810 if (enm->flags & LYS_SET_VALUE) {
1811 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001812 ret = LY_EVALID;
1813 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001815 enm->flags |= LYS_SET_VALUE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816
1817 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001818 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819
Radek Krejcid6b76452019-09-03 17:03:03 +02001820 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 +02001821 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001822 ret = LY_EVALID;
1823 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824 }
1825
1826 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001827 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001828 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001829 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001830 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001831 ret = LY_EVALID;
1832 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001833 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001834 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001835 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001836 if (unum > UINT64_C(4294967295)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001837 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001838 ret = LY_EVALID;
1839 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001840 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001841 }
1842 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001843 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001844 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001845 ret = LY_EVALID;
1846 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001847 }
1848 if (errno == ERANGE) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001849 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001850 ret = LY_EVALID;
1851 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001852 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001853 if (val_kw == LY_STMT_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001854 enm->value = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001855 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02001856 enm->value = unum;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001857 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001858
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001859 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001860 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001861 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001862 ret = parse_ext(ctx, word, word_len, enm, val_kw, 0, &enm->exts);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001863 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001864 break;
1865 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001866 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001867 ret = LY_EVALID;
1868 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001869 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001870 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871 }
Radek Krejci8b764662018-11-14 14:15:13 +01001872
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001873cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001874 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001875 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876}
1877
Michal Vaskoea5abea2018-09-18 13:10:54 +02001878/**
1879 * @brief Parse the enum or bit statement. Substatement of type statement.
1880 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001881 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001882 * @param[in] enum_kw Type of this particular keyword.
1883 * @param[in,out] enums Enums or bits to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001884 * @return LY_ERR values.
1885 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001886static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001887parse_type_enum(struct lysp_yang_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001889 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001890 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001891 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001892 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001893 struct lysp_type_enum *enm;
1894
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001895 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896
1897 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001898 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 +02001899 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001900 ret = lysp_check_enum_name((struct lysp_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001901 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001902 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001903
Michal Vasko12ef5362022-09-16 15:13:58 +02001904 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001905 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(enum_kw), enm->name);
Radek Krejci8b764662018-11-14 14:15:13 +01001906
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001907 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001909 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001910 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 +02001911 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001912 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001913 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001914 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001915 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001916 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001917 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 +02001918 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001919 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001920 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001922 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001923 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1924 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1925 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
David Sedlák9fb515f2019-07-11 10:33:58 +02001926 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001927 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001928 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1929 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1930 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001932 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001933 LY_CHECK_RET(parse_ext(ctx, word, word_len, enm, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934 break;
1935 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001936 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001937 return LY_EVALID;
1938 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001939 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001940 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001941
1942cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001943 return ret;
1944}
1945
Michal Vaskoea5abea2018-09-18 13:10:54 +02001946/**
1947 * @brief Parse the fraction-digits statement. Substatement of type statement.
1948 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001949 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001950 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001951 * @return LY_ERR values.
1952 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001954parse_type_fracdigits(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001955{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001956 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001957 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001958 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001959 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001960 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001961
Michal Vasko193dacd2022-10-13 08:43:05 +02001962 if (type->fraction_digits) {
David Sedlákb3077192019-06-19 10:55:37 +02001963 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001964 return LY_EVALID;
1965 }
1966
1967 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001968 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969
1970 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001971 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001972 ret = LY_EVALID;
1973 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001974 }
1975
1976 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001977 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001979 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001980 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001981 ret = LY_EVALID;
1982 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001983 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001984 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001985 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001986 ret = LY_EVALID;
1987 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001988 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001989 type->fraction_digits = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001991 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001993 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001994 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 +02001995 break;
1996 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001997 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001998 ret = LY_EVALID;
1999 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002000 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002001 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002002 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002003
2004cleanup:
2005 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006 return ret;
2007}
2008
Michal Vaskoea5abea2018-09-18 13:10:54 +02002009/**
2010 * @brief Parse the require-instance statement. Substatement of type statement.
2011 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002012 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002013 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002014 * @return LY_ERR values.
2015 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002016static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002017parse_type_reqinstance(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002019 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002020 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002021 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002022 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023
Michal Vasko193dacd2022-10-13 08:43:05 +02002024 if (type->flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002025 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026 return LY_EVALID;
2027 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002028 type->flags |= LYS_SET_REQINST;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029
2030 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002031 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002032
Radek Krejcif13b87b2020-12-01 22:02:17 +01002033 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002034 type->require_instance = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002035 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002036 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002037 ret = LY_EVALID;
2038 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002040
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002041 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002043 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002044 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 +02002045 break;
2046 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002047 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002048 ret = LY_EVALID;
2049 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002050 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002051 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002053
2054cleanup:
2055 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 return ret;
2057}
2058
Michal Vaskoea5abea2018-09-18 13:10:54 +02002059/**
2060 * @brief Parse the modifier statement. Substatement of type pattern statement.
2061 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002062 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002063 * @param[in,out] restr Restriction to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002064 * @return LY_ERR values.
2065 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002067parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002069 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002070 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002071 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002072 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002073
Michal Vasko193dacd2022-10-13 08:43:05 +02002074 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002075 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076 return LY_EVALID;
2077 }
2078
2079 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002080 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081
Radek Krejcif13b87b2020-12-01 22:02:17 +01002082 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002083 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002084 ret = LY_EVALID;
2085 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002087
2088 /* replace the value in the dictionary */
Michal Vasko193dacd2022-10-13 08:43:05 +02002089 buf = malloc(strlen(restr->arg.str) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002090 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02002091 strcpy(buf, restr->arg.str);
2092 lydict_remove(PARSER_CTX(ctx), restr->arg.str);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002093
Radek Krejcif13b87b2020-12-01 22:02:17 +01002094 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2095 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko183ea332022-11-08 10:50:03 +01002096 ret = lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002097 buf = NULL;
Michal Vasko183ea332022-11-08 10:50:03 +01002098 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002099
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002100 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002101 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002102 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002103 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 +02002104 break;
2105 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002106 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002107 ret = LY_EVALID;
2108 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002110 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002111 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002112
2113cleanup:
2114 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 return ret;
2116}
2117
Michal Vaskoea5abea2018-09-18 13:10:54 +02002118/**
2119 * @brief Parse the pattern statement. Substatement of type statement.
2120 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002121 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002122 * @param[in,out] patterns Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002123 * @return LY_ERR values.
2124 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002126parse_type_pattern(struct lysp_yang_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002128 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002130 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002131 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132 struct lysp_restr *restr;
2133
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002134 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135
2136 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01002137 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, &restr->arg.flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002138
2139 /* add special meaning first byte */
2140 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002141 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002142 word = buf;
2143 } else {
2144 buf = malloc(word_len + 2);
2145 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002146 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002147 if (word_len) {
2148 memmove(buf + 1, word, word_len);
2149 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002150 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002151 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002152 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002153 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002155 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002157 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002158 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 +02002159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002160 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002161 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 +02002162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002163 case LY_STMT_ERROR_APP_TAG:
Michal Vasko72c6d642024-02-27 14:59:01 +01002164 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 +02002165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002166 case LY_STMT_ERROR_MESSAGE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002167 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 +02002168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002169 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002170 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko193dacd2022-10-13 08:43:05 +02002171 LY_CHECK_RET(parse_type_pattern_modifier(ctx, restr));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002173 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002174 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175 break;
2176 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002177 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002178 return LY_EVALID;
2179 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002180 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002182
2183cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002184 return ret;
2185}
2186
Michal Vaskoea5abea2018-09-18 13:10:54 +02002187/**
2188 * @brief Parse the type statement.
2189 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002190 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002191 * @param[in,out] type Type to wrote to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002192 * @return LY_ERR values.
2193 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002195parse_type(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002196{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002197 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002198 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002199 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002200 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002201 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202 struct lysp_type *nest_type;
2203
2204 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002205 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002206 return LY_EVALID;
2207 }
2208
2209 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002210 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002211 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002213 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002214 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002215
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002216 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002217 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002218 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002219 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 +01002220 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002222 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002223 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002224 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002226 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002227 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002228 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002230 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002231 LY_CHECK_RET(parse_type_fracdigits(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002232 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002234 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002236 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 return LY_EVALID;
2238 }
2239 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002240 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241
Radek Krejci33090f92020-12-17 20:12:46 +01002242 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002243 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002244 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002245 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002246 if (type->path) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002247 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002248 return LY_EVALID;
2249 }
2250
aPiecek4bb1e372021-05-07 11:01:00 +02002251 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2252 * corresponding structure, so in case of failure, the lysp_module_free function will take
2253 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2254 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2255 */
Michal Vasko72c6d642024-02-27 14:59:01 +01002256 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 +02002257 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002258 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002259 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002260 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002261 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002262 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002263 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002265 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002266 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002267 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002269 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002270 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002271 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002272 return LY_EVALID;
2273 }
2274 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002275 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276
Radek Krejci33090f92020-12-17 20:12:46 +01002277 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002278 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002280 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002281 LY_CHECK_RET(parse_type_reqinstance(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002282 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002284 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002285 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002286 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002287 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002289 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002290 LY_CHECK_RET(parse_ext(ctx, word, word_len, type, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002291 break;
2292 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002293 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002294 return LY_EVALID;
2295 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002296 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002297 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002298
2299cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002300 return ret;
2301}
2302
Michal Vaskoea5abea2018-09-18 13:10:54 +02002303/**
2304 * @brief Parse the leaf statement.
2305 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002306 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002307 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002308 * @return LY_ERR values.
2309 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002310LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002311parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002313 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002315 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002316 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317 struct lysp_node_leaf *leaf;
2318
David Sedlák60adc092019-08-06 15:57:02 +02002319 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002320 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002321 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002322 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002323
Michal Vasko7fbc8162018-09-17 10:35:16 +02002324 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002325 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002326 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002327
2328 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002329 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002331 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002332 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002334 case LY_STMT_DEFAULT:
Michal Vasko72c6d642024-02-27 14:59:01 +01002335 LY_CHECK_RET(parse_text_field(ctx, &leaf->dflt, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG,
2336 &leaf->dflt.flags, &leaf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002337 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002338 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002339 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002340 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 +02002341 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002342 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002343 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002345 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002346 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002348 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002349 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002351 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002352 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 +02002353 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002354 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002355 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002357 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002358 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002360 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002361 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 +02002362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002363 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002364 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002366 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002367 LY_CHECK_RET(parse_ext(ctx, word, word_len, leaf, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368 break;
2369 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002370 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002371 return LY_EVALID;
2372 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002373 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002374 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002375
Michal Vasko7fbc8162018-09-17 10:35:16 +02002376 /* mandatory substatements */
2377 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002378 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002379 return LY_EVALID;
2380 }
2381
Michal Vasko12ef5362022-09-16 15:13:58 +02002382cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383 return ret;
2384}
2385
Michal Vaskoea5abea2018-09-18 13:10:54 +02002386/**
2387 * @brief Parse the max-elements statement.
2388 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002389 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002390 * @param[in,out] max Value to write to.
2391 * @param[in,out] flags Flags to write to.
2392 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002393 * @return LY_ERR values.
2394 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002395LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002396parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002398 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002399 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002400 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002401 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002402 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403
2404 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002405 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 return LY_EVALID;
2407 }
2408 *flags |= LYS_SET_MAX;
2409
2410 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002411 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412
2413 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002414 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002415 ret = LY_EVALID;
2416 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002417 }
2418
Radek Krejci7f9b6512019-09-18 13:11:09 +02002419 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002421 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002423 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002424 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002425 ret = LY_EVALID;
2426 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427 }
2428 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002429 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002430 ret = LY_EVALID;
2431 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432 }
2433
2434 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002435 } else {
2436 /* unbounded */
2437 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002439
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002440 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002441 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002442 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002443 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 +02002444 break;
2445 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002446 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002447 ret = LY_EVALID;
2448 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002450 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002451 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002452
2453cleanup:
2454 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455 return ret;
2456}
2457
Michal Vaskoea5abea2018-09-18 13:10:54 +02002458/**
2459 * @brief Parse the min-elements statement.
2460 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002461 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002462 * @param[in,out] min Value to write to.
2463 * @param[in,out] flags Flags to write to.
2464 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002465 * @return LY_ERR values.
2466 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002467LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002468parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002470 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002471 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002472 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002473 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002474 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475
2476 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002477 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 return LY_EVALID;
2479 }
2480 *flags |= LYS_SET_MIN;
2481
2482 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002483 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002484
2485 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002486 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002487 ret = LY_EVALID;
2488 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002489 }
2490
2491 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002492 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002494 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002495 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002496 ret = LY_EVALID;
2497 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002498 }
2499 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002500 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002501 ret = LY_EVALID;
2502 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 }
2504 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002506 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002508 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002509 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 +02002510 break;
2511 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002512 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002513 ret = LY_EVALID;
2514 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002515 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002516 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002518
2519cleanup:
2520 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002521 return ret;
2522}
2523
Michal Vaskoea5abea2018-09-18 13:10:54 +02002524/**
2525 * @brief Parse the ordered-by statement.
2526 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002527 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002528 * @param[in,out] llist List or leaf-list to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002529 * @return LY_ERR values.
2530 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002532parse_orderedby(struct lysp_yang_ctx *ctx, struct lysp_node *llist)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002534 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002535 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002536 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002537 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538
Michal Vasko193dacd2022-10-13 08:43:05 +02002539 if (llist->flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002540 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541 return LY_EVALID;
2542 }
2543
2544 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002545 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546
Radek Krejcif13b87b2020-12-01 22:02:17 +01002547 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002548 llist->flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002549 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002550 llist->flags |= LYS_ORDBY_USER;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002551 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002552 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002553 ret = LY_EVALID;
2554 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002557 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002559 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002560 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 +02002561 break;
2562 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002563 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002564 ret = LY_EVALID;
2565 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002566 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002567 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002568 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002569
2570cleanup:
2571 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572 return ret;
2573}
2574
Michal Vaskoea5abea2018-09-18 13:10:54 +02002575/**
2576 * @brief Parse the leaf-list statement.
2577 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002578 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002579 * @param[in,out] siblings Siblings to add to.
2580 *
2581 * @return LY_ERR values.
2582 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002583LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002584parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002585{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002586 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002588 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002589 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 struct lysp_node_leaflist *llist;
2591
David Sedlák60adc092019-08-06 15:57:02 +02002592 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002593 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002595 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002596
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002598 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002599 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600
2601 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002602 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002604 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002605 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002607 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002608 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002609 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002611 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002612 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 +02002613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002614 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002615 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002617 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002618 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002619 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002620 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002621 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002623 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002624 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002625 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002626 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02002627 LY_CHECK_RET(parse_orderedby(ctx, &llist->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002628 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002629 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002630 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 +02002631 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002632 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002633 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002634 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002635 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002636 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002638 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002639 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 +02002640 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002641 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002642 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002643 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002644 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002645 LY_CHECK_RET(parse_ext(ctx, word, word_len, llist, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646 break;
2647 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002648 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649 return LY_EVALID;
2650 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002651 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002653
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654 /* mandatory substatements */
2655 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002656 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657 return LY_EVALID;
2658 }
2659
Michal Vasko12ef5362022-09-16 15:13:58 +02002660cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 return ret;
2662}
2663
Michal Vaskoea5abea2018-09-18 13:10:54 +02002664/**
2665 * @brief Parse the refine statement.
2666 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002667 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002668 * @param[in,out] refines Refines to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002669 * @return LY_ERR values.
2670 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002672parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002674 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002676 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002677 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 struct lysp_refine *rf;
2679
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002680 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681
2682 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002683 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002684 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002685 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002686
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002687 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002689 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002690 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002692 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002693 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002695 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002696 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 +02002697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002698 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002699 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002700 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002701 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002702 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002703 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002705 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002706 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002707 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002708 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002709 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002711 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002712 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002713 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002714 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002715 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 +02002716 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002717 case LY_STMT_PRESENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002718 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 +02002719 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002720 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002721 LY_CHECK_RET(parse_ext(ctx, word, word_len, rf, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002722 break;
2723 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002724 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002725 return LY_EVALID;
2726 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002727 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002729
2730cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731 return ret;
2732}
2733
Michal Vaskoea5abea2018-09-18 13:10:54 +02002734/**
2735 * @brief Parse the typedef statement.
2736 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002737 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002738 * @param[in,out] typedefs Typedefs to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002739 * @return LY_ERR values.
2740 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002742parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002743{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002744 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002746 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002747 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002748 struct lysp_tpdf *tpdf;
2749
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002750 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751
2752 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002753 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002754 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755
2756 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002757 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002759 case LY_STMT_DEFAULT:
Michal Vasko72c6d642024-02-27 14:59:01 +01002760 LY_CHECK_RET(parse_text_field(ctx, &tpdf->dflt, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG,
2761 &tpdf->dflt.flags, &tpdf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002762 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002764 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002765 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 +02002766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002767 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002768 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 +02002769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002770 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002771 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002773 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002774 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002776 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002777 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 +02002778 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002779 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002780 LY_CHECK_RET(parse_ext(ctx, word, word_len, tpdf, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002781 break;
2782 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002783 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784 return LY_EVALID;
2785 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002786 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002788
Michal Vasko7fbc8162018-09-17 10:35:16 +02002789 /* mandatory substatements */
2790 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002791 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792 return LY_EVALID;
2793 }
2794
Radek Krejcibbe09a92018-11-08 09:36:54 +01002795 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002796 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002797 assert(ctx->main_ctx);
2798 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002799 }
2800
Michal Vasko12ef5362022-09-16 15:13:58 +02002801cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802 return ret;
2803}
2804
Michal Vaskoea5abea2018-09-18 13:10:54 +02002805/**
2806 * @brief Parse the input or output statement.
2807 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002808 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002809 * @param[in] kw Type of this particular keyword
2810 * @param[in,out] inout_p Input/output pointer to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002811 * @return LY_ERR values.
2812 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002813static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002814parse_inout(struct lysp_yang_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002815 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002816{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002817 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002819 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002820 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002821 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002822
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002823 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002824 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002825 return LY_EVALID;
2826 }
2827
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002828 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002829 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2830 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002831 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002832
2833 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002834 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002835 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002836 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002837 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002838 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002839 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002840 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002841 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002842 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002843 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002845 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002846 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002847 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002848 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002849 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002850 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002851 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002852 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002854 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002855 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002857 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002858 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002860 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002861 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002863 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002864 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002865 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002866 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002867 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002868 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002870 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002871 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_p, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002872 break;
2873 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002874 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002875 return LY_EVALID;
2876 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002877 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002878 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002879
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002880cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881 return ret;
2882}
2883
Michal Vaskoea5abea2018-09-18 13:10:54 +02002884/**
2885 * @brief Parse the action statement.
2886 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002887 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002888 * @param[in,out] actions Actions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002889 * @return LY_ERR values.
2890 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002891LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002892parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002893{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002894 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002896 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002897 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002898 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002899
Radek Krejci2a9fc652021-01-22 17:44:34 +01002900 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901
2902 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002903 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002904 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002905 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002906 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002908 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002910 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002911 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 +02002912 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002913 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002914 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002916 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002917 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 +02002918 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002919 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002920 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 break;
2922
Radek Krejcid6b76452019-09-03 17:03:03 +02002923 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002924 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002926 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002927 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 break;
2929
Radek Krejcid6b76452019-09-03 17:03:03 +02002930 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002931 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002933 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002934 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002936 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002937 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 +02002938 break;
2939 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002940 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941 return LY_EVALID;
2942 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002943 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002945
2946 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2947 if (!act->input.nodetype) {
2948 act->input.nodetype = LYS_INPUT;
2949 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002950 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002951 }
2952 if (!act->output.nodetype) {
2953 act->output.nodetype = LYS_OUTPUT;
2954 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002955 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002956 }
2957
Michal Vasko12ef5362022-09-16 15:13:58 +02002958cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002959 return ret;
2960}
2961
Michal Vaskoea5abea2018-09-18 13:10:54 +02002962/**
2963 * @brief Parse the notification statement.
2964 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002965 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002966 * @param[in,out] notifs Notifications to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002967 * @return LY_ERR values.
2968 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002969LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002970parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002971{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002972 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002974 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002975 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002976 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977
Radek Krejci2a9fc652021-01-22 17:44:34 +01002978 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002979
2980 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002981 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002982 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002983 notif->nodetype = LYS_NOTIF;
2984 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002986 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002988 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002989 LY_CHECK_RET(parse_text_field(ctx, notif->dsc, LY_STMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, NULL,
2990 &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002992 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002993 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002995 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002996 LY_CHECK_RET(parse_text_field(ctx, notif->ref, LY_STMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, NULL,
2997 &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002999 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003000 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 break;
3002
Radek Krejcid6b76452019-09-03 17:03:03 +02003003 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003004 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02003005 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003006 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003007 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003008 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003009 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01003010 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003011 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003012 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003013 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003015 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003016 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003018 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003019 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003021 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003022 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003024 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003025 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026 break;
3027
Radek Krejcid6b76452019-09-03 17:03:03 +02003028 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003029 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003030 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003031 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003032 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003033 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003034 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003035 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003036 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003037 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003038 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003039 LY_CHECK_RET(parse_ext(ctx, word, word_len, notif, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040 break;
3041 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003042 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043 return LY_EVALID;
3044 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003045 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003046 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003047
Michal Vasko12ef5362022-09-16 15:13:58 +02003048cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 return ret;
3050}
3051
Michal Vaskoea5abea2018-09-18 13:10:54 +02003052/**
3053 * @brief Parse the grouping statement.
3054 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003055 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003056 * @param[in,out] groupings Groupings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003057 * @return LY_ERR values.
3058 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003059LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003060parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003062 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003064 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003065 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003066 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003067
Radek Krejci2a9fc652021-01-22 17:44:34 +01003068 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069
3070 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003071 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003072 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003073 grp->nodetype = LYS_GROUPING;
3074 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003076 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003078 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003079 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 +02003080 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003081 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003082 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 +02003083 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003084 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003085 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 break;
3087
Radek Krejcid6b76452019-09-03 17:03:03 +02003088 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003089 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003090 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003091 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003092 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003094 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003095 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003097 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003098 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003100 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003101 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003103 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003104 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003106 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003107 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003109 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003110 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003111 break;
3112
Radek Krejcid6b76452019-09-03 17:03:03 +02003113 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003114 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003116 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003117 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003118 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003120 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003121 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003123 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003124 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003125 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003127 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003128 LY_CHECK_RET(parse_ext(ctx, word, word_len, grp, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 break;
3130 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003131 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132 return LY_EVALID;
3133 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003134 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003136
aPiecek63e080d2021-06-29 13:53:28 +02003137 /* store data for collision check */
3138 if (parent) {
3139 assert(ctx->main_ctx);
3140 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3141 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003142
Michal Vasko12ef5362022-09-16 15:13:58 +02003143cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144 return ret;
3145}
3146
Michal Vaskoea5abea2018-09-18 13:10:54 +02003147/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003148 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003149 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003150 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003151 * @param[in,out] augments Augments to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003152 * @return LY_ERR values.
3153 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003154LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003155parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003156{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003157 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003159 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003160 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003161 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162
Radek Krejci2a9fc652021-01-22 17:44:34 +01003163 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164
3165 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003166 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003167 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003168 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003169 aug->nodetype = LYS_AUGMENT;
3170 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003172 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003174 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003175 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 +02003176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003177 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003178 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003180 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003181 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 +02003182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003183 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003184 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003186 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003187 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 break;
3189
Radek Krejcid6b76452019-09-03 17:03:03 +02003190 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003191 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003192 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003193 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003194 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003196 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003197 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003199 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003200 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003202 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003203 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003205 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003206 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003208 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003209 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003211 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003212 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003214 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003215 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216 break;
3217
Radek Krejcid6b76452019-09-03 17:03:03 +02003218 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003219 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003220 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003222 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003223 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003224 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003226 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003227 LY_CHECK_RET(parse_ext(ctx, word, word_len, aug, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 break;
3229 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003230 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231 return LY_EVALID;
3232 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003233 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003234 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003235
Michal Vasko12ef5362022-09-16 15:13:58 +02003236cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 return ret;
3238}
3239
Michal Vaskoea5abea2018-09-18 13:10:54 +02003240/**
3241 * @brief Parse the uses statement.
3242 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003243 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003244 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003245 * @return LY_ERR values.
3246 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003247LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003248parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003250 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003252 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003253 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 struct lysp_node_uses *uses;
3255
David Sedlák60adc092019-08-06 15:57:02 +02003256 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003257 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003258 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003259 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260
Michal Vasko7fbc8162018-09-17 10:35:16 +02003261 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003262 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003263 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264
3265 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003266 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003268 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003269 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 +02003270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003271 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003272 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003274 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003275 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 +02003276 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003277 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003278 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003280 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003281 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003282 break;
3283
Radek Krejcid6b76452019-09-03 17:03:03 +02003284 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003285 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003287 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003288 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003289 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003290 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003291 LY_CHECK_RET(parse_ext(ctx, word, word_len, uses, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292 break;
3293 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003294 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295 return LY_EVALID;
3296 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003297 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003298 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003299
Michal Vasko12ef5362022-09-16 15:13:58 +02003300cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 return ret;
3302}
3303
Michal Vaskoea5abea2018-09-18 13:10:54 +02003304/**
3305 * @brief Parse the case statement.
3306 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003307 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003308 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003309 * @return LY_ERR values.
3310 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003311LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003312parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003314 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003316 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003317 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 struct lysp_node_case *cas;
3319
David Sedlák60adc092019-08-06 15:57:02 +02003320 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003321 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003323 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003324
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003326 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003327 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003328
3329 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003330 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003332 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003333 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 +02003334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003335 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003336 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003338 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003339 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 +02003340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003341 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003342 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003344 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003345 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 break;
3347
Radek Krejcid6b76452019-09-03 17:03:03 +02003348 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003349 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003350 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003351 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003352 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003354 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003355 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003357 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003358 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003360 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003361 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003363 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003364 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003366 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003367 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003369 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003370 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003372 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003373 LY_CHECK_RET(parse_ext(ctx, word, word_len, cas, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003374 break;
3375 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003376 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377 return LY_EVALID;
3378 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003379 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003381
3382cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383 return ret;
3384}
3385
Michal Vaskoea5abea2018-09-18 13:10:54 +02003386/**
3387 * @brief Parse the choice statement.
3388 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003389 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003390 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003391 * @return LY_ERR values.
3392 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003393LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003394parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003396 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003397 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003398 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003399 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003400 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401
David Sedlák60adc092019-08-06 15:57:02 +02003402 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003403 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003404 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003405 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406
Michal Vasko7fbc8162018-09-17 10:35:16 +02003407 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003408 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003409 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410
3411 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003412 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003414 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003415 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003417 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003418 LY_CHECK_RET(parse_text_field(ctx, choice->dsc, LY_STMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, NULL,
3419 &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003421 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003422 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003424 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003425 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003427 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003428 LY_CHECK_RET(parse_text_field(ctx, choice->ref, LY_STMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, NULL,
3429 &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003432 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003434 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003435 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003437 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003438 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 +01003439 &choice->dflt.flags, &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003440 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 break;
3442
Radek Krejcid6b76452019-09-03 17:03:03 +02003443 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003444 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003445 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003446 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003447 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003449 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003450 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003452 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003453 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003454 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003456 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003457 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003459 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003460 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003462 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003463 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003465 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003466 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003468 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003469 LY_CHECK_RET(parse_ext(ctx, word, word_len, choice, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003470 break;
3471 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003472 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473 return LY_EVALID;
3474 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003475 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003477
3478cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003479 return ret;
3480}
3481
Michal Vaskoea5abea2018-09-18 13:10:54 +02003482/**
3483 * @brief Parse the container statement.
3484 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003485 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003486 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003487 * @return LY_ERR values.
3488 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003489LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003490parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491{
3492 LY_ERR ret = 0;
3493 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003494 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003495 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 struct lysp_node_container *cont;
3497
David Sedlák60adc092019-08-06 15:57:02 +02003498 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003499 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003501 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003502
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003504 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003505 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003506
3507 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003508 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003510 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003511 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003513 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003514 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 +02003515 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003516 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003517 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003519 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003520 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 +02003521 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003522 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003523 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003524 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003525 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003526 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003528 case LY_STMT_PRESENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003529 LY_CHECK_RET(parse_text_field(ctx, cont->presence, LY_STMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, NULL,
3530 &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 break;
3532
Radek Krejcid6b76452019-09-03 17:03:03 +02003533 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003534 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003535 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003536 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003537 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003539 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003540 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003542 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003543 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003545 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003546 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003548 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003549 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003551 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003552 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003554 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003555 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556 break;
3557
Radek Krejcid6b76452019-09-03 17:03:03 +02003558 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003559 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003561 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003562 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003564 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003565 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003566 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003568 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003569 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003571 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003572 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003573 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003575 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003576 LY_CHECK_RET(parse_ext(ctx, word, word_len, cont, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577 break;
3578 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003579 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580 return LY_EVALID;
3581 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003582 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003583 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003584
Michal Vasko12ef5362022-09-16 15:13:58 +02003585cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 return ret;
3587}
3588
Michal Vaskoea5abea2018-09-18 13:10:54 +02003589/**
3590 * @brief Parse the list statement.
3591 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003592 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003593 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003594 * @return LY_ERR values.
3595 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003596LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003597parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003599 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003601 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003602 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003603 struct lysp_node_list *list;
3604
David Sedlák60adc092019-08-06 15:57:02 +02003605 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003606 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003607 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003608 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609
Michal Vasko7fbc8162018-09-17 10:35:16 +02003610 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003611 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003612 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613
3614 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003615 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003616 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003617 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003618 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003619 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003620 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003621 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 +02003622 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003623 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003624 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003626 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003627 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 +02003628 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003629 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003630 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003632 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003633 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003634 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003635 case LY_STMT_KEY:
Michal Vasko72c6d642024-02-27 14:59:01 +01003636 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 +02003637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003638 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003639 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003641 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003642 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003644 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003645 LY_CHECK_RET(parse_orderedby(ctx, &list->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003647 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003648 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 break;
3650
Radek Krejcid6b76452019-09-03 17:03:03 +02003651 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003652 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003653 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003654 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003655 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003656 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003657 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003658 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003659 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003660 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003661 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003662 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003663 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003664 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003666 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003667 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003669 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003670 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003672 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003673 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674 break;
3675
Radek Krejcid6b76452019-09-03 17:03:03 +02003676 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003677 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003679 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003680 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003682 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003683 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003684 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003686 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003687 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003689 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003690 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003691 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003693 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003694 LY_CHECK_RET(parse_ext(ctx, word, word_len, list, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003695 break;
3696 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003697 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003698 return LY_EVALID;
3699 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003700 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003702
Michal Vasko12ef5362022-09-16 15:13:58 +02003703cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704 return ret;
3705}
3706
Michal Vaskoea5abea2018-09-18 13:10:54 +02003707/**
3708 * @brief Parse the yin-element statement.
3709 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003710 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003711 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003712 * @return LY_ERR values.
3713 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003715parse_yinelement(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003716{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003717 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003718 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003719 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003720 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003721
Michal Vasko193dacd2022-10-13 08:43:05 +02003722 if (ext->flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003723 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724 return LY_EVALID;
3725 }
3726
3727 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003728 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003729
Radek Krejcif13b87b2020-12-01 22:02:17 +01003730 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003731 ext->flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003732 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003733 ext->flags |= LYS_YINELEM_FALSE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003735 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736 free(buf);
3737 return LY_EVALID;
3738 }
3739 free(buf);
3740
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003741 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003743 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003744 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_YIN_ELEMENT, 0, &ext->exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003745 LY_CHECK_RET(ret);
3746 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003748 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003749 return LY_EVALID;
3750 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003751 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003752 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003753
3754cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755 return ret;
3756}
3757
Michal Vaskoea5abea2018-09-18 13:10:54 +02003758/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003759 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003760 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003761 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003762 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003763 * @return LY_ERR values.
3764 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003766parse_argument(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003767{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003768 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003770 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003771 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772
Michal Vasko193dacd2022-10-13 08:43:05 +02003773 if (ext->argname) {
David Sedlákb3077192019-06-19 10:55:37 +02003774 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 return LY_EVALID;
3776 }
3777
3778 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003779 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko193dacd2022-10-13 08:43:05 +02003780 INSERT_WORD_GOTO(ctx, buf, ext->argname, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003782 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003784 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003785 LY_CHECK_RET(parse_yinelement(ctx, ext));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003787 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003788 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_ARGUMENT, 0, &ext->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003789 break;
3790 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003791 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003792 return LY_EVALID;
3793 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003794 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003796
3797cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798 return ret;
3799}
3800
Michal Vaskoea5abea2018-09-18 13:10:54 +02003801/**
3802 * @brief Parse the extension statement.
3803 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003804 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003805 * @param[in,out] extensions Extensions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003806 * @return LY_ERR values.
3807 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003809parse_extension(struct lysp_yang_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003810{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003811 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003813 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003814 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003815 struct lysp_ext *ex;
3816
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003817 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003818
3819 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003820 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003821 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003822
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003823 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003824 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003825 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003826 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 +02003827 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003828 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003829 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 +02003830 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003831 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003832 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003833 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003834 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003835 LY_CHECK_RET(parse_argument(ctx, ex));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003836 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003837 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003838 LY_CHECK_RET(parse_ext(ctx, word, word_len, ex, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003839 break;
3840 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003841 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003842 return LY_EVALID;
3843 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003844 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003846
3847cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 return ret;
3849}
3850
Michal Vaskoea5abea2018-09-18 13:10:54 +02003851/**
3852 * @brief Parse the deviate statement.
3853 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003854 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003855 * @param[in,out] deviates Deviates to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003856 * @return LY_ERR values.
3857 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003858LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003859parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003860{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003861 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003862 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003863 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003864 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003865 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003866 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003867 struct lysp_deviate_add *d_add = NULL;
3868 struct lysp_deviate_rpl *d_rpl = NULL;
3869 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003870 const char **d_units = NULL;
3871 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003872 struct lysp_restr **d_musts = NULL;
3873 uint16_t *d_flags = 0;
3874 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003875
3876 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003877 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003878
Radek Krejcif13b87b2020-12-01 22:02:17 +01003879 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003881 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003882 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003883 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003885 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003886 dev_mod = LYS_DEV_DELETE;
3887 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003888 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003889 ret = LY_EVALID;
3890 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003891 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892
3893 /* create structure */
3894 switch (dev_mod) {
3895 case LYS_DEV_NOT_SUPPORTED:
3896 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003897 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 break;
3899 case LYS_DEV_ADD:
3900 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003901 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 d = (struct lysp_deviate *)d_add;
3903 d_units = &d_add->units;
3904 d_uniques = &d_add->uniques;
3905 d_dflts = &d_add->dflts;
3906 d_musts = &d_add->musts;
3907 d_flags = &d_add->flags;
3908 d_min = &d_add->min;
3909 d_max = &d_add->max;
3910 break;
3911 case LYS_DEV_REPLACE:
3912 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003913 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914 d = (struct lysp_deviate *)d_rpl;
3915 d_units = &d_rpl->units;
3916 d_flags = &d_rpl->flags;
3917 d_min = &d_rpl->min;
3918 d_max = &d_rpl->max;
3919 break;
3920 case LYS_DEV_DELETE:
3921 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003922 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003923 d = (struct lysp_deviate *)d_del;
3924 d_units = &d_del->units;
3925 d_uniques = &d_del->uniques;
3926 d_dflts = &d_del->dflts;
3927 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928 break;
3929 default:
3930 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003931 LOGINT(PARSER_CTX(ctx));
3932 ret = LY_EINT;
3933 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934 }
3935 d->mod = dev_mod;
3936
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003937 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003939 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 switch (dev_mod) {
3941 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003942 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003943 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003944 ret = LY_EVALID;
3945 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003947 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003948 break;
3949 }
3950 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003951 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003952 switch (dev_mod) {
3953 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02003954 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003955 ret = LY_EVALID;
3956 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003957 case LYS_DEV_REPLACE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003958 ret = parse_text_field(ctx, &d_rpl->dflt, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG,
3959 &d_rpl->dflt.flags, &d->exts);
Michal Vasko193dacd2022-10-13 08:43:05 +02003960 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003961 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962 break;
3963 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003964 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 +02003965 break;
3966 }
3967 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003968 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969 switch (dev_mod) {
3970 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003971 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003972 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003973 ret = LY_EVALID;
3974 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003975 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003976 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003977 break;
3978 }
3979 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003980 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981 switch (dev_mod) {
3982 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003983 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003984 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003985 ret = LY_EVALID;
3986 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003988 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003989 break;
3990 }
3991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003992 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 switch (dev_mod) {
3994 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003995 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003996 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003997 ret = LY_EVALID;
3998 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003999 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004000 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001 break;
4002 }
4003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004004 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004005 switch (dev_mod) {
4006 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004007 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004008 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004009 ret = LY_EVALID;
4010 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004012 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 break;
4014 }
4015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004016 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017 switch (dev_mod) {
4018 case LYS_DEV_NOT_SUPPORTED:
4019 case LYS_DEV_ADD:
4020 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004021 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004022 ret = LY_EVALID;
4023 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004025 if (d_rpl->type) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004026 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004027 ret = LY_EVALID;
4028 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004029 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004030 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004031 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4032 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033 break;
4034 }
4035 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004036 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004037 switch (dev_mod) {
4038 case LYS_DEV_NOT_SUPPORTED:
4039 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004040 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004041 ret = LY_EVALID;
4042 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004044 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 +02004045 break;
4046 }
4047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004048 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004049 switch (dev_mod) {
4050 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02004051 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004052 ret = LY_EVALID;
4053 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004054 default:
Michal Vasko72c6d642024-02-27 14:59:01 +01004055 ret = parse_text_field(ctx, *d_units, LY_STMT_UNITS, 0, d_units, Y_STR_ARG, NULL, &d->exts);
4056 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 break;
4058 }
4059 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004060 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004061 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 +02004062 break;
4063 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004064 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004065 ret = LY_EVALID;
4066 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004067 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004068 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004069 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004070
4071cleanup:
4072 free(buf);
4073 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004074 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004075 free(d);
4076 } else {
4077 /* insert into siblings */
4078 LY_LIST_INSERT(deviates, d, next);
4079 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004080 return ret;
4081}
4082
Michal Vaskoea5abea2018-09-18 13:10:54 +02004083/**
4084 * @brief Parse the deviation statement.
4085 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004086 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004087 * @param[in,out] deviations Deviations to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004088 * @return LY_ERR values.
4089 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004090LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004091parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004092{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004093 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004094 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004095 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004096 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004097 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004098 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004099
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004100 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004101
4102 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004103 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004104 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004105 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004107 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004108 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004109 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004110 ret = parse_text_field(ctx, dev->dsc, LY_STMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, NULL, &dev->exts);
4111 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004113 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004114 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004116 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004117 ret = parse_text_field(ctx, dev->ref, LY_STMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, NULL, &dev->exts);
4118 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004119 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004120 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004121 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 +02004122 break;
4123 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004124 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004125 ret = LY_EVALID;
4126 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004127 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004128 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004129 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004130
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131 /* mandatory substatements */
4132 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004133 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004134 ret = LY_EVALID;
4135 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004136 }
4137
Michal Vasko12ef5362022-09-16 15:13:58 +02004138cleanup:
4139 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004140 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004141 LY_ARRAY_DECREMENT_FREE(*deviations);
4142 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004143 return ret;
4144}
4145
Michal Vaskoea5abea2018-09-18 13:10:54 +02004146/**
4147 * @brief Parse the feature statement.
4148 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004149 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004150 * @param[in,out] features Features to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004151 * @return LY_ERR values.
4152 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004153LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004154parse_feature(struct lysp_yang_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004156 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004157 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004158 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004159 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004160 struct lysp_feature *feat;
4161
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004162 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163
4164 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004165 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004166 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004167
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004168 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004170 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004171 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 +02004172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004173 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004174 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004176 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004177 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 +02004178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004179 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004180 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004182 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004183 LY_CHECK_RET(parse_ext(ctx, word, word_len, feat, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 break;
4185 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004186 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004187 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004188 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004189 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004191
4192cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193 return ret;
4194}
4195
Michal Vaskoea5abea2018-09-18 13:10:54 +02004196/**
4197 * @brief Parse the identity statement.
4198 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004199 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004200 * @param[in,out] identities Identities to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004201 * @return LY_ERR values.
4202 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004203LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004204parse_identity(struct lysp_yang_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004206 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004208 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004209 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 struct lysp_ident *ident;
4211
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004212 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004213
4214 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004215 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004216 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004217
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004218 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004220 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004221 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 +02004222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004223 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004224 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004225 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004226 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004227 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004228 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 +02004229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004230 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004231 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004232 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004233 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004234 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
Michal Vasko72c6d642024-02-27 14:59:01 +01004235 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
4236 "Identity can be derived from multiple base identities only in YANG 1.1 modules");
Radek Krejci10113652018-11-14 16:56:50 +01004237 return LY_EVALID;
4238 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004239 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 +02004240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004241 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004242 LY_CHECK_RET(parse_ext(ctx, word, word_len, ident, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004243 break;
4244 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004245 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004246 return LY_EVALID;
4247 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004248 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004249 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004250
4251cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004252 return ret;
4253}
4254
Michal Vaskoea5abea2018-09-18 13:10:54 +02004255/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004256 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004257 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004258 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004259 * @param[in,out] mod Module to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004260 * @return LY_ERR values.
4261 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004262LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004263parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004264{
4265 LY_ERR ret = 0;
4266 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004267 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004268 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004269 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004270 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004271
Michal Vaskoc3781c32020-10-06 14:04:08 +02004272 mod->is_submod = 0;
4273
4274 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004275 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004276 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004277
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004278 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004279
Radek Krejcie3846472018-10-15 15:24:51 +02004280#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004281 if (mod_stmt > SECTION) {\
4282 LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;\
4283 } mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004284
Michal Vasko7fbc8162018-09-17 10:35:16 +02004285 switch (kw) {
4286 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004287 case LY_STMT_NAMESPACE:
4288 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004289 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4290 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004291 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004292 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004293 break;
4294 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004295 case LY_STMT_INCLUDE:
4296 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004297 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004298 break;
4299 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004300 case LY_STMT_ORGANIZATION:
4301 case LY_STMT_CONTACT:
4302 case LY_STMT_DESCRIPTION:
4303 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004304 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004305 break;
4306
4307 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004308 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004309 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004310 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004311 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004312 case LY_STMT_ANYDATA:
4313 case LY_STMT_ANYXML:
4314 case LY_STMT_AUGMENT:
4315 case LY_STMT_CHOICE:
4316 case LY_STMT_CONTAINER:
4317 case LY_STMT_DEVIATION:
4318 case LY_STMT_EXTENSION:
4319 case LY_STMT_FEATURE:
4320 case LY_STMT_GROUPING:
4321 case LY_STMT_IDENTITY:
4322 case LY_STMT_LEAF:
4323 case LY_STMT_LEAF_LIST:
4324 case LY_STMT_LIST:
4325 case LY_STMT_NOTIFICATION:
4326 case LY_STMT_RPC:
4327 case LY_STMT_TYPEDEF:
4328 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004329 mod_stmt = Y_MOD_BODY;
4330 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004331 case LY_STMT_EXTENSION_INSTANCE:
4332 /* no place in the statement order defined */
4333 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004334 default:
4335 /* error handled in the next switch */
4336 break;
4337 }
Radek Krejcie3846472018-10-15 15:24:51 +02004338#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004339
Radek Krejcie3846472018-10-15 15:24:51 +02004340 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004341 switch (kw) {
4342 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004343 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004344 LY_CHECK_RET(parse_yangversion(ctx, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004345 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004346 case LY_STMT_NAMESPACE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004347 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 +02004348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004349 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01004350 LY_CHECK_RET(parse_text_field(ctx, mod->mod->prefix, LY_STMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG,
4351 NULL, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004352 break;
4353
4354 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004355 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004356 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004357 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004358 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004359 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004360 break;
4361
4362 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_ORGANIZATION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004364 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 +02004365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_CONTACT:
Michal Vasko72c6d642024-02-27 14:59:01 +01004367 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 +02004368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004370 LY_CHECK_RET(parse_text_field(ctx, mod->mod->dsc, LY_STMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, NULL,
4371 &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004373 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004374 LY_CHECK_RET(parse_text_field(ctx, mod->mod->ref, LY_STMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, NULL,
4375 &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004376 break;
4377
4378 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004379 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004380 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004381 break;
4382
4383 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004385 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004386 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004388 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004390 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004391 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004393 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004394 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004396 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004397 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004399 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004400 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004402 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004403 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004405 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004406 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004407 break;
4408
Radek Krejcid6b76452019-09-03 17:03:03 +02004409 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004410 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004412 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004413 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004415 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004416 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004418 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004419 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004421 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004422 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004423 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004424 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004425 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004426 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004427 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004428 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004429 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004430 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004431 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004433 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004434 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004436 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004437 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004438 break;
4439
4440 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004441 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004442 return LY_EVALID;
4443 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004444 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004445 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004446
Michal Vasko7fbc8162018-09-17 10:35:16 +02004447 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004448 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004449 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004450 return LY_EVALID;
4451 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004452 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004453 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004454 }
4455
Radek Krejcie9e987e2018-10-31 12:50:27 +01004456 /* submodules share the namespace with the module names, so there must not be
4457 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004458 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004459 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004460 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004461 return LY_EVALID;
4462 }
4463
Michal Vasko12ef5362022-09-16 15:13:58 +02004464cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004465 return ret;
4466}
4467
4468/**
4469 * @brief Parse submodule substatements.
4470 *
4471 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004472 * @param[out] submod Parsed submodule structure.
4473 *
4474 * @return LY_ERR values.
4475 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004476LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004477parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004478{
4479 LY_ERR ret = 0;
4480 char *buf, *word;
4481 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004482 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004483 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004484 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004485
Michal Vaskoc3781c32020-10-06 14:04:08 +02004486 submod->is_submod = 1;
4487
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004488 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004489 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004490 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004491
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004492 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004493
4494#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004495 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 +01004496
4497 switch (kw) {
4498 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004499 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004500 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4501 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004502 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004503 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4504 break;
4505 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004506 case LY_STMT_INCLUDE:
4507 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004508 CHECK_ORDER(Y_MOD_LINKAGE);
4509 break;
4510 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004511 case LY_STMT_ORGANIZATION:
4512 case LY_STMT_CONTACT:
4513 case LY_STMT_DESCRIPTION:
4514 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004515 CHECK_ORDER(Y_MOD_META);
4516 break;
4517
4518 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004519 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004520 CHECK_ORDER(Y_MOD_REVISION);
4521 break;
4522 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004523 case LY_STMT_ANYDATA:
4524 case LY_STMT_ANYXML:
4525 case LY_STMT_AUGMENT:
4526 case LY_STMT_CHOICE:
4527 case LY_STMT_CONTAINER:
4528 case LY_STMT_DEVIATION:
4529 case LY_STMT_EXTENSION:
4530 case LY_STMT_FEATURE:
4531 case LY_STMT_GROUPING:
4532 case LY_STMT_IDENTITY:
4533 case LY_STMT_LEAF:
4534 case LY_STMT_LEAF_LIST:
4535 case LY_STMT_LIST:
4536 case LY_STMT_NOTIFICATION:
4537 case LY_STMT_RPC:
4538 case LY_STMT_TYPEDEF:
4539 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004540 mod_stmt = Y_MOD_BODY;
4541 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004542 case LY_STMT_EXTENSION_INSTANCE:
4543 /* no place in the statement order defined */
4544 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004545 default:
4546 /* error handled in the next switch */
4547 break;
4548 }
4549#undef CHECK_ORDER
4550
4551 prev_kw = kw;
4552 switch (kw) {
4553 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004554 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004555 LY_CHECK_RET(parse_yangversion(ctx, (struct lysp_module *)submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004556 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004557 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02004558 LY_CHECK_RET(parse_belongsto(ctx, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004559 break;
4560
4561 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004562 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004563 if (submod->version == LYS_VERSION_1_1) {
4564 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4565 submod->name);
4566 }
Radek Krejci33090f92020-12-17 20:12:46 +01004567 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004568 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004569 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004570 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004571 break;
4572
4573 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004574 case LY_STMT_ORGANIZATION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004575 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 +01004576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004577 case LY_STMT_CONTACT:
Michal Vasko72c6d642024-02-27 14:59:01 +01004578 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 +01004579 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004580 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004581 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 +01004582 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004583 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004584 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 +01004585 break;
4586
4587 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004588 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004589 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004590 break;
4591
4592 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004593 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004594 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004595 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004596 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004597 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004598 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004599 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004600 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004601 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004602 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004603 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004604 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004605 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004606 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004607 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004608 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004609 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004610 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004611 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004612 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004614 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004615 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004616 break;
4617
Radek Krejcid6b76452019-09-03 17:03:03 +02004618 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004619 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004621 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004622 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004624 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004625 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004627 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004628 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004630 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004631 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004632 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004633 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004634 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004635 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004636 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004637 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004639 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004640 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004642 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004643 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004644 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004645 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004646 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004647 break;
4648
4649 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004650 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004651 return LY_EVALID;
4652 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004653 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004654 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004655
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004656 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004657 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004658 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004659 return LY_EVALID;
4660 }
4661
4662 /* submodules share the namespace with the module names, so there must not be
4663 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004664 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004665 /* main modules may have different revisions */
4666 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004667 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004668 return LY_EVALID;
4669 }
4670
Michal Vasko12ef5362022-09-16 15:13:58 +02004671cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004672 return ret;
4673}
4674
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004675/**
4676 * @brief Skip any redundant characters, namely whitespaces and comments.
4677 *
4678 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004679 * @return LY_SUCCESS on success.
4680 * @return LY_EVALID on invalid comment.
4681 */
4682static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004683skip_redundant_chars(struct lysp_yang_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004684{
4685 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004686 while (ctx->in->current[0]) {
4687 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004688 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004689 ly_in_skip(ctx->in, 2);
4690 LY_CHECK_RET(skip_comment(ctx, 1));
4691 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004692 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004693 ly_in_skip(ctx->in, 2);
4694 LY_CHECK_RET(skip_comment(ctx, 2));
4695 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004696 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004697 if (ctx->in->current[0] == '\n') {
4698 LY_IN_NEW_LINE(ctx->in);
4699 }
Radek Krejci33090f92020-12-17 20:12:46 +01004700 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004701 } else {
4702 break;
4703 }
4704 }
4705
4706 return LY_SUCCESS;
4707}
4708
Radek Krejcid4557c62018-09-17 11:42:09 +02004709LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004710yang_parse_submodule(struct lysp_yang_ctx **context, struct ly_ctx *ly_ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004711 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004712{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004713 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004714 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004715 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004716 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004717 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004718 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004719
aPiecek56be92a2021-07-01 07:18:10 +02004720 assert(context && ly_ctx && main_ctx && in && submod);
4721
David Sedlák1b623122019-08-05 15:27:49 +02004722 /* create context */
4723 *context = calloc(1, sizeof **context);
4724 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004725 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004726 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004727 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004728
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004729 mod_p = calloc(1, sizeof *mod_p);
4730 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004731 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004732 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004733
4734 /* use main context parsed mods adding the current one */
4735 (*context)->parsed_mods = main_ctx->parsed_mods;
4736 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004737
Michal Vasko7a266772024-01-23 11:02:38 +01004738 ly_log_location(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004739
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004740 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004741 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004742 LY_CHECK_GOTO(ret, cleanup);
4743
Michal Vasko7fbc8162018-09-17 10:35:16 +02004744 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004745 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004746 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004747
Radek Krejcid6b76452019-09-03 17:03:03 +02004748 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004749 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004750 ret = LY_EINVAL;
4751 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004752 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004753 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004754 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004755 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004756 }
4757
Michal Vasko7fbc8162018-09-17 10:35:16 +02004758 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004759 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004760 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004761
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004762 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004763 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004764 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004765 if (in->current[0]) {
4766 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004767 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004768 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004769 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004770
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004771 mod_p->parsing = 0;
4772 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004773
Radek Krejcibbe09a92018-11-08 09:36:54 +01004774cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +01004775 ly_log_location_revert(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004776 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004777 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004778 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004779 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004780 }
4781
4782 return ret;
4783}
4784
4785LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004786yang_parse_module(struct lysp_yang_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004787{
4788 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004789 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004790 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004791 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004792 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004793 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004794
David Sedlák1b623122019-08-05 15:27:49 +02004795 /* create context */
4796 *context = calloc(1, sizeof **context);
4797 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004798 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004799 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004800 (*context)->in = in;
Michal Vaskod0625d72022-10-06 15:02:50 +02004801 (*context)->main_ctx = (struct lysp_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004802
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004803 mod_p = calloc(1, sizeof *mod_p);
4804 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4805 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004806 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004807
Michal Vasko7a266772024-01-23 11:02:38 +01004808 ly_log_location(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004809
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004810 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004811 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004812 LY_CHECK_GOTO(ret, cleanup);
4813
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004814 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004815 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004816 LY_CHECK_GOTO(ret, cleanup);
4817
Radek Krejcid6b76452019-09-03 17:03:03 +02004818 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004819 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 +01004820 ret = LY_EINVAL;
4821 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004822 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004823 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004824 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004825 goto cleanup;
4826 }
4827
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004828 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004829 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004830 LY_CHECK_GOTO(ret, cleanup);
4831
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004832 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004833 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004834 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004835 if (in->current[0]) {
4836 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004837 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004838 goto cleanup;
4839 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004840
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004841 mod->parsed = mod_p;
4842
4843cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +01004844 ly_log_location_revert(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004845 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004846 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004847 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004848 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004849 }
4850
Michal Vasko7fbc8162018-09-17 10:35:16 +02004851 return ret;
4852}