blob: e3231918a72aac40999c8869ef96984bd6984ca7 [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;
Michal Vasko453b0002024-05-21 15:10:23 +0200534 case '/':
535 if (ctx->in->current[1] == '/') {
536 /* one-line comment */
537 MOVE_INPUT(ctx, 2);
538 LY_CHECK_RET(skip_comment(ctx, 1));
539 break;
540 } else if (ctx->in->current[1] == '*') {
541 /* block comment */
542 MOVE_INPUT(ctx, 2);
543 LY_CHECK_RET(skip_comment(ctx, 2));
544 break;
545 }
546 /* not a comment after all */
547 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200548 default:
549 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200550 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200551 return LY_EVALID;
552 }
Radek Krejci33090f92020-12-17 20:12:46 +0100553 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200554 break;
555 default:
556 return LY_EINT;
557 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200558 }
559
560string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200561 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200562 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200563 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200564 return LY_EVALID;
565 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200566 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100567
568#undef STRING_ENDED
569#undef STRING_SINGLE_QUOTED
570#undef STRING_DOUBLE_QUOTED
571#undef STRING_DOUBLE_QUOTED_ESCAPED
572#undef STRING_PAUSED_NEXTSTRING
573#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200574}
575
Michal Vaskoea5abea2018-09-18 13:10:54 +0200576/**
577 * @brief Get another YANG string from the raw data.
578 *
Michal Vasko72c6d642024-02-27 14:59:01 +0100579 * @param[in] ctx Yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200580 * @param[in] arg Type of YANG keyword argument expected.
Michal Vasko72c6d642024-02-27 14:59:01 +0100581 * @param[out] flags Optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
Michal Vaskob68ea142021-04-26 13:46:00 +0200582 * [schema node flags](@ref snodeflags))
Michal Vasko72c6d642024-02-27 14:59:01 +0100583 * @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 +0200584 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
Michal Vasko72c6d642024-02-27 14:59:01 +0100585 * set to NULL. Otherwise equal to @p word_p.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200586 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200587 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200588 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200589LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200590get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200591 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200592{
Michal Vaskob68ea142021-04-26 13:46:00 +0200593 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200594 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200595 uint8_t prefix = 0;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100596 ly_bool str_end = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200597
Michal Vasko7fbc8162018-09-17 10:35:16 +0200598 /* word buffer - dynamically allocated */
599 *word_b = NULL;
600
601 /* word pointer - just a pointer to data */
602 *word_p = NULL;
603
604 *word_len = 0;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100605 while (!str_end) {
Radek Krejci33090f92020-12-17 20:12:46 +0100606 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200607 case '\'':
608 case '\"':
609 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200610 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100611 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200612 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200613 ret = LY_EVALID;
614 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200615 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200616 if (flags) {
Michal Vasko72c6d642024-02-27 14:59:01 +0100617 (*flags) |= (ctx->in->current[0] == '\'') ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200618 }
Michal Vasko1e4b1162024-01-19 09:16:49 +0100619
Michal Vaskob68ea142021-04-26 13:46:00 +0200620 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200621 if (!*word_p) {
622 /* do not return NULL word */
623 *word_p = "";
624 }
Michal Vasko1e4b1162024-01-19 09:16:49 +0100625 str_end = 1;
626 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200627 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100628 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200629 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100630 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200631 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100632 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200633 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100634 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200635 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200636 } else {
637 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200638 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 +0200639 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200640 break;
641 case ' ':
642 if (*word_len) {
643 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100644 str_end = 1;
645 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200646 }
Radek Krejci33090f92020-12-17 20:12:46 +0100647 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200648 break;
649 case '\t':
650 if (*word_len) {
651 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100652 str_end = 1;
653 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200654 }
655 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100656 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200657
Radek Krejci33090f92020-12-17 20:12:46 +0100658 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200660 case '\r':
661 if (ctx->in->current[1] != '\n') {
662 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200663 ret = LY_EVALID;
664 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200665 }
666 MOVE_INPUT(ctx, 1);
667 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200668 case '\n':
669 if (*word_len) {
670 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100671 str_end = 1;
672 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200673 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100674 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100675 MOVE_INPUT(ctx, 1);
676
Michal Vasko7fbc8162018-09-17 10:35:16 +0200677 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200678 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200679 break;
680 case ';':
681 case '{':
682 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
683 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100684 str_end = 1;
685 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686 }
687
Radek Krejci33090f92020-12-17 20:12:46 +0100688 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200689 ret = LY_EVALID;
690 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200691 case '}':
692 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100693 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200694 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200695 ret = LY_EVALID;
696 goto error;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100697 case '\0':
698 /* unexpected EOF */
699 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
700 ret = LY_EVALID;
701 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200702 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200703 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 +0200704 break;
705 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200706 }
707
Radek Krejci44ceedc2018-10-02 15:54:31 +0200708 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200709 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200710 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200711 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200712 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200713 *word_p = *word_b;
714 }
715
716 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200717
718error:
719 free(*word_b);
720 *word_b = NULL;
721 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200722}
723
Michal Vaskoea5abea2018-09-18 13:10:54 +0200724/**
725 * @brief Get another YANG keyword from the raw data.
726 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200727 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200728 * @param[out] kw YANG keyword read.
729 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
730 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200731 * @return LY_ERR values.
732 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200733LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200734get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200735{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200736 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200738 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200739
740 if (word_p) {
741 *word_p = NULL;
742 *word_len = 0;
743 }
744
745 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100746 while (ctx->in->current[0]) {
747 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200748 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100749 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200750 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100751 MOVE_INPUT(ctx, 2);
752 LY_CHECK_RET(skip_comment(ctx, 1));
753 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200754 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100755 MOVE_INPUT(ctx, 2);
756 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200757 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200758 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200759 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200760 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200761 }
Radek Krejci13028282019-06-11 14:56:48 +0200762 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200763 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200764 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100765 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200766 ctx->indent = 0;
767 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200768 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200769 /* skip whitespaces (optsep) */
770 ++ctx->indent;
771 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200772 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200773 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100774 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200775 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100776 case '\r':
777 /* possible CRLF endline */
778 if (ctx->in->current[1] == '\n') {
779 break;
780 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100781 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200782 default:
783 /* either a keyword start or an invalid character */
784 goto keyword_start;
785 }
786
Radek Krejci33090f92020-12-17 20:12:46 +0100787 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200788 }
789
790keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100791 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100792 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200793
aPiecek93582ed2021-05-25 14:49:06 +0200794 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
795 goto success;
796 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
797 ctx->depth++;
798 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100799 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200800 return LY_EINVAL;
801 }
802 goto success;
803 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
804 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200805 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200806 }
807
Radek Krejcid6b76452019-09-03 17:03:03 +0200808 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200809 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100810 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200811 case '\r':
812 if (ctx->in->current[1] != '\n') {
813 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
814 return LY_EVALID;
815 }
816 MOVE_INPUT(ctx, 1);
817 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200818 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200819 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200820 case ' ':
821 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200822 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200823 case ':':
824 /* keyword is not actually a keyword, but prefix of an extension.
825 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
826 * and we will be checking the keyword (extension instance) itself */
827 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100828 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200829 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200830 case '{':
Michal Vasko946b70c2023-07-19 08:57:31 +0200831 case ';':
832 /* allowed only for input and output statements which are without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200833 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200834 break;
835 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100836 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100838 MOVE_INPUT(ctx, 1);
839 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200840 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841 return LY_EVALID;
842 }
843 } else {
844 /* still can be an extension */
845 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200846
Radek Krejci156ccaf2018-10-15 15:49:17 +0200847extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100848 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200849 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
850 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200851 uint32_t c = 0;
852
Radek Krejci33090f92020-12-17 20:12:46 +0100853 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
854 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200855 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200856 /* check character validity */
Michal Vaskod0625d72022-10-06 15:02:50 +0200857 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100858 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200859 }
Radek Krejci33090f92020-12-17 20:12:46 +0100860 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200861 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200862 return LY_EVALID;
863 }
864
865 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200866 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100867 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200868 return LY_EVALID;
869 }
870
Radek Krejcid6b76452019-09-03 17:03:03 +0200871 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200872 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200873
Radek Krejci626df482018-10-11 15:06:31 +0200874success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200875 if (word_p) {
876 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100877 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878 }
879
880 return LY_SUCCESS;
881}
882
Michal Vaskoea5abea2018-09-18 13:10:54 +0200883/**
884 * @brief Parse extension instance substatements.
885 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200886 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200887 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200888 * @param[in] word Extension instance substatement name (keyword).
889 * @param[in] word_len Extension instance substatement name length.
890 * @param[in,out] child Children of this extension instance to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200891 * @return LY_ERR values.
892 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200893static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200894parse_ext_substmt(struct lysp_yang_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200895 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200896{
897 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100898 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200899 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200900 struct lysp_stmt *stmt, *par_child;
901
902 stmt = calloc(1, sizeof *stmt);
903 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
904
Radek Krejcibb9b1982019-04-08 14:24:59 +0200905 /* insert into parent statements */
906 if (!*child) {
907 *child = stmt;
908 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200909 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200910 par_child->next = stmt;
911 }
912
Michal Vaskofc2cd072021-02-24 13:17:17 +0100913 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200914 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200915
916 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100917 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200918 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200919 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200920 }
921
Radek Krejci8df109d2021-04-23 12:19:08 +0200922 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100923 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100924 stmt->kw = kw;
925
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200926 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
927 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
928 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200929 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200930
931cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932 return ret;
933}
934
Michal Vaskoea5abea2018-09-18 13:10:54 +0200935/**
936 * @brief Parse extension instance.
937 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200938 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200939 * @param[in] ext_name Extension instance substatement name (keyword).
940 * @param[in] ext_name_len Extension instance substatement name length.
Michal Vasko193dacd2022-10-13 08:43:05 +0200941 * @param[in] parent Current statement parent.
942 * @param[in] parent_stmt Type of @p parent statement.
943 * @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 +0200944 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200945 * @return LY_ERR values.
946 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200947static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200948parse_ext(struct lysp_yang_ctx *ctx, const char *ext_name, size_t ext_name_len, const void *parent,
949 enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200950{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100951 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200952 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200953 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200954 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200955 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200956
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200957 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200958
Michal Vaskofc2cd072021-02-24 13:17:17 +0100959 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200960 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%.*s\" without the mandatory prefix.",
961 (int)ext_name_len, ext_name);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100962 return LY_EVALID;
963 }
964
965 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200966 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200967
968 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100969 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200970 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200971 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200972 }
973
Michal Vaskofc2cd072021-02-24 13:17:17 +0100974 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200975 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200976 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100977 e->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200978 e->parent = (void *)parent;
979 e->parent_stmt = parent_stmt;
980 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100981
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200982 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
983 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
984 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200985 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200986
987cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988 return ret;
989}
990
Michal Vaskoea5abea2018-09-18 13:10:54 +0200991/**
992 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
993 * description, etc...
994 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200995 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +0200996 * @param[in] parent Current statement parent.
997 * @param[in] parent_stmt Type of statement in @p value.
998 * @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 +0200999 * @param[in,out] value Place to store the parsed value.
1000 * @param[in] arg Type of the YANG keyword argument (of the value).
Michal Vasko72c6d642024-02-27 14:59:01 +01001001 * @param[out] flags Optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
1002 * [schema node flags](@ref snodeflags))
Michal Vaskoea5abea2018-09-18 13:10:54 +02001003 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001004 * @return LY_ERR values.
1005 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001007parse_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 +01001008 const char **value, enum yang_arg arg, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001010 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001012 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001013 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014
1015 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001016 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001017 return LY_EVALID;
1018 }
1019
1020 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001021 LY_CHECK_RET(get_argument(ctx, arg, flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001022
1023 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +02001024 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001026 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001027 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001028 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001029 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent, parent_stmt, parent_stmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001030 break;
1031 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001032 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001033 return LY_EVALID;
1034 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001035 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001037
1038cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039 return ret;
1040}
1041
Michal Vaskoea5abea2018-09-18 13:10:54 +02001042/**
1043 * @brief Parse the yang-version statement.
1044 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001045 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001046 * @param[in,out] mod Module to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001047 * @return LY_ERR values.
1048 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001049static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001050parse_yangversion(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001052 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001054 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001055 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056
Michal Vasko193dacd2022-10-13 08:43:05 +02001057 if (mod->version) {
David Sedlákb3077192019-06-19 10:55:37 +02001058 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001059 return LY_EVALID;
1060 }
1061
1062 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001063 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064
Radek Krejci96e48da2020-09-04 13:18:06 +02001065 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001066 mod->version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001067 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001068 mod->version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001070 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071 free(buf);
1072 return LY_EVALID;
1073 }
1074 free(buf);
1075
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001076 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001077 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001078 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001079 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_YANG_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001080 break;
1081 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001082 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083 return LY_EVALID;
1084 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001085 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001086 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001087
1088cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 return ret;
1090}
1091
Michal Vaskoea5abea2018-09-18 13:10:54 +02001092/**
1093 * @brief Parse the belongs-to statement.
1094 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001095 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001096 * @param[in,out] submod Submodule to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001097 * @return LY_ERR values.
1098 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001099static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001100parse_belongsto(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001101{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001102 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001103 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001104 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001105 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001106
Michal Vasko193dacd2022-10-13 08:43:05 +02001107 if (submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001108 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001109 return LY_EVALID;
1110 }
1111
Michal Vaskoc3781c32020-10-06 14:04:08 +02001112 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001113 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001114 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001115 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 +01001116 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001117 free(buf);
1118 return LY_EVALID;
1119 }
1120 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001121
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001122 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001123 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001124 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01001125 LY_CHECK_RET(parse_text_field(ctx, submod->prefix, LY_STMT_PREFIX, 0, &submod->prefix, Y_IDENTIF_ARG, NULL,
1126 &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001128 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001129 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_BELONGS_TO, 0, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130 break;
1131 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001132 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001133 return LY_EVALID;
1134 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001135 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001137
Michal Vasko7fbc8162018-09-17 10:35:16 +02001138 /* mandatory substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +02001139 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001140 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 return LY_EVALID;
1142 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001143
1144cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145 return ret;
1146}
1147
Michal Vaskoea5abea2018-09-18 13:10:54 +02001148/**
1149 * @brief Parse the revision-date statement.
1150 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001151 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001152 * @param[in,out] rev Buffer to store the parsed value in.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001153 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001154 * @return LY_ERR values.
1155 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001156static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001157parse_revisiondate(struct lysp_yang_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001159 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001161 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001162 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001163
1164 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001165 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001166 return LY_EVALID;
1167 }
1168
1169 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001170 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001171
1172 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001173 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001174 free(buf);
1175 return LY_EVALID;
1176 }
1177
1178 /* store value and spend buf if allocated */
1179 strncpy(rev, word, word_len);
1180 free(buf);
1181
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001182 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001184 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001185 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001186 break;
1187 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001188 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189 return LY_EVALID;
1190 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001191 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001193
1194cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195 return ret;
1196}
1197
Michal Vaskoea5abea2018-09-18 13:10:54 +02001198/**
1199 * @brief Parse the include statement.
1200 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001201 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001202 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001203 * @param[in,out] includes Parsed includes to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001204 * @return LY_ERR values.
1205 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001207parse_include(struct lysp_yang_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208{
Radek Krejcid33273d2018-10-25 14:55:52 +02001209 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001210 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001211 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001212 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001213 struct lysp_include *inc;
1214
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001215 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001216
1217 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001218 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219
Michal Vasko12ef5362022-09-16 15:13:58 +02001220 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001221
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001222 /* submodules share the namespace with the module names, so there must not be
1223 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001224 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001225 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001226 return LY_EVALID;
1227 }
1228
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001229 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001231 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001232 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko72c6d642024-02-27 14:59:01 +01001233 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 +02001234 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001235 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001236 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko72c6d642024-02-27 14:59:01 +01001237 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 +02001238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001239 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001240 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001242 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001243 LY_CHECK_RET(parse_ext(ctx, word, word_len, inc, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001244 break;
1245 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001246 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001247 return LY_EVALID;
1248 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001249 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001250 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001251
1252cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001253 return ret;
1254}
1255
Michal Vaskoea5abea2018-09-18 13:10:54 +02001256/**
1257 * @brief Parse the import statement.
1258 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001259 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001260 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001261 * @param[in,out] imports Parsed imports to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001262 * @return LY_ERR values.
1263 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001264static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001265parse_import(struct lysp_yang_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001267 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001269 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001270 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001271 struct lysp_import *imp;
1272
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001273 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001274
1275 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001276 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001277 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001278
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001279 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001281 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01001282 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 +02001283 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001284 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001285 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001286 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko72c6d642024-02-27 14:59:01 +01001287 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 +02001288 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001289 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001290 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko72c6d642024-02-27 14:59:01 +01001291 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 +02001292 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001293 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001294 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001295 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001296 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001297 LY_CHECK_RET(parse_ext(ctx, word, word_len, imp, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001298 break;
1299 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001300 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001301 return LY_EVALID;
1302 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001303 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001304 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001305
Michal Vasko7fbc8162018-09-17 10:35:16 +02001306 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001307 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001308
Michal Vasko12ef5362022-09-16 15:13:58 +02001309cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310 return ret;
1311}
1312
Michal Vaskoea5abea2018-09-18 13:10:54 +02001313/**
1314 * @brief Parse the revision statement.
1315 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001316 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001317 * @param[in,out] revs Parsed revisions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001318 * @return LY_ERR values.
1319 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001320static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001321parse_revision(struct lysp_yang_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001322{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001323 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001325 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001326 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327 struct lysp_revision *rev;
1328
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001329 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330
1331 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001332 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001333
1334 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001335 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001336 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337 return LY_EVALID;
1338 }
1339
Radek Krejcib7db73a2018-10-24 14:18:40 +02001340 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001341 free(buf);
1342
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001343 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001344 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001345 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001346 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 +02001347 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001348 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001349 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 +02001350 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001351 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001352 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353 break;
1354 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001355 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001356 return LY_EVALID;
1357 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001358 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001359 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001360
1361cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362 return ret;
1363}
1364
Michal Vaskoea5abea2018-09-18 13:10:54 +02001365/**
1366 * @brief Parse a generic text field that can have more instances such as base.
1367 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001368 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001369 * @param[in] parent Current statement parent.
1370 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001371 * @param[in,out] texts Parsed values to add to.
1372 * @param[in] arg Type of the expected argument.
1373 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001374 * @return LY_ERR values.
1375 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001376static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001377parse_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 +02001378 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001379{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001380 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001382 const char **item;
1383 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001384 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001385
1386 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001387 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388
1389 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001390 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001391
Michal Vasko12ef5362022-09-16 15:13:58 +02001392 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001393 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001394 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001395 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001396 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 +02001397 break;
1398 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001399 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001400 return LY_EVALID;
1401 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001402 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001404
1405cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001406 return ret;
1407}
1408
Michal Vaskoea5abea2018-09-18 13:10:54 +02001409/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001410 * @brief Parse a generic text field that can have more instances such as base.
1411 *
1412 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001413 * @param[in] parent_stmt Type of statement stored in @p qnames.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001414 * @param[in,out] qnames Parsed qnames to add to.
1415 * @param[in] arg Type of the expected argument.
1416 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001417 * @return LY_ERR values.
1418 */
1419static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001420parse_qnames(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_qname **qnames, enum yang_arg arg,
1421 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +02001422{
1423 LY_ERR ret = LY_SUCCESS;
1424 char *buf, *word;
1425 struct lysp_qname *item;
1426 size_t word_len;
1427 enum ly_stmt kw;
1428
1429 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001430 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001431
1432 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001433 LY_CHECK_RET(get_argument(ctx, arg, &item->flags, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001434
Michal Vasko12ef5362022-09-16 15:13:58 +02001435 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001436 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001437 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001438 switch (kw) {
1439 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001440 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 +02001441 break;
1442 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001443 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001444 return LY_EVALID;
1445 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001446 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001447 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001448
1449cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001450 return ret;
1451}
1452
1453/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001454 * @brief Parse the config statement.
1455 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001456 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001457 * @param[in,out] flags Flags to add to.
1458 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001459 * @return LY_ERR values.
1460 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001462parse_config(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001464 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001466 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001467 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468
1469 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001470 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001471 return LY_EVALID;
1472 }
1473
1474 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001475 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001476
Radek Krejcif13b87b2020-12-01 22:02:17 +01001477 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001479 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001480 *flags |= LYS_CONFIG_R;
1481 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001482 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483 free(buf);
1484 return LY_EVALID;
1485 }
1486 free(buf);
1487
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001488 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001490 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001491 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001492 break;
1493 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001494 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001495 return LY_EVALID;
1496 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001497 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001499
1500cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501 return ret;
1502}
1503
Michal Vaskoea5abea2018-09-18 13:10:54 +02001504/**
1505 * @brief Parse the mandatory statement.
1506 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001507 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001508 * @param[in,out] flags Flags to add to.
1509 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001510 * @return LY_ERR values.
1511 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001512static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001513parse_mandatory(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001515 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001517 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001518 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519
1520 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001521 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001522 return LY_EVALID;
1523 }
1524
1525 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001526 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001527
Radek Krejcif13b87b2020-12-01 22:02:17 +01001528 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001529 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001530 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531 *flags |= LYS_MAND_FALSE;
1532 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001533 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001534 free(buf);
1535 return LY_EVALID;
1536 }
1537 free(buf);
1538
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001539 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001540 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001541 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001542 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001543 break;
1544 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001545 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546 return LY_EVALID;
1547 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001548 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001549 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001550
1551cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552 return ret;
1553}
1554
Michal Vaskoea5abea2018-09-18 13:10:54 +02001555/**
1556 * @brief Parse a restriction such as range or length.
1557 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001558 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001559 * @param[in] restr_kw Type of this particular restriction.
1560 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001561 * @return LY_ERR values.
1562 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001563static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001564parse_restr(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001566 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001568 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001569 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001570
1571 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001572 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, &restr->arg.flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001573
Michal Vasko193dacd2022-10-13 08:43:05 +02001574 CHECK_NONEMPTY(ctx, word_len, lyplg_ext_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001575 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001576 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001577 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001578 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001579 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001580 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 +02001581 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001582 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001583 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 +02001584 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001585 case LY_STMT_ERROR_APP_TAG:
Michal Vasko72c6d642024-02-27 14:59:01 +01001586 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 +02001587 &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001589 case LY_STMT_ERROR_MESSAGE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001590 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 +02001591 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001592 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001593 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001594 break;
1595 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001596 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001597 return LY_EVALID;
1598 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001599 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001601
1602cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603 return ret;
1604}
1605
Michal Vaskoea5abea2018-09-18 13:10:54 +02001606/**
1607 * @brief Parse a restriction that can have more instances such as must.
1608 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001609 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001610 * @param[in] restr_kw Type of this particular restriction.
1611 * @param[in,out] restrs Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001612 * @return LY_ERR values.
1613 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001614static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001615parse_restrs(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001616{
1617 struct lysp_restr *restr;
1618
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001619 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001620 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621}
1622
Michal Vaskoea5abea2018-09-18 13:10:54 +02001623/**
1624 * @brief Parse the status statement.
1625 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001626 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001627 * @param[in,out] flags Flags to add to.
1628 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001629 * @return LY_ERR values.
1630 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001632parse_status(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001634 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001635 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001636 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001637 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638
1639 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001640 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641 return LY_EVALID;
1642 }
1643
1644 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001645 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001646
Radek Krejcif13b87b2020-12-01 22:02:17 +01001647 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001648 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001649 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001650 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001651 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001652 *flags |= LYS_STATUS_OBSLT;
1653 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001654 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001655 free(buf);
1656 return LY_EVALID;
1657 }
1658 free(buf);
1659
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001660 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001662 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001663 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 break;
1665 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001666 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667 return LY_EVALID;
1668 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001669 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001670 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001671
1672cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673 return ret;
1674}
1675
Michal Vaskoea5abea2018-09-18 13:10:54 +02001676/**
1677 * @brief Parse the when statement.
1678 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001679 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001680 * @param[in,out] when_p When pointer to parse to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001681 * @return LY_ERR values.
1682 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001683LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001684parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001685{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001686 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001688 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001689 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001691 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001692
1693 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001694 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001695 return LY_EVALID;
1696 }
1697
1698 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001699 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001700
1701 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001702 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001703 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001704 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001705
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001706 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001708 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001709 ret = parse_text_field(ctx, when->dsc, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, NULL, &when->exts);
1710 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001711 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001712 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001713 ret = parse_text_field(ctx, when->ref, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, NULL, &when->exts);
1714 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001716 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001717 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 +02001718 break;
1719 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001720 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001721 ret = LY_EVALID;
1722 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001723 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001724 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001725 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001726
1727cleanup:
1728 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001729 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001730 free(when);
1731 } else {
1732 *when_p = when;
1733 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734 return ret;
1735}
1736
Michal Vaskoea5abea2018-09-18 13:10:54 +02001737/**
1738 * @brief Parse the anydata or anyxml statement.
1739 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001740 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001741 * @param[in] any_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001742 * @param[in] parent Node parent.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001743 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001744 * @return LY_ERR values.
1745 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001746LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001747parse_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 +02001748{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001749 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001750 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001751 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001752 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001753 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754
David Sedlák60adc092019-08-06 15:57:02 +02001755 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001756 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001757
Radek Krejci39b7fc22021-02-26 23:29:18 +01001758 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001759 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001760
Michal Vasko7fbc8162018-09-17 10:35:16 +02001761 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001762 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001763 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001764
1765 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001766 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001767 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001768 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001769 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001770 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001771 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001772 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 +02001773 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001774 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001775 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001776 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001777 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001778 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001780 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001781 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001782 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001783 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001784 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 +02001785 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001786 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001787 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001788 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001789 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001790 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001791 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001792 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001793 LY_CHECK_RET(parse_ext(ctx, word, word_len, any, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 break;
1795 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001796 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797 return LY_EVALID;
1798 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001799 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001800 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001801
1802cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001803 return ret;
1804}
1805
Michal Vaskoea5abea2018-09-18 13:10:54 +02001806/**
1807 * @brief Parse the value or position statement. Substatement of type enum statement.
1808 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001809 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001810 * @param[in] val_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001811 * @param[in,out] enm Structure to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001812 * @return LY_ERR values.
1813 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001814LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001815parse_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 +02001816{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001817 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001818 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001819 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001820 long long num = 0;
1821 unsigned long long unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001822 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001823
Michal Vasko193dacd2022-10-13 08:43:05 +02001824 if (enm->flags & LYS_SET_VALUE) {
1825 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001826 ret = LY_EVALID;
1827 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001829 enm->flags |= LYS_SET_VALUE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001830
1831 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001832 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001833
Radek Krejcid6b76452019-09-03 17:03:03 +02001834 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 +02001835 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001836 ret = LY_EVALID;
1837 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001838 }
1839
1840 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001841 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001842 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001843 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
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;
Radek Krejci8b764662018-11-14 14:15:13 +01001847 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001849 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001850 if (unum > UINT64_C(4294967295)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001851 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001852 ret = LY_EVALID;
1853 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001854 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001855 }
1856 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001857 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001858 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001859 ret = LY_EVALID;
1860 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001861 }
1862 if (errno == ERANGE) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001863 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001864 ret = LY_EVALID;
1865 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001866 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001867 if (val_kw == LY_STMT_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001868 enm->value = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001869 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02001870 enm->value = unum;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001872
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001873 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001875 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001876 ret = parse_ext(ctx, word, word_len, enm, val_kw, 0, &enm->exts);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001877 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001878 break;
1879 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001880 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001881 ret = LY_EVALID;
1882 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001883 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001884 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 }
Radek Krejci8b764662018-11-14 14:15:13 +01001886
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001887cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001888 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001889 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001890}
1891
Michal Vaskoea5abea2018-09-18 13:10:54 +02001892/**
1893 * @brief Parse the enum or bit statement. Substatement of type statement.
1894 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001895 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001896 * @param[in] enum_kw Type of this particular keyword.
1897 * @param[in,out] enums Enums or bits to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001898 * @return LY_ERR values.
1899 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001900static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001901parse_type_enum(struct lysp_yang_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001903 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001905 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001906 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001907 struct lysp_type_enum *enm;
1908
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001909 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910
1911 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001912 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 +02001913 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001914 ret = lysp_check_enum_name((struct lysp_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001915 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001916 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001917
Michal Vasko12ef5362022-09-16 15:13:58 +02001918 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001919 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(enum_kw), enm->name);
Radek Krejci8b764662018-11-14 14:15:13 +01001920
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001921 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001923 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001924 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 +02001925 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001926 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001927 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001928 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001930 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001931 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 +02001932 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001933 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001934 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001935 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001936 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001937 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1938 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1939 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
David Sedlák9fb515f2019-07-11 10:33:58 +02001940 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001941 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001942 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1943 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1944 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001945 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001946 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001947 LY_CHECK_RET(parse_ext(ctx, word, word_len, enm, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001948 break;
1949 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001950 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951 return LY_EVALID;
1952 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001953 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001954 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001955
1956cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001957 return ret;
1958}
1959
Michal Vaskoea5abea2018-09-18 13:10:54 +02001960/**
1961 * @brief Parse the fraction-digits statement. Substatement of type statement.
1962 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001963 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001964 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001965 * @return LY_ERR values.
1966 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001967static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001968parse_type_fracdigits(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001970 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001971 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001972 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001973 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001974 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975
Michal Vasko193dacd2022-10-13 08:43:05 +02001976 if (type->fraction_digits) {
David Sedlákb3077192019-06-19 10:55:37 +02001977 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978 return LY_EVALID;
1979 }
1980
1981 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001982 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001983
1984 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001985 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (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 }
1989
1990 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001991 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001993 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001994 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001995 ret = LY_EVALID;
1996 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001997 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001998 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001999 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002000 ret = LY_EVALID;
2001 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002002 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002003 type->fraction_digits = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002004
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002005 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002007 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002008 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 +02002009 break;
2010 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002011 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002012 ret = LY_EVALID;
2013 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002015 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002016 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002017
2018cleanup:
2019 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020 return ret;
2021}
2022
Michal Vaskoea5abea2018-09-18 13:10:54 +02002023/**
2024 * @brief Parse the require-instance statement. Substatement of type statement.
2025 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002026 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002027 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002028 * @return LY_ERR values.
2029 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002031parse_type_reqinstance(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002032{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002033 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002034 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002035 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002036 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037
Michal Vasko193dacd2022-10-13 08:43:05 +02002038 if (type->flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002039 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002040 return LY_EVALID;
2041 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002042 type->flags |= LYS_SET_REQINST;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043
2044 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002045 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046
Radek Krejcif13b87b2020-12-01 22:02:17 +01002047 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002048 type->require_instance = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002049 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002050 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002051 ret = LY_EVALID;
2052 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002054
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002055 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002057 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002058 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 +02002059 break;
2060 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002061 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002062 ret = LY_EVALID;
2063 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002065 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002067
2068cleanup:
2069 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070 return ret;
2071}
2072
Michal Vaskoea5abea2018-09-18 13:10:54 +02002073/**
2074 * @brief Parse the modifier statement. Substatement of type pattern statement.
2075 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002076 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002077 * @param[in,out] restr Restriction to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002078 * @return LY_ERR values.
2079 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002081parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002083 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002084 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002085 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002086 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002087
Michal Vasko193dacd2022-10-13 08:43:05 +02002088 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002089 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090 return LY_EVALID;
2091 }
2092
2093 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002094 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095
Radek Krejcif13b87b2020-12-01 22:02:17 +01002096 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002097 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002098 ret = LY_EVALID;
2099 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002101
2102 /* replace the value in the dictionary */
Michal Vasko193dacd2022-10-13 08:43:05 +02002103 buf = malloc(strlen(restr->arg.str) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002104 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02002105 strcpy(buf, restr->arg.str);
2106 lydict_remove(PARSER_CTX(ctx), restr->arg.str);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107
Radek Krejcif13b87b2020-12-01 22:02:17 +01002108 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2109 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko183ea332022-11-08 10:50:03 +01002110 ret = lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002111 buf = NULL;
Michal Vasko183ea332022-11-08 10:50:03 +01002112 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002113
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002114 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002116 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002117 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 +02002118 break;
2119 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002120 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002121 ret = LY_EVALID;
2122 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002124 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002126
2127cleanup:
2128 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 return ret;
2130}
2131
Michal Vaskoea5abea2018-09-18 13:10:54 +02002132/**
2133 * @brief Parse the pattern statement. Substatement of type statement.
2134 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002135 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002136 * @param[in,out] patterns Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002137 * @return LY_ERR values.
2138 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002140parse_type_pattern(struct lysp_yang_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002142 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002144 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002145 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 struct lysp_restr *restr;
2147
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002148 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002149
2150 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01002151 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, &restr->arg.flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152
2153 /* add special meaning first byte */
2154 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002155 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 word = buf;
2157 } else {
2158 buf = malloc(word_len + 2);
2159 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002160 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002161 if (word_len) {
2162 memmove(buf + 1, word, word_len);
2163 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002164 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002165 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002166 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002167 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002169 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002170 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002171 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002172 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 +02002173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002174 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002175 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 +02002176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002177 case LY_STMT_ERROR_APP_TAG:
Michal Vasko72c6d642024-02-27 14:59:01 +01002178 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 +02002179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002180 case LY_STMT_ERROR_MESSAGE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002181 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 +02002182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002183 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002184 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko193dacd2022-10-13 08:43:05 +02002185 LY_CHECK_RET(parse_type_pattern_modifier(ctx, restr));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002187 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002188 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 break;
2190 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002191 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 return LY_EVALID;
2193 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002194 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002195 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002196
2197cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002198 return ret;
2199}
2200
Michal Vaskoea5abea2018-09-18 13:10:54 +02002201/**
2202 * @brief Parse the type statement.
2203 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002204 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002205 * @param[in,out] type Type to wrote to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002206 * @return LY_ERR values.
2207 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002208static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002209parse_type(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002210{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002211 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002213 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002214 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002215 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002216 struct lysp_type *nest_type;
2217
2218 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002219 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 return LY_EVALID;
2221 }
2222
2223 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002224 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002225 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002226
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002227 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002228 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002229
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002230 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002231 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002232 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002233 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 +01002234 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002236 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002237 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002238 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002240 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002241 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002242 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002243 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002244 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002245 LY_CHECK_RET(parse_type_fracdigits(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002246 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002247 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002248 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002249 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002250 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 return LY_EVALID;
2252 }
2253 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002254 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255
Radek Krejci33090f92020-12-17 20:12:46 +01002256 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002257 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002259 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002260 if (type->path) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002261 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002262 return LY_EVALID;
2263 }
2264
aPiecek4bb1e372021-05-07 11:01:00 +02002265 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2266 * corresponding structure, so in case of failure, the lysp_module_free function will take
2267 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2268 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2269 */
Michal Vasko72c6d642024-02-27 14:59:01 +01002270 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 +02002271 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002272 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002273 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002274 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002275 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002276 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002277 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002279 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002280 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002281 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002282 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002283 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002285 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 return LY_EVALID;
2287 }
2288 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002289 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290
Radek Krejci33090f92020-12-17 20:12:46 +01002291 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002292 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002294 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002295 LY_CHECK_RET(parse_type_reqinstance(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002296 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002297 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002298 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002299 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002300 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002301 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002303 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002304 LY_CHECK_RET(parse_ext(ctx, word, word_len, type, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002305 break;
2306 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002307 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308 return LY_EVALID;
2309 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002310 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002312
2313cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314 return ret;
2315}
2316
Michal Vaskoea5abea2018-09-18 13:10:54 +02002317/**
2318 * @brief Parse the leaf statement.
2319 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002320 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002321 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002322 * @return LY_ERR values.
2323 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002324LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002325parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002327 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002329 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002330 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 struct lysp_node_leaf *leaf;
2332
David Sedlák60adc092019-08-06 15:57:02 +02002333 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002334 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002335 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002336 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337
Michal Vasko7fbc8162018-09-17 10:35:16 +02002338 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002339 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002340 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002341
2342 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002343 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002345 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002346 LY_CHECK_RET(parse_config(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_DEFAULT:
Michal Vasko72c6d642024-02-27 14:59:01 +01002349 LY_CHECK_RET(parse_text_field(ctx, &leaf->dflt, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG,
2350 &leaf->dflt.flags, &leaf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002351 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002353 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002354 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 +02002355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002356 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002357 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002359 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002360 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002362 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002363 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002365 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002366 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 +02002367 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002368 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002369 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002371 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002372 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002374 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002375 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 +02002376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002377 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002378 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002380 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002381 LY_CHECK_RET(parse_ext(ctx, word, word_len, leaf, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382 break;
2383 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002384 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385 return LY_EVALID;
2386 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002387 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002389
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 /* mandatory substatements */
2391 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002392 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002393 return LY_EVALID;
2394 }
2395
Michal Vasko12ef5362022-09-16 15:13:58 +02002396cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 return ret;
2398}
2399
Michal Vaskoea5abea2018-09-18 13:10:54 +02002400/**
2401 * @brief Parse the max-elements statement.
2402 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002403 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002404 * @param[in,out] max Value to write to.
2405 * @param[in,out] flags Flags to write to.
2406 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002407 * @return LY_ERR values.
2408 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002409LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002410parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002412 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002413 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002414 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002415 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002416 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002417
2418 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002419 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420 return LY_EVALID;
2421 }
2422 *flags |= LYS_SET_MAX;
2423
2424 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002425 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426
2427 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002428 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002429 ret = LY_EVALID;
2430 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002431 }
2432
Radek Krejci7f9b6512019-09-18 13:11:09 +02002433 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002434 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002435 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002437 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002438 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002439 ret = LY_EVALID;
2440 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002441 }
2442 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002443 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002444 ret = LY_EVALID;
2445 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 }
2447
2448 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002449 } else {
2450 /* unbounded */
2451 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002453
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002454 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002456 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002457 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 +02002458 break;
2459 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002460 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002461 ret = LY_EVALID;
2462 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002464 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002466
2467cleanup:
2468 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469 return ret;
2470}
2471
Michal Vaskoea5abea2018-09-18 13:10:54 +02002472/**
2473 * @brief Parse the min-elements statement.
2474 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002475 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002476 * @param[in,out] min Value to write to.
2477 * @param[in,out] flags Flags to write to.
2478 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002479 * @return LY_ERR values.
2480 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002481LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002482parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002484 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002485 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002486 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002487 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002488 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002489
2490 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002491 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002492 return LY_EVALID;
2493 }
2494 *flags |= LYS_SET_MIN;
2495
2496 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002497 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002498
2499 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002500 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (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
2505 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002506 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002508 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002509 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002510 ret = LY_EVALID;
2511 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002512 }
2513 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002514 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002515 ret = LY_EVALID;
2516 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 }
2518 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002520 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002521 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002522 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002523 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 +02002524 break;
2525 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002526 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002527 ret = LY_EVALID;
2528 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002529 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002530 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002532
2533cleanup:
2534 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002535 return ret;
2536}
2537
Michal Vaskoea5abea2018-09-18 13:10:54 +02002538/**
2539 * @brief Parse the ordered-by statement.
2540 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002541 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002542 * @param[in,out] llist List or leaf-list to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002543 * @return LY_ERR values.
2544 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002545static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002546parse_orderedby(struct lysp_yang_ctx *ctx, struct lysp_node *llist)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002547{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002548 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002549 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002550 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002551 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552
Michal Vasko193dacd2022-10-13 08:43:05 +02002553 if (llist->flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002554 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 return LY_EVALID;
2556 }
2557
2558 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002559 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560
Radek Krejcif13b87b2020-12-01 22:02:17 +01002561 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002562 llist->flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002563 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002564 llist->flags |= LYS_ORDBY_USER;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002565 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002566 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002567 ret = LY_EVALID;
2568 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002569 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002571 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002573 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002574 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 +02002575 break;
2576 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002577 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002578 ret = LY_EVALID;
2579 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002580 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002581 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002582 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002583
2584cleanup:
2585 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586 return ret;
2587}
2588
Michal Vaskoea5abea2018-09-18 13:10:54 +02002589/**
2590 * @brief Parse the leaf-list statement.
2591 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002592 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002593 * @param[in,out] siblings Siblings to add to.
2594 *
2595 * @return LY_ERR values.
2596 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002597LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002598parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002599{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002600 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002601 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002602 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002603 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002604 struct lysp_node_leaflist *llist;
2605
David Sedlák60adc092019-08-06 15:57:02 +02002606 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002607 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002609 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002612 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002613 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614
2615 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002616 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002617 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002618 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002619 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002621 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002622 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002623 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002626 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 +02002627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002629 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002631 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002632 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002634 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002635 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002637 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002638 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002640 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02002641 LY_CHECK_RET(parse_orderedby(ctx, &llist->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002643 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002644 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 +02002645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002646 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002647 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002648 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002649 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002650 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002651 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002652 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002653 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 +02002654 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002655 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002656 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002658 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002659 LY_CHECK_RET(parse_ext(ctx, word, word_len, llist, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660 break;
2661 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002662 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 return LY_EVALID;
2664 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002665 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002667
Michal Vasko7fbc8162018-09-17 10:35:16 +02002668 /* mandatory substatements */
2669 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002670 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671 return LY_EVALID;
2672 }
2673
Michal Vasko12ef5362022-09-16 15:13:58 +02002674cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 return ret;
2676}
2677
Michal Vaskoea5abea2018-09-18 13:10:54 +02002678/**
2679 * @brief Parse the refine statement.
2680 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002681 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002682 * @param[in,out] refines Refines to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002683 * @return LY_ERR values.
2684 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002686parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002688 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002690 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002691 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 struct lysp_refine *rf;
2693
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002694 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695
2696 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002697 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002698 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002699 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002701 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002703 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002704 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002705 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002706 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002707 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002708 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002709 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002710 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 +02002711 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002712 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002713 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002714 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002715 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002716 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002717 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002719 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002720 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002722 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002723 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002724 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002725 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002726 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002728 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002729 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 +02002730 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002731 case LY_STMT_PRESENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002732 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 +02002733 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002734 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002735 LY_CHECK_RET(parse_ext(ctx, word, word_len, rf, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736 break;
2737 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002738 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739 return LY_EVALID;
2740 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002741 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002742 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002743
2744cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745 return ret;
2746}
2747
Michal Vaskoea5abea2018-09-18 13:10:54 +02002748/**
2749 * @brief Parse the typedef statement.
2750 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002751 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002752 * @param[in,out] typedefs Typedefs to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002753 * @return LY_ERR values.
2754 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002756parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002758 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002760 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002761 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002762 struct lysp_tpdf *tpdf;
2763
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002764 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765
2766 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002767 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002768 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769
2770 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002771 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002773 case LY_STMT_DEFAULT:
Michal Vasko72c6d642024-02-27 14:59:01 +01002774 LY_CHECK_RET(parse_text_field(ctx, &tpdf->dflt, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG,
2775 &tpdf->dflt.flags, &tpdf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002776 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002777 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002778 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002779 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 +02002780 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002781 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002782 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 +02002783 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002784 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002785 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002786 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002787 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002788 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002789 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002790 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002791 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 +02002792 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002793 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002794 LY_CHECK_RET(parse_ext(ctx, word, word_len, tpdf, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002795 break;
2796 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002797 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798 return LY_EVALID;
2799 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002800 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002801 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002802
Michal Vasko7fbc8162018-09-17 10:35:16 +02002803 /* mandatory substatements */
2804 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002805 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806 return LY_EVALID;
2807 }
2808
Radek Krejcibbe09a92018-11-08 09:36:54 +01002809 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002810 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002811 assert(ctx->main_ctx);
2812 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002813 }
2814
Michal Vasko12ef5362022-09-16 15:13:58 +02002815cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002816 return ret;
2817}
2818
Michal Vaskoea5abea2018-09-18 13:10:54 +02002819/**
2820 * @brief Parse the input or output statement.
2821 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002822 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002823 * @param[in] kw Type of this particular keyword
2824 * @param[in,out] inout_p Input/output pointer to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002825 * @return LY_ERR values.
2826 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002828parse_inout(struct lysp_yang_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002829 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002831 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002832 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002833 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002834 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002835 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002837 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002838 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839 return LY_EVALID;
2840 }
2841
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002842 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002843 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2844 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002845 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002846
2847 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002848 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002849 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002850 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002851 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002852 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002853 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002854 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002856 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002857 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002859 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002860 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002862 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002863 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002865 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002866 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002868 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002869 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002871 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002872 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002874 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002875 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002877 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002878 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002879 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002881 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002882 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002884 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002885 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_p, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002886 break;
2887 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002888 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889 return LY_EVALID;
2890 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002891 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002893
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002894cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 return ret;
2896}
2897
Michal Vaskoea5abea2018-09-18 13:10:54 +02002898/**
2899 * @brief Parse the action statement.
2900 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002901 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002902 * @param[in,out] actions Actions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002903 * @return LY_ERR values.
2904 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002905LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002906parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002908 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002910 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002911 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002912 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913
Radek Krejci2a9fc652021-01-22 17:44:34 +01002914 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915
2916 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002917 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002918 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002919 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002920 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002922 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002924 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002925 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 +02002926 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002927 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002928 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002930 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002931 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 +02002932 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002933 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002934 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935 break;
2936
Radek Krejcid6b76452019-09-03 17:03:03 +02002937 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002938 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002940 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002941 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002942 break;
2943
Radek Krejcid6b76452019-09-03 17:03:03 +02002944 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002945 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002947 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002948 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002950 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002951 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 +02002952 break;
2953 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002954 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955 return LY_EVALID;
2956 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002957 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002959
2960 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2961 if (!act->input.nodetype) {
2962 act->input.nodetype = LYS_INPUT;
2963 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002964 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002965 }
2966 if (!act->output.nodetype) {
2967 act->output.nodetype = LYS_OUTPUT;
2968 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002969 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002970 }
2971
Michal Vasko12ef5362022-09-16 15:13:58 +02002972cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973 return ret;
2974}
2975
Michal Vaskoea5abea2018-09-18 13:10:54 +02002976/**
2977 * @brief Parse the notification statement.
2978 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002979 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002980 * @param[in,out] notifs Notifications to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002981 * @return LY_ERR values.
2982 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002983LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002984parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002986 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002988 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002989 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002990 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991
Radek Krejci2a9fc652021-01-22 17:44:34 +01002992 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002993
2994 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002995 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002996 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002997 notif->nodetype = LYS_NOTIF;
2998 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002999
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003000 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003002 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003003 LY_CHECK_RET(parse_text_field(ctx, notif->dsc, LY_STMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, NULL,
3004 &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003006 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003007 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003008 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003009 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003010 LY_CHECK_RET(parse_text_field(ctx, notif->ref, LY_STMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, NULL,
3011 &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003013 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003014 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 break;
3016
Radek Krejcid6b76452019-09-03 17:03:03 +02003017 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003018 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02003019 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003020 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003021 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003022 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003023 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01003024 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003025 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003026 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003027 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003029 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003030 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003031 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003032 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003033 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003034 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003035 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003036 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003037 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003038 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003039 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040 break;
3041
Radek Krejcid6b76452019-09-03 17:03:03 +02003042 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003043 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003044 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003046 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003047 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003048 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003049 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003050 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003051 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003052 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003053 LY_CHECK_RET(parse_ext(ctx, word, word_len, notif, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003054 break;
3055 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003056 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003057 return LY_EVALID;
3058 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003059 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003061
Michal Vasko12ef5362022-09-16 15:13:58 +02003062cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063 return ret;
3064}
3065
Michal Vaskoea5abea2018-09-18 13:10:54 +02003066/**
3067 * @brief Parse the grouping statement.
3068 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003069 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003070 * @param[in,out] groupings Groupings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003071 * @return LY_ERR values.
3072 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003073LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003074parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003076 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003078 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003079 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003080 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081
Radek Krejci2a9fc652021-01-22 17:44:34 +01003082 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083
3084 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003085 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003086 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003087 grp->nodetype = LYS_GROUPING;
3088 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003090 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003091 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003092 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003093 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 +02003094 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003095 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003096 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 +02003097 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003098 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003099 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003100 break;
3101
Radek Krejcid6b76452019-09-03 17:03:03 +02003102 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003103 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003104 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003105 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003106 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003108 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003109 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003111 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003112 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003114 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003115 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003116 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003117 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003118 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003120 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003121 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003123 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003124 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125 break;
3126
Radek Krejcid6b76452019-09-03 17:03:03 +02003127 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003128 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003130 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003131 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003132 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003134 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003135 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003137 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003138 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003139 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003141 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003142 LY_CHECK_RET(parse_ext(ctx, word, word_len, grp, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143 break;
3144 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003145 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146 return LY_EVALID;
3147 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003148 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003149 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003150
aPiecek63e080d2021-06-29 13:53:28 +02003151 /* store data for collision check */
3152 if (parent) {
3153 assert(ctx->main_ctx);
3154 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3155 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003156
Michal Vasko12ef5362022-09-16 15:13:58 +02003157cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158 return ret;
3159}
3160
Michal Vaskoea5abea2018-09-18 13:10:54 +02003161/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003162 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003163 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003164 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003165 * @param[in,out] augments Augments to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003166 * @return LY_ERR values.
3167 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003168LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003169parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003171 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003173 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003174 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003175 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176
Radek Krejci2a9fc652021-01-22 17:44:34 +01003177 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003178
3179 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003180 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003181 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003182 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003183 aug->nodetype = LYS_AUGMENT;
3184 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003186 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003187 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003188 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003189 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 +02003190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003191 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003192 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003194 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003195 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 +02003196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003197 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003198 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003200 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003201 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202 break;
3203
Radek Krejcid6b76452019-09-03 17:03:03 +02003204 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003205 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003206 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003207 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003208 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003209 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003210 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003211 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003213 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003214 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003216 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003217 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003219 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003220 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003222 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003223 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003225 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003226 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003228 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003229 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 break;
3231
Radek Krejcid6b76452019-09-03 17:03:03 +02003232 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003233 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003234 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003236 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003237 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003238 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003240 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003241 LY_CHECK_RET(parse_ext(ctx, word, word_len, aug, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 break;
3243 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003244 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 return LY_EVALID;
3246 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003247 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003249
Michal Vasko12ef5362022-09-16 15:13:58 +02003250cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 return ret;
3252}
3253
Michal Vaskoea5abea2018-09-18 13:10:54 +02003254/**
3255 * @brief Parse the uses statement.
3256 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003257 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003258 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003259 * @return LY_ERR values.
3260 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003261LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003262parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003263{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003264 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003265 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003266 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003267 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268 struct lysp_node_uses *uses;
3269
David Sedlák60adc092019-08-06 15:57:02 +02003270 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003271 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003273 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003276 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003277 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278
3279 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003280 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003281 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003282 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003283 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 +02003284 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003285 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003286 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003287 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003288 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003289 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 +02003290 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003291 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003292 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003293 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003294 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003295 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003296 break;
3297
Radek Krejcid6b76452019-09-03 17:03:03 +02003298 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003299 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003301 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003302 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003303 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003304 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003305 LY_CHECK_RET(parse_ext(ctx, word, word_len, uses, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306 break;
3307 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003308 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003309 return LY_EVALID;
3310 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003311 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003313
Michal Vasko12ef5362022-09-16 15:13:58 +02003314cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315 return ret;
3316}
3317
Michal Vaskoea5abea2018-09-18 13:10:54 +02003318/**
3319 * @brief Parse the case statement.
3320 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003321 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003322 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003323 * @return LY_ERR values.
3324 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003325LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003326parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003327{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003328 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003330 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003331 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332 struct lysp_node_case *cas;
3333
David Sedlák60adc092019-08-06 15:57:02 +02003334 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003335 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003336 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003337 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003340 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003341 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342
3343 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003344 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003346 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003347 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 +02003348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003349 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003350 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003352 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003353 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 +02003354 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003355 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003356 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003358 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003359 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003360 break;
3361
Radek Krejcid6b76452019-09-03 17:03:03 +02003362 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003363 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003364 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003365 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003366 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003368 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003369 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003371 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003372 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003374 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003375 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003377 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003378 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003380 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003381 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003383 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003384 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003386 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003387 LY_CHECK_RET(parse_ext(ctx, word, word_len, cas, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388 break;
3389 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003390 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003391 return LY_EVALID;
3392 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003393 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003395
3396cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003397 return ret;
3398}
3399
Michal Vaskoea5abea2018-09-18 13:10:54 +02003400/**
3401 * @brief Parse the choice statement.
3402 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003403 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003404 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003405 * @return LY_ERR values.
3406 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003407LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003408parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003410 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003412 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003414 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415
David Sedlák60adc092019-08-06 15:57:02 +02003416 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003417 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003418 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003419 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003422 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003423 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424
3425 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003426 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003429 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003432 LY_CHECK_RET(parse_text_field(ctx, choice->dsc, LY_STMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, NULL,
3433 &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003435 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003436 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003438 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003439 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003441 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003442 LY_CHECK_RET(parse_text_field(ctx, choice->ref, LY_STMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, NULL,
3443 &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003445 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003446 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003448 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003449 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003450 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003451 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003452 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 +01003453 &choice->dflt.flags, &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003454 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
3456
Radek Krejcid6b76452019-09-03 17:03:03 +02003457 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003458 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003459 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003461 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003463 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003464 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003466 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003467 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003468 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003469 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003470 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003471 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003473 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003474 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003476 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003477 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003479 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003480 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003481 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003482 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003483 LY_CHECK_RET(parse_ext(ctx, word, word_len, choice, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484 break;
3485 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003486 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 return LY_EVALID;
3488 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003489 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003491
3492cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 return ret;
3494}
3495
Michal Vaskoea5abea2018-09-18 13:10:54 +02003496/**
3497 * @brief Parse the container statement.
3498 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003499 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003500 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003501 * @return LY_ERR values.
3502 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003503LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003504parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505{
3506 LY_ERR ret = 0;
3507 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003508 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003509 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003510 struct lysp_node_container *cont;
3511
David Sedlák60adc092019-08-06 15:57:02 +02003512 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003513 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003514 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003515 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003516
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003518 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003519 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520
3521 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003522 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003524 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003525 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003527 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003528 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 +02003529 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003530 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003531 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003533 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003534 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 +02003535 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003536 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003537 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003539 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003540 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003542 case LY_STMT_PRESENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003543 LY_CHECK_RET(parse_text_field(ctx, cont->presence, LY_STMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, NULL,
3544 &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545 break;
3546
Radek Krejcid6b76452019-09-03 17:03:03 +02003547 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003548 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003549 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003550 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003551 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003552 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003553 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003554 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003556 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003557 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003559 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003560 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003561 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003562 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003563 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003565 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003566 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003568 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003569 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570 break;
3571
Radek Krejcid6b76452019-09-03 17:03:03 +02003572 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003573 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003575 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003576 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003578 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003579 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003580 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003582 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003583 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003585 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003586 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003587 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003589 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003590 LY_CHECK_RET(parse_ext(ctx, word, word_len, cont, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591 break;
3592 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003593 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 return LY_EVALID;
3595 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003596 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003598
Michal Vasko12ef5362022-09-16 15:13:58 +02003599cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 return ret;
3601}
3602
Michal Vaskoea5abea2018-09-18 13:10:54 +02003603/**
3604 * @brief Parse the list statement.
3605 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003606 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003607 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003608 * @return LY_ERR values.
3609 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003610LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003611parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003612{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003613 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003614 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003615 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003616 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 struct lysp_node_list *list;
3618
David Sedlák60adc092019-08-06 15:57:02 +02003619 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003620 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003622 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623
Michal Vasko7fbc8162018-09-17 10:35:16 +02003624 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003625 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003626 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627
3628 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003629 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003630 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003631 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003632 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003634 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003635 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 +02003636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003637 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003638 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003640 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003641 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 +02003642 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003643 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003644 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003646 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003647 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003648 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003649 case LY_STMT_KEY:
Michal Vasko72c6d642024-02-27 14:59:01 +01003650 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 +02003651 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003652 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003653 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003654 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003655 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003656 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003658 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003659 LY_CHECK_RET(parse_orderedby(ctx, &list->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003661 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003662 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003663 break;
3664
Radek Krejcid6b76452019-09-03 17:03:03 +02003665 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003666 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003667 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003668 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003669 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003670 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003671 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003672 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003674 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003675 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003677 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003678 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003680 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003681 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003683 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003684 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003686 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003687 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688 break;
3689
Radek Krejcid6b76452019-09-03 17:03:03 +02003690 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003691 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003693 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003694 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003695 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003696 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003697 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003698 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003700 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003701 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003702 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003703 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003704 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003705 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003706 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003707 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003708 LY_CHECK_RET(parse_ext(ctx, word, word_len, list, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709 break;
3710 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003711 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003712 return LY_EVALID;
3713 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003714 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003716
Michal Vasko12ef5362022-09-16 15:13:58 +02003717cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003718 return ret;
3719}
3720
Michal Vaskoea5abea2018-09-18 13:10:54 +02003721/**
3722 * @brief Parse the yin-element statement.
3723 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003724 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003725 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003726 * @return LY_ERR values.
3727 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003729parse_yinelement(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003730{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003731 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003733 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003734 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735
Michal Vasko193dacd2022-10-13 08:43:05 +02003736 if (ext->flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003737 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003738 return LY_EVALID;
3739 }
3740
3741 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003742 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743
Radek Krejcif13b87b2020-12-01 22:02:17 +01003744 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003745 ext->flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003746 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003747 ext->flags |= LYS_YINELEM_FALSE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003749 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 free(buf);
3751 return LY_EVALID;
3752 }
3753 free(buf);
3754
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003755 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003756 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003757 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003758 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_YIN_ELEMENT, 0, &ext->exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003759 LY_CHECK_RET(ret);
3760 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003762 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003763 return LY_EVALID;
3764 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003765 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003767
3768cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769 return ret;
3770}
3771
Michal Vaskoea5abea2018-09-18 13:10:54 +02003772/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003773 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003774 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003775 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003776 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003777 * @return LY_ERR values.
3778 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003780parse_argument(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003782 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003784 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003785 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786
Michal Vasko193dacd2022-10-13 08:43:05 +02003787 if (ext->argname) {
David Sedlákb3077192019-06-19 10:55:37 +02003788 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003789 return LY_EVALID;
3790 }
3791
3792 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003793 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko193dacd2022-10-13 08:43:05 +02003794 INSERT_WORD_GOTO(ctx, buf, ext->argname, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003796 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003797 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003798 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003799 LY_CHECK_RET(parse_yinelement(ctx, ext));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003800 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003801 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003802 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_ARGUMENT, 0, &ext->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003803 break;
3804 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003805 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003806 return LY_EVALID;
3807 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003808 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003809 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003810
3811cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 return ret;
3813}
3814
Michal Vaskoea5abea2018-09-18 13:10:54 +02003815/**
3816 * @brief Parse the extension statement.
3817 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003818 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003819 * @param[in,out] extensions Extensions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003820 * @return LY_ERR values.
3821 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003822static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003823parse_extension(struct lysp_yang_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003824{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003825 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003827 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003828 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 struct lysp_ext *ex;
3830
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003831 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832
3833 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003834 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003835 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003836
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003837 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003839 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003840 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 +02003841 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003842 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003843 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 +02003844 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003845 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003846 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003848 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003849 LY_CHECK_RET(parse_argument(ctx, ex));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003851 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003852 LY_CHECK_RET(parse_ext(ctx, word, word_len, ex, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003853 break;
3854 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003855 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003856 return LY_EVALID;
3857 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003858 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003859 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003860
3861cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862 return ret;
3863}
3864
Michal Vaskoea5abea2018-09-18 13:10:54 +02003865/**
3866 * @brief Parse the deviate statement.
3867 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003868 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003869 * @param[in,out] deviates Deviates to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003870 * @return LY_ERR values.
3871 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003872LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003873parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003875 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003876 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003877 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003878 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003879 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003880 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 struct lysp_deviate_add *d_add = NULL;
3882 struct lysp_deviate_rpl *d_rpl = NULL;
3883 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003884 const char **d_units = NULL;
3885 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003886 struct lysp_restr **d_musts = NULL;
3887 uint16_t *d_flags = 0;
3888 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003889
3890 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003891 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892
Radek Krejcif13b87b2020-12-01 22:02:17 +01003893 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003895 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003896 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003897 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003899 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003900 dev_mod = LYS_DEV_DELETE;
3901 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003902 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003903 ret = LY_EVALID;
3904 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003905 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003906
3907 /* create structure */
3908 switch (dev_mod) {
3909 case LYS_DEV_NOT_SUPPORTED:
3910 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003911 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 break;
3913 case LYS_DEV_ADD:
3914 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003915 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003916 d = (struct lysp_deviate *)d_add;
3917 d_units = &d_add->units;
3918 d_uniques = &d_add->uniques;
3919 d_dflts = &d_add->dflts;
3920 d_musts = &d_add->musts;
3921 d_flags = &d_add->flags;
3922 d_min = &d_add->min;
3923 d_max = &d_add->max;
3924 break;
3925 case LYS_DEV_REPLACE:
3926 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003927 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928 d = (struct lysp_deviate *)d_rpl;
3929 d_units = &d_rpl->units;
3930 d_flags = &d_rpl->flags;
3931 d_min = &d_rpl->min;
3932 d_max = &d_rpl->max;
3933 break;
3934 case LYS_DEV_DELETE:
3935 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003936 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 d = (struct lysp_deviate *)d_del;
3938 d_units = &d_del->units;
3939 d_uniques = &d_del->uniques;
3940 d_dflts = &d_del->dflts;
3941 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942 break;
3943 default:
3944 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003945 LOGINT(PARSER_CTX(ctx));
3946 ret = LY_EINT;
3947 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003948 }
3949 d->mod = dev_mod;
3950
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003951 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003952 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003953 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 switch (dev_mod) {
3955 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003956 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003957 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003958 ret = LY_EVALID;
3959 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003961 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962 break;
3963 }
3964 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003965 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003966 switch (dev_mod) {
3967 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02003968 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003969 ret = LY_EVALID;
3970 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003971 case LYS_DEV_REPLACE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003972 ret = parse_text_field(ctx, &d_rpl->dflt, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG,
3973 &d_rpl->dflt.flags, &d->exts);
Michal Vasko193dacd2022-10-13 08:43:05 +02003974 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003975 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 break;
3977 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003978 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 +02003979 break;
3980 }
3981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003982 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983 switch (dev_mod) {
3984 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003985 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003986 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003987 ret = LY_EVALID;
3988 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003989 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003990 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991 break;
3992 }
3993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003994 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003995 switch (dev_mod) {
3996 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003997 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003998 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003999 ret = LY_EVALID;
4000 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004002 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 break;
4004 }
4005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004006 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004007 switch (dev_mod) {
4008 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004009 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004010 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004011 ret = LY_EVALID;
4012 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004014 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004015 break;
4016 }
4017 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004018 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019 switch (dev_mod) {
4020 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004021 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004022 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004023 ret = LY_EVALID;
4024 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004025 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004026 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004027 break;
4028 }
4029 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004030 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004031 switch (dev_mod) {
4032 case LYS_DEV_NOT_SUPPORTED:
4033 case LYS_DEV_ADD:
4034 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004035 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004036 ret = LY_EVALID;
4037 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004038 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004039 if (d_rpl->type) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004040 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004041 ret = LY_EVALID;
4042 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004043 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004044 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004045 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4046 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004047 break;
4048 }
4049 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004050 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004051 switch (dev_mod) {
4052 case LYS_DEV_NOT_SUPPORTED:
4053 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004054 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004055 ret = LY_EVALID;
4056 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004058 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 +02004059 break;
4060 }
4061 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004062 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004063 switch (dev_mod) {
4064 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02004065 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004066 ret = LY_EVALID;
4067 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004068 default:
Michal Vasko72c6d642024-02-27 14:59:01 +01004069 ret = parse_text_field(ctx, *d_units, LY_STMT_UNITS, 0, d_units, Y_STR_ARG, NULL, &d->exts);
4070 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004071 break;
4072 }
4073 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004074 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004075 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 +02004076 break;
4077 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004078 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004079 ret = LY_EVALID;
4080 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004081 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004082 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004083 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004084
4085cleanup:
4086 free(buf);
4087 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004088 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004089 free(d);
4090 } else {
4091 /* insert into siblings */
4092 LY_LIST_INSERT(deviates, d, next);
4093 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004094 return ret;
4095}
4096
Michal Vaskoea5abea2018-09-18 13:10:54 +02004097/**
4098 * @brief Parse the deviation statement.
4099 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004100 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004101 * @param[in,out] deviations Deviations to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004102 * @return LY_ERR values.
4103 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004104LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004105parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004107 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004108 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004109 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004110 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004112 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004113
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004114 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115
4116 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004117 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004118 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004119 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004121 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004122 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004123 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004124 ret = parse_text_field(ctx, dev->dsc, LY_STMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, NULL, &dev->exts);
4125 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004127 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004128 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004130 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004131 ret = parse_text_field(ctx, dev->ref, LY_STMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, NULL, &dev->exts);
4132 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004134 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004135 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 +02004136 break;
4137 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004138 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004139 ret = LY_EVALID;
4140 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004141 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004142 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004143 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004144
Michal Vasko7fbc8162018-09-17 10:35:16 +02004145 /* mandatory substatements */
4146 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004147 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004148 ret = LY_EVALID;
4149 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004150 }
4151
Michal Vasko12ef5362022-09-16 15:13:58 +02004152cleanup:
4153 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004154 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004155 LY_ARRAY_DECREMENT_FREE(*deviations);
4156 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004157 return ret;
4158}
4159
Michal Vaskoea5abea2018-09-18 13:10:54 +02004160/**
4161 * @brief Parse the feature statement.
4162 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004163 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004164 * @param[in,out] features Features to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004165 * @return LY_ERR values.
4166 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004167LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004168parse_feature(struct lysp_yang_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004170 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004172 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004173 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004174 struct lysp_feature *feat;
4175
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004176 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004177
4178 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004179 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004180 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004181
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004182 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004183 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004184 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004185 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 +02004186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004187 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004188 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004189 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004190 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004191 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 +02004192 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004193 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004194 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004196 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004197 LY_CHECK_RET(parse_ext(ctx, word, word_len, feat, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004198 break;
4199 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004200 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004201 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004202 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004203 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004204 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004205
4206cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 return ret;
4208}
4209
Michal Vaskoea5abea2018-09-18 13:10:54 +02004210/**
4211 * @brief Parse the identity statement.
4212 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004213 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004214 * @param[in,out] identities Identities to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004215 * @return LY_ERR values.
4216 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004217LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004218parse_identity(struct lysp_yang_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004220 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004222 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004223 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 struct lysp_ident *ident;
4225
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004226 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227
4228 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004229 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004230 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004231
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004232 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004233 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004234 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004235 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 +02004236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004237 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004238 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004239 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004241 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004242 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 +02004243 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004244 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004245 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004246 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004247 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004248 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
Michal Vasko72c6d642024-02-27 14:59:01 +01004249 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
4250 "Identity can be derived from multiple base identities only in YANG 1.1 modules");
Radek Krejci10113652018-11-14 16:56:50 +01004251 return LY_EVALID;
4252 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004253 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 +02004254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004255 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004256 LY_CHECK_RET(parse_ext(ctx, word, word_len, ident, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004257 break;
4258 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004259 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004260 return LY_EVALID;
4261 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004262 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004263 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004264
4265cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004266 return ret;
4267}
4268
Michal Vaskoea5abea2018-09-18 13:10:54 +02004269/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004270 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004271 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004272 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004273 * @param[in,out] mod Module to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004274 * @return LY_ERR values.
4275 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004276LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004277parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004278{
4279 LY_ERR ret = 0;
4280 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004281 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004282 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004283 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004284 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004285
Michal Vaskoc3781c32020-10-06 14:04:08 +02004286 mod->is_submod = 0;
4287
4288 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004289 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004290 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004291
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004292 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004293
Radek Krejcie3846472018-10-15 15:24:51 +02004294#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004295 if (mod_stmt > SECTION) {\
4296 LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;\
4297 } mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004298
Michal Vasko7fbc8162018-09-17 10:35:16 +02004299 switch (kw) {
4300 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004301 case LY_STMT_NAMESPACE:
4302 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004303 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4304 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004305 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004306 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004307 break;
4308 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004309 case LY_STMT_INCLUDE:
4310 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004311 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004312 break;
4313 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004314 case LY_STMT_ORGANIZATION:
4315 case LY_STMT_CONTACT:
4316 case LY_STMT_DESCRIPTION:
4317 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004318 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004319 break;
4320
4321 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004322 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004323 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004324 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004325 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004326 case LY_STMT_ANYDATA:
4327 case LY_STMT_ANYXML:
4328 case LY_STMT_AUGMENT:
4329 case LY_STMT_CHOICE:
4330 case LY_STMT_CONTAINER:
4331 case LY_STMT_DEVIATION:
4332 case LY_STMT_EXTENSION:
4333 case LY_STMT_FEATURE:
4334 case LY_STMT_GROUPING:
4335 case LY_STMT_IDENTITY:
4336 case LY_STMT_LEAF:
4337 case LY_STMT_LEAF_LIST:
4338 case LY_STMT_LIST:
4339 case LY_STMT_NOTIFICATION:
4340 case LY_STMT_RPC:
4341 case LY_STMT_TYPEDEF:
4342 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004343 mod_stmt = Y_MOD_BODY;
4344 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004345 case LY_STMT_EXTENSION_INSTANCE:
4346 /* no place in the statement order defined */
4347 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004348 default:
4349 /* error handled in the next switch */
4350 break;
4351 }
Radek Krejcie3846472018-10-15 15:24:51 +02004352#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004353
Radek Krejcie3846472018-10-15 15:24:51 +02004354 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004355 switch (kw) {
4356 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004357 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004358 LY_CHECK_RET(parse_yangversion(ctx, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_NAMESPACE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004361 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 +02004362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01004364 LY_CHECK_RET(parse_text_field(ctx, mod->mod->prefix, LY_STMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG,
4365 NULL, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004366 break;
4367
4368 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004370 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004373 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004374 break;
4375
4376 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004377 case LY_STMT_ORGANIZATION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004378 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 +02004379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004380 case LY_STMT_CONTACT:
Michal Vasko72c6d642024-02-27 14:59:01 +01004381 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 +02004382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004383 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004384 LY_CHECK_RET(parse_text_field(ctx, mod->mod->dsc, LY_STMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, NULL,
4385 &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004388 LY_CHECK_RET(parse_text_field(ctx, mod->mod->ref, LY_STMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, NULL,
4389 &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004390 break;
4391
4392 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004393 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004394 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004395 break;
4396
4397 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004398 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004399 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004400 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004401 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004402 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004404 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004405 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004407 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004408 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004410 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004411 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004413 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004414 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004416 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004417 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004419 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004420 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004421 break;
4422
Radek Krejcid6b76452019-09-03 17:03:03 +02004423 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004424 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004425 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004426 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004427 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004429 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004430 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004432 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004433 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004435 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004436 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004438 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004439 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004441 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004442 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004444 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004445 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004447 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004448 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004450 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004451 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004452 break;
4453
4454 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004455 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004456 return LY_EVALID;
4457 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004458 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004459 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004460
Michal Vasko7fbc8162018-09-17 10:35:16 +02004461 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004462 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004463 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004464 return LY_EVALID;
4465 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004466 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004467 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004468 }
4469
Radek Krejcie9e987e2018-10-31 12:50:27 +01004470 /* submodules share the namespace with the module names, so there must not be
4471 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004472 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004473 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004474 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004475 return LY_EVALID;
4476 }
4477
Michal Vasko12ef5362022-09-16 15:13:58 +02004478cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004479 return ret;
4480}
4481
4482/**
4483 * @brief Parse submodule substatements.
4484 *
4485 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004486 * @param[out] submod Parsed submodule structure.
4487 *
4488 * @return LY_ERR values.
4489 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004490LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004491parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004492{
4493 LY_ERR ret = 0;
4494 char *buf, *word;
4495 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004496 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004497 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004498 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004499
Michal Vaskoc3781c32020-10-06 14:04:08 +02004500 submod->is_submod = 1;
4501
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004502 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004503 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004504 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004505
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004506 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004507
4508#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004509 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 +01004510
4511 switch (kw) {
4512 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004513 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004514 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4515 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004516 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004517 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4518 break;
4519 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004520 case LY_STMT_INCLUDE:
4521 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004522 CHECK_ORDER(Y_MOD_LINKAGE);
4523 break;
4524 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004525 case LY_STMT_ORGANIZATION:
4526 case LY_STMT_CONTACT:
4527 case LY_STMT_DESCRIPTION:
4528 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004529 CHECK_ORDER(Y_MOD_META);
4530 break;
4531
4532 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004533 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004534 CHECK_ORDER(Y_MOD_REVISION);
4535 break;
4536 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004537 case LY_STMT_ANYDATA:
4538 case LY_STMT_ANYXML:
4539 case LY_STMT_AUGMENT:
4540 case LY_STMT_CHOICE:
4541 case LY_STMT_CONTAINER:
4542 case LY_STMT_DEVIATION:
4543 case LY_STMT_EXTENSION:
4544 case LY_STMT_FEATURE:
4545 case LY_STMT_GROUPING:
4546 case LY_STMT_IDENTITY:
4547 case LY_STMT_LEAF:
4548 case LY_STMT_LEAF_LIST:
4549 case LY_STMT_LIST:
4550 case LY_STMT_NOTIFICATION:
4551 case LY_STMT_RPC:
4552 case LY_STMT_TYPEDEF:
4553 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004554 mod_stmt = Y_MOD_BODY;
4555 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004556 case LY_STMT_EXTENSION_INSTANCE:
4557 /* no place in the statement order defined */
4558 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004559 default:
4560 /* error handled in the next switch */
4561 break;
4562 }
4563#undef CHECK_ORDER
4564
4565 prev_kw = kw;
4566 switch (kw) {
4567 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004568 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004569 LY_CHECK_RET(parse_yangversion(ctx, (struct lysp_module *)submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004570 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004571 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02004572 LY_CHECK_RET(parse_belongsto(ctx, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004573 break;
4574
4575 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004576 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004577 if (submod->version == LYS_VERSION_1_1) {
4578 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4579 submod->name);
4580 }
Radek Krejci33090f92020-12-17 20:12:46 +01004581 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004582 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004583 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004584 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004585 break;
4586
4587 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004588 case LY_STMT_ORGANIZATION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004589 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 +01004590 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004591 case LY_STMT_CONTACT:
Michal Vasko72c6d642024-02-27 14:59:01 +01004592 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 +01004593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004594 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004595 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 +01004596 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004597 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004598 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 +01004599 break;
4600
4601 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004602 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004603 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004604 break;
4605
4606 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004607 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004608 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004609 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004610 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004611 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004613 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004614 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004616 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004617 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004619 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004620 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004622 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004623 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004625 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004626 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004628 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004629 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004630 break;
4631
Radek Krejcid6b76452019-09-03 17:03:03 +02004632 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004633 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004634 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004635 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004636 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004638 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004639 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004640 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004641 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004642 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004643 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004644 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004645 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004647 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004648 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004650 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004651 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004653 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004654 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004655 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004656 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004657 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004658 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004659 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004660 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004661 break;
4662
4663 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004664 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004665 return LY_EVALID;
4666 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004667 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004668 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004669
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004670 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004671 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004672 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004673 return LY_EVALID;
4674 }
4675
4676 /* submodules share the namespace with the module names, so there must not be
4677 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004678 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004679 /* main modules may have different revisions */
4680 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004681 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004682 return LY_EVALID;
4683 }
4684
Michal Vasko12ef5362022-09-16 15:13:58 +02004685cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004686 return ret;
4687}
4688
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004689/**
4690 * @brief Skip any redundant characters, namely whitespaces and comments.
4691 *
4692 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004693 * @return LY_SUCCESS on success.
4694 * @return LY_EVALID on invalid comment.
4695 */
4696static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004697skip_redundant_chars(struct lysp_yang_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004698{
4699 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004700 while (ctx->in->current[0]) {
4701 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004702 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004703 ly_in_skip(ctx->in, 2);
4704 LY_CHECK_RET(skip_comment(ctx, 1));
4705 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004706 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004707 ly_in_skip(ctx->in, 2);
4708 LY_CHECK_RET(skip_comment(ctx, 2));
4709 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004710 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004711 if (ctx->in->current[0] == '\n') {
4712 LY_IN_NEW_LINE(ctx->in);
4713 }
Radek Krejci33090f92020-12-17 20:12:46 +01004714 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004715 } else {
4716 break;
4717 }
4718 }
4719
4720 return LY_SUCCESS;
4721}
4722
Radek Krejcid4557c62018-09-17 11:42:09 +02004723LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004724yang_parse_submodule(struct lysp_yang_ctx **context, struct ly_ctx *ly_ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004725 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004726{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004727 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004728 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004729 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004730 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004731 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004732 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004733
aPiecek56be92a2021-07-01 07:18:10 +02004734 assert(context && ly_ctx && main_ctx && in && submod);
4735
David Sedlák1b623122019-08-05 15:27:49 +02004736 /* create context */
4737 *context = calloc(1, sizeof **context);
4738 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004739 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004740 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004741 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004742
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004743 mod_p = calloc(1, sizeof *mod_p);
4744 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004745 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004746 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004747
4748 /* use main context parsed mods adding the current one */
4749 (*context)->parsed_mods = main_ctx->parsed_mods;
4750 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004751
Michal Vasko7a266772024-01-23 11:02:38 +01004752 ly_log_location(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004753
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004754 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004755 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004756 LY_CHECK_GOTO(ret, cleanup);
4757
Michal Vasko7fbc8162018-09-17 10:35:16 +02004758 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004759 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004760 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004761
Radek Krejcid6b76452019-09-03 17:03:03 +02004762 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004763 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004764 ret = LY_EINVAL;
4765 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004766 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004767 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004768 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004769 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004770 }
4771
Michal Vasko7fbc8162018-09-17 10:35:16 +02004772 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004773 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004774 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004775
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004776 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004777 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004778 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004779 if (in->current[0]) {
4780 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004781 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004782 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004783 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004784
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004785 mod_p->parsing = 0;
4786 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004787
Radek Krejcibbe09a92018-11-08 09:36:54 +01004788cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +01004789 ly_log_location_revert(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004790 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004791 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004792 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004793 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004794 }
4795
4796 return ret;
4797}
4798
4799LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004800yang_parse_module(struct lysp_yang_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004801{
4802 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004803 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004804 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004805 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004806 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004807 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004808
David Sedlák1b623122019-08-05 15:27:49 +02004809 /* create context */
4810 *context = calloc(1, sizeof **context);
4811 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004812 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004813 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004814 (*context)->in = in;
Michal Vaskod0625d72022-10-06 15:02:50 +02004815 (*context)->main_ctx = (struct lysp_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004816
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004817 mod_p = calloc(1, sizeof *mod_p);
4818 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4819 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004820 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004821
Michal Vasko7a266772024-01-23 11:02:38 +01004822 ly_log_location(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004823
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004824 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004825 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004826 LY_CHECK_GOTO(ret, cleanup);
4827
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004828 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004829 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004830 LY_CHECK_GOTO(ret, cleanup);
4831
Radek Krejcid6b76452019-09-03 17:03:03 +02004832 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004833 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 +01004834 ret = LY_EINVAL;
4835 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004836 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004837 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004838 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004839 goto cleanup;
4840 }
4841
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004842 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004843 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004844 LY_CHECK_GOTO(ret, cleanup);
4845
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004846 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004847 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004848 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004849 if (in->current[0]) {
4850 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004851 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004852 goto cleanup;
4853 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004854
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004855 mod->parsed = mod_p;
4856
4857cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +01004858 ly_log_location_revert(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004859 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004860 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004861 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004862 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004863 }
4864
Michal Vasko7fbc8162018-09-17 10:35:16 +02004865 return ret;
4866}