blob: 49ea8b7bf3bd0d2494d7adb8d524c03a5d694558 [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 Vaskoc636ea42022-09-16 10:20:31 +02006 * Copyright (c) 2018 - 2022 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
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "log.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 }
424 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200425 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200426 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200427 /* we will be removing the indents so we need our own buffer */
428 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200429
430 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200431 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200432
Radek Krejciff13cd12019-10-25 15:34:24 +0200433 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200434 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200435 }
436
437 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100438 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 +0200439
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200440 /* reset context indentation counter for possible string after this one */
441 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200442 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200443 break;
444 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200445 /* first non-whitespace character, stop eating indentation */
446 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200447
448 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100449 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 +0200450 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200451 break;
452 }
453 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100454 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200455 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100456 c = ctx->in->current;
457 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200458 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100459 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100460 /* fix false newline count in buf_store_char() */
461 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200462 break;
463 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100464 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200465 break;
466 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200468 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200469 break;
470 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200471 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100472 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200473 return LY_EVALID;
474 }
475
476 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100477 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 +0200478
Radek Krejcif13b87b2020-12-01 22:02:17 +0100479 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100480 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200481 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100482 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100483 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200484 case '+':
485 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100486 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200487 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200488 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200489 case '\r':
490 if (ctx->in->current[1] != '\n') {
491 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
492 return LY_EVALID;
493 }
494 MOVE_INPUT(ctx, 1);
495 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200496 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100497 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100498 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200499 case ' ':
500 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200501 /* just skip */
502 break;
503 default:
504 /* string is finished */
505 goto string_end;
506 }
Radek Krejci33090f92020-12-17 20:12:46 +0100507 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200508 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100509 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100510 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200511 case '\r':
512 if (ctx->in->current[1] != '\n') {
513 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
514 return LY_EVALID;
515 }
516 MOVE_INPUT(ctx, 1);
517 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200518 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100519 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100520 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200521 case ' ':
522 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200523 /* skip */
524 break;
525 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100526 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200527 break;
528 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100529 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200530 break;
531 default:
532 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200533 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200534 return LY_EVALID;
535 }
Radek Krejci33090f92020-12-17 20:12:46 +0100536 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537 break;
538 default:
539 return LY_EINT;
540 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200541 }
542
543string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200544 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200545 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200546 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200547 return LY_EVALID;
548 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200549 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100550
551#undef STRING_ENDED
552#undef STRING_SINGLE_QUOTED
553#undef STRING_DOUBLE_QUOTED
554#undef STRING_DOUBLE_QUOTED_ESCAPED
555#undef STRING_PAUSED_NEXTSTRING
556#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200557}
558
Michal Vaskoea5abea2018-09-18 13:10:54 +0200559/**
560 * @brief Get another YANG string from the raw data.
561 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200562 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200563 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200564 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
565 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200566 * @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 +0200567 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
568 * set to NULL. Otherwise equal to \p word_p.
569 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200570 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200571 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200572LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200573get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200574 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200575{
Michal Vaskob68ea142021-04-26 13:46:00 +0200576 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200577 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200578 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200579
Michal Vasko7fbc8162018-09-17 10:35:16 +0200580 /* word buffer - dynamically allocated */
581 *word_b = NULL;
582
583 /* word pointer - just a pointer to data */
584 *word_p = NULL;
585
586 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100587 while (ctx->in->current[0]) {
588 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200589 case '\'':
590 case '\"':
591 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200592 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100593 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200594 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200595 ret = LY_EVALID;
596 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200597 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200598 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100599 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200600 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200601 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200602 if (!*word_p) {
603 /* do not return NULL word */
604 *word_p = "";
605 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200606 goto str_end;
607 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100608 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200609 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100610 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200611 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100612 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200613 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100614 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200615 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200616 } else {
617 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200618 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 +0200619 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200620 break;
621 case ' ':
622 if (*word_len) {
623 /* word is finished */
624 goto str_end;
625 }
Radek Krejci33090f92020-12-17 20:12:46 +0100626 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200627 break;
628 case '\t':
629 if (*word_len) {
630 /* word is finished */
631 goto str_end;
632 }
633 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100634 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200635
Radek Krejci33090f92020-12-17 20:12:46 +0100636 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200637 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200638 case '\r':
639 if (ctx->in->current[1] != '\n') {
640 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200641 ret = LY_EVALID;
642 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200643 }
644 MOVE_INPUT(ctx, 1);
645 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200646 case '\n':
647 if (*word_len) {
648 /* word is finished */
649 goto str_end;
650 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100651 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100652 MOVE_INPUT(ctx, 1);
653
Michal Vasko7fbc8162018-09-17 10:35:16 +0200654 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200655 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200656 break;
657 case ';':
658 case '{':
659 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
660 /* word is finished */
661 goto str_end;
662 }
663
Radek Krejci33090f92020-12-17 20:12:46 +0100664 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200665 ret = LY_EVALID;
666 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200667 case '}':
668 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100669 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200670 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200671 ret = LY_EVALID;
672 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200673 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200674 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 +0200675 break;
676 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200677 }
678
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200679 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200680 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200681 ret = LY_EVALID;
682 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200683
Michal Vasko7fbc8162018-09-17 10:35:16 +0200684str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200685 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200687 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200688 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200689 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200690 *word_p = *word_b;
691 }
692
693 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200694
695error:
696 free(*word_b);
697 *word_b = NULL;
698 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699}
700
Michal Vaskoea5abea2018-09-18 13:10:54 +0200701/**
702 * @brief Get another YANG keyword from the raw data.
703 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200704 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200705 * @param[out] kw YANG keyword read.
706 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
707 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200708 * @return LY_ERR values.
709 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200710LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200711get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200712{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200713 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200714 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200715 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200716
717 if (word_p) {
718 *word_p = NULL;
719 *word_len = 0;
720 }
721
722 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100723 while (ctx->in->current[0]) {
724 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200725 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100726 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200727 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100728 MOVE_INPUT(ctx, 2);
729 LY_CHECK_RET(skip_comment(ctx, 1));
730 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200731 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100732 MOVE_INPUT(ctx, 2);
733 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200734 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200735 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200736 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200737 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200738 }
Radek Krejci13028282019-06-11 14:56:48 +0200739 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200740 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200741 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100742 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200743 ctx->indent = 0;
744 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200745 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200746 /* skip whitespaces (optsep) */
747 ++ctx->indent;
748 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200749 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200750 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100751 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100753 case '\r':
754 /* possible CRLF endline */
755 if (ctx->in->current[1] == '\n') {
756 break;
757 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100758 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 default:
760 /* either a keyword start or an invalid character */
761 goto keyword_start;
762 }
763
Radek Krejci33090f92020-12-17 20:12:46 +0100764 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200765 }
766
767keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100768 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100769 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200770
aPiecek93582ed2021-05-25 14:49:06 +0200771 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
772 goto success;
773 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
774 ctx->depth++;
775 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100776 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200777 return LY_EINVAL;
778 }
779 goto success;
780 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
781 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200782 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200783 }
784
Radek Krejcid6b76452019-09-03 17:03:03 +0200785 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200786 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100787 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200788 case '\r':
789 if (ctx->in->current[1] != '\n') {
790 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
791 return LY_EVALID;
792 }
793 MOVE_INPUT(ctx, 1);
794 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200795 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200796 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200797 case ' ':
798 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200799 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200800 case ':':
801 /* keyword is not actually a keyword, but prefix of an extension.
802 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
803 * and we will be checking the keyword (extension instance) itself */
804 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100805 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200806 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200807 case '{':
Michal Vasko946b70c2023-07-19 08:57:31 +0200808 case ';':
809 /* allowed only for input and output statements which are without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200810 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200811 break;
812 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100813 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200814 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100815 MOVE_INPUT(ctx, 1);
816 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200817 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818 return LY_EVALID;
819 }
820 } else {
821 /* still can be an extension */
822 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200823
Radek Krejci156ccaf2018-10-15 15:49:17 +0200824extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100825 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200826 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
827 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200828 uint32_t c = 0;
829
Radek Krejci33090f92020-12-17 20:12:46 +0100830 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
831 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200832 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200833 /* check character validity */
Michal Vaskod0625d72022-10-06 15:02:50 +0200834 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100835 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200836 }
Radek Krejci33090f92020-12-17 20:12:46 +0100837 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200838 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200839 return LY_EVALID;
840 }
841
842 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200843 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100844 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845 return LY_EVALID;
846 }
847
Radek Krejcid6b76452019-09-03 17:03:03 +0200848 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200849 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200850
Radek Krejci626df482018-10-11 15:06:31 +0200851success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200852 if (word_p) {
853 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100854 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200855 }
856
857 return LY_SUCCESS;
858}
859
Michal Vaskoea5abea2018-09-18 13:10:54 +0200860/**
861 * @brief Parse extension instance substatements.
862 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200863 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200864 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200865 * @param[in] word Extension instance substatement name (keyword).
866 * @param[in] word_len Extension instance substatement name length.
867 * @param[in,out] child Children of this extension instance to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200868 * @return LY_ERR values.
869 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200871parse_ext_substmt(struct lysp_yang_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200872 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200873{
874 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100875 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200876 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200877 struct lysp_stmt *stmt, *par_child;
878
879 stmt = calloc(1, sizeof *stmt);
880 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
881
Radek Krejcibb9b1982019-04-08 14:24:59 +0200882 /* insert into parent statements */
883 if (!*child) {
884 *child = stmt;
885 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200886 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200887 par_child->next = stmt;
888 }
889
Michal Vaskofc2cd072021-02-24 13:17:17 +0100890 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200891 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892
893 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100894 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200895 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200896 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200897 }
898
Radek Krejci8df109d2021-04-23 12:19:08 +0200899 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100900 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100901 stmt->kw = kw;
902
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200903 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
904 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
905 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200906 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200907
908cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200909 return ret;
910}
911
Michal Vaskoea5abea2018-09-18 13:10:54 +0200912/**
913 * @brief Parse extension instance.
914 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200915 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200916 * @param[in] ext_name Extension instance substatement name (keyword).
917 * @param[in] ext_name_len Extension instance substatement name length.
Michal Vasko193dacd2022-10-13 08:43:05 +0200918 * @param[in] parent Current statement parent.
919 * @param[in] parent_stmt Type of @p parent statement.
920 * @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 +0200921 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200922 * @return LY_ERR values.
923 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200924static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200925parse_ext(struct lysp_yang_ctx *ctx, const char *ext_name, size_t ext_name_len, const void *parent,
926 enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100928 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200929 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200930 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200932 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200934 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200935
Michal Vaskofc2cd072021-02-24 13:17:17 +0100936 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200937 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%.*s\" without the mandatory prefix.",
938 (int)ext_name_len, ext_name);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100939 return LY_EVALID;
940 }
941
942 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200943 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200944
945 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100946 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200947 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200948 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200949 }
950
Michal Vaskofc2cd072021-02-24 13:17:17 +0100951 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200952 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200953 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100954 e->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200955 e->parent = (void *)parent;
956 e->parent_stmt = parent_stmt;
957 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100958
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200959 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
960 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
961 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200962 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200963
964cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200965 return ret;
966}
967
Michal Vaskoea5abea2018-09-18 13:10:54 +0200968/**
969 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
970 * description, etc...
971 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200972 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +0200973 * @param[in] parent Current statement parent.
974 * @param[in] parent_stmt Type of statement in @p value.
975 * @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 +0200976 * @param[in,out] value Place to store the parsed value.
977 * @param[in] arg Type of the YANG keyword argument (of the value).
978 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200979 * @return LY_ERR values.
980 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200982parse_text_field(struct lysp_yang_ctx *ctx, const void *parent, enum ly_stmt parent_stmt, uint32_t parent_stmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200983 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100985 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200986 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200987 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200988 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989
990 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200991 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992 return LY_EVALID;
993 }
994
995 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100996 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200997
998 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +0200999 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001000
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001001 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001002 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001003 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001004 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent, parent_stmt, parent_stmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001005 break;
1006 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001007 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008 return LY_EVALID;
1009 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001010 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001012
1013cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014 return ret;
1015}
1016
Michal Vaskoea5abea2018-09-18 13:10:54 +02001017/**
1018 * @brief Parse the yang-version statement.
1019 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001020 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001021 * @param[in,out] mod Module to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001022 * @return LY_ERR values.
1023 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001024static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001025parse_yangversion(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001026{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001027 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001028 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001029 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001030 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031
Michal Vasko193dacd2022-10-13 08:43:05 +02001032 if (mod->version) {
David Sedlákb3077192019-06-19 10:55:37 +02001033 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001034 return LY_EVALID;
1035 }
1036
1037 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001038 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039
Radek Krejci96e48da2020-09-04 13:18:06 +02001040 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001041 mod->version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001042 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001043 mod->version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001044 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001045 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001046 free(buf);
1047 return LY_EVALID;
1048 }
1049 free(buf);
1050
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001051 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001052 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001053 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001054 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_YANG_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055 break;
1056 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001057 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 return LY_EVALID;
1059 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001060 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001062
1063cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064 return ret;
1065}
1066
Michal Vaskoea5abea2018-09-18 13:10:54 +02001067/**
1068 * @brief Parse the belongs-to statement.
1069 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001070 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001071 * @param[in,out] submod Submodule to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001072 * @return LY_ERR values.
1073 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001074static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001075parse_belongsto(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001076{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001077 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001079 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001080 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081
Michal Vasko193dacd2022-10-13 08:43:05 +02001082 if (submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001083 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001084 return LY_EVALID;
1085 }
1086
Michal Vaskoc3781c32020-10-06 14:04:08 +02001087 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001088 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001089 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001090 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 +01001091 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001092 free(buf);
1093 return LY_EVALID;
1094 }
1095 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001096
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001097 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001098 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001099 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001100 LY_CHECK_RET(parse_text_field(ctx, submod->prefix, LY_STMT_PREFIX, 0, &submod->prefix, Y_IDENTIF_ARG, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001102 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001103 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_BELONGS_TO, 0, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104 break;
1105 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001106 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 return LY_EVALID;
1108 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001109 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001111
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112 /* mandatory substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +02001113 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001114 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001115 return LY_EVALID;
1116 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001117
1118cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001119 return ret;
1120}
1121
Michal Vaskoea5abea2018-09-18 13:10:54 +02001122/**
1123 * @brief Parse the revision-date statement.
1124 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001125 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001126 * @param[in,out] rev Buffer to store the parsed value in.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001127 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001128 * @return LY_ERR values.
1129 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001131parse_revisiondate(struct lysp_yang_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001133 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001134 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001135 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001136 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137
1138 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001139 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001140 return LY_EVALID;
1141 }
1142
1143 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001144 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145
1146 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001147 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001148 free(buf);
1149 return LY_EVALID;
1150 }
1151
1152 /* store value and spend buf if allocated */
1153 strncpy(rev, word, word_len);
1154 free(buf);
1155
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001156 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001158 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001159 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160 break;
1161 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001162 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001163 return LY_EVALID;
1164 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001165 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001166 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001167
1168cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169 return ret;
1170}
1171
Michal Vaskoea5abea2018-09-18 13:10:54 +02001172/**
1173 * @brief Parse the include statement.
1174 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001175 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001176 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001177 * @param[in,out] includes Parsed includes to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001178 * @return LY_ERR values.
1179 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001181parse_include(struct lysp_yang_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182{
Radek Krejcid33273d2018-10-25 14:55:52 +02001183 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001185 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001186 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001187 struct lysp_include *inc;
1188
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001189 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190
1191 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001192 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001193
Michal Vasko12ef5362022-09-16 15:13:58 +02001194 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001195
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001196 /* submodules share the namespace with the module names, so there must not be
1197 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001198 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001199 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001200 return LY_EVALID;
1201 }
1202
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001203 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001204 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001205 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001206 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001207 LY_CHECK_RET(parse_text_field(ctx, inc->dsc, LY_STMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001209 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001210 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001211 LY_CHECK_RET(parse_text_field(ctx, inc->ref, LY_STMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001213 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001214 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001216 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001217 LY_CHECK_RET(parse_ext(ctx, word, word_len, inc, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218 break;
1219 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001220 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221 return LY_EVALID;
1222 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001223 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001224 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001225
1226cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227 return ret;
1228}
1229
Michal Vaskoea5abea2018-09-18 13:10:54 +02001230/**
1231 * @brief Parse the import statement.
1232 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001233 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001234 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001235 * @param[in,out] imports Parsed imports to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001236 * @return LY_ERR values.
1237 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001238static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001239parse_import(struct lysp_yang_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001241 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001243 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001244 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 struct lysp_import *imp;
1246
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001247 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248
1249 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001250 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001251 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001253 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001255 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001256 LY_CHECK_RET(parse_text_field(ctx, imp->prefix, LY_STMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskod0625d72022-10-06 15:02:50 +02001257 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001258 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001259 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001260 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001261 LY_CHECK_RET(parse_text_field(ctx, imp->dsc, LY_STMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001262 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001263 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001264 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001265 LY_CHECK_RET(parse_text_field(ctx, imp->ref, LY_STMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001267 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001268 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001269 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001270 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001271 LY_CHECK_RET(parse_ext(ctx, word, word_len, imp, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001272 break;
1273 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001274 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001275 return LY_EVALID;
1276 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001277 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001278 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001279
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001281 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001282
Michal Vasko12ef5362022-09-16 15:13:58 +02001283cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001284 return ret;
1285}
1286
Michal Vaskoea5abea2018-09-18 13:10:54 +02001287/**
1288 * @brief Parse the revision statement.
1289 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001290 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001291 * @param[in,out] revs Parsed revisions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001292 * @return LY_ERR values.
1293 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001294static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001295parse_revision(struct lysp_yang_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001297 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001298 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001299 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001300 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001301 struct lysp_revision *rev;
1302
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001303 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001304
1305 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001306 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001307
1308 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001309 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001310 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001311 return LY_EVALID;
1312 }
1313
Radek Krejcib7db73a2018-10-24 14:18:40 +02001314 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315 free(buf);
1316
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001317 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001319 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001320 LY_CHECK_RET(parse_text_field(ctx, rev->dsc, LY_STMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001322 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001323 LY_CHECK_RET(parse_text_field(ctx, rev->ref, LY_STMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001325 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001326 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327 break;
1328 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001329 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330 return LY_EVALID;
1331 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001332 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001333 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001334
1335cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336 return ret;
1337}
1338
Michal Vaskoea5abea2018-09-18 13:10:54 +02001339/**
1340 * @brief Parse a generic text field that can have more instances such as base.
1341 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001342 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001343 * @param[in] parent Current statement parent.
1344 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001345 * @param[in,out] texts Parsed values to add to.
1346 * @param[in] arg Type of the expected argument.
1347 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001348 * @return LY_ERR values.
1349 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001350static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001351parse_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 +02001352 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001354 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001355 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001356 const char **item;
1357 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001358 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001359
1360 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001361 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362
1363 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001364 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001365
Michal Vasko12ef5362022-09-16 15:13:58 +02001366 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001367 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001369 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001370 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 +02001371 break;
1372 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001373 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374 return LY_EVALID;
1375 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001376 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001377 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001378
1379cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001380 return ret;
1381}
1382
Michal Vaskoea5abea2018-09-18 13:10:54 +02001383/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001384 * @brief Parse a generic text field that can have more instances such as base.
1385 *
1386 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001387 * @param[in] parent_stmt Type of statement stored in @p qnames.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001388 * @param[in,out] qnames Parsed qnames to add to.
1389 * @param[in] arg Type of the expected argument.
1390 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001391 * @return LY_ERR values.
1392 */
1393static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001394parse_qnames(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_qname **qnames, enum yang_arg arg,
1395 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +02001396{
1397 LY_ERR ret = LY_SUCCESS;
1398 char *buf, *word;
1399 struct lysp_qname *item;
1400 size_t word_len;
1401 enum ly_stmt kw;
1402
1403 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001404 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001405
1406 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001407 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001408
Michal Vasko12ef5362022-09-16 15:13:58 +02001409 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001410 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001411 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001412 switch (kw) {
1413 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001414 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 +02001415 break;
1416 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001417 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001418 return LY_EVALID;
1419 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001420 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001421 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001422
1423cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001424 return ret;
1425}
1426
1427/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001428 * @brief Parse the config statement.
1429 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001430 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001431 * @param[in,out] flags Flags to add to.
1432 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001433 * @return LY_ERR values.
1434 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001436parse_config(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001438 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001439 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001440 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001441 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001442
1443 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001444 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001445 return LY_EVALID;
1446 }
1447
1448 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001449 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450
Radek Krejcif13b87b2020-12-01 22:02:17 +01001451 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001452 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001453 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001454 *flags |= LYS_CONFIG_R;
1455 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001456 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001457 free(buf);
1458 return LY_EVALID;
1459 }
1460 free(buf);
1461
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001462 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001464 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001465 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466 break;
1467 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001468 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001469 return LY_EVALID;
1470 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001471 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001473
1474cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475 return ret;
1476}
1477
Michal Vaskoea5abea2018-09-18 13:10:54 +02001478/**
1479 * @brief Parse the mandatory statement.
1480 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001481 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001482 * @param[in,out] flags Flags to add to.
1483 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001484 * @return LY_ERR values.
1485 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001487parse_mandatory(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001489 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001490 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001491 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001492 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493
1494 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001495 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 return LY_EVALID;
1497 }
1498
1499 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001500 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501
Radek Krejcif13b87b2020-12-01 22:02:17 +01001502 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001504 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505 *flags |= LYS_MAND_FALSE;
1506 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001507 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 free(buf);
1509 return LY_EVALID;
1510 }
1511 free(buf);
1512
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001513 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001515 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001516 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 break;
1518 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001519 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 return LY_EVALID;
1521 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001522 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001523 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001524
1525cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526 return ret;
1527}
1528
Michal Vaskoea5abea2018-09-18 13:10:54 +02001529/**
1530 * @brief Parse a restriction such as range or length.
1531 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001532 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001533 * @param[in] restr_kw Type of this particular restriction.
1534 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001535 * @return LY_ERR values.
1536 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001538parse_restr(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001539{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001540 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001541 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001542 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001543 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544
1545 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001546 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547
Michal Vasko193dacd2022-10-13 08:43:05 +02001548 CHECK_NONEMPTY(ctx, word_len, lyplg_ext_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001549 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001550 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001551 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001553 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001554 LY_CHECK_RET(parse_text_field(ctx, restr->dsc, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001556 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001557 LY_CHECK_RET(parse_text_field(ctx, restr->ref, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001559 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02001560 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG,
1561 &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001562 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001563 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001564 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001566 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001567 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 break;
1569 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001570 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 return LY_EVALID;
1572 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001573 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001575
1576cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 return ret;
1578}
1579
Michal Vaskoea5abea2018-09-18 13:10:54 +02001580/**
1581 * @brief Parse a restriction that can have more instances such as must.
1582 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001583 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001584 * @param[in] restr_kw Type of this particular restriction.
1585 * @param[in,out] restrs Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001586 * @return LY_ERR values.
1587 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001589parse_restrs(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001590{
1591 struct lysp_restr *restr;
1592
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001593 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001594 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001595}
1596
Michal Vaskoea5abea2018-09-18 13:10:54 +02001597/**
1598 * @brief Parse the status statement.
1599 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001600 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001601 * @param[in,out] flags Flags to add to.
1602 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001603 * @return LY_ERR values.
1604 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001605static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001606parse_status(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001608 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001609 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001610 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001611 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612
1613 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001614 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 return LY_EVALID;
1616 }
1617
1618 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001619 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001620
Radek Krejcif13b87b2020-12-01 22:02:17 +01001621 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001623 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001625 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001626 *flags |= LYS_STATUS_OBSLT;
1627 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001628 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001629 free(buf);
1630 return LY_EVALID;
1631 }
1632 free(buf);
1633
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001634 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001635 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001636 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001637 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 break;
1639 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001640 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641 return LY_EVALID;
1642 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001643 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001645
1646cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647 return ret;
1648}
1649
Michal Vaskoea5abea2018-09-18 13:10:54 +02001650/**
1651 * @brief Parse the when statement.
1652 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001653 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001654 * @param[in,out] when_p When pointer to parse to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001655 * @return LY_ERR values.
1656 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001657LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001658parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001659{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001660 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001662 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001663 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001665 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666
1667 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001668 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001669 return LY_EVALID;
1670 }
1671
1672 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001673 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001674
1675 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001676 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001677 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001678 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001680 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001682 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001683 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->dsc, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts),
1684 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001685 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001686 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001687 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->ref, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts),
1688 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001689 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001690 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001691 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 +02001692 break;
1693 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001694 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001695 ret = LY_EVALID;
1696 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001698 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001700
1701cleanup:
1702 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001703 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001704 free(when);
1705 } else {
1706 *when_p = when;
1707 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001708 return ret;
1709}
1710
Michal Vaskoea5abea2018-09-18 13:10:54 +02001711/**
1712 * @brief Parse the anydata or anyxml statement.
1713 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001714 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001715 * @param[in] any_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001716 * @param[in] parent Node parent.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001717 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001718 * @return LY_ERR values.
1719 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001720LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001721parse_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 +02001722{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001723 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001725 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001726 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001727 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001728
David Sedlák60adc092019-08-06 15:57:02 +02001729 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001730 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001731
Radek Krejci39b7fc22021-02-26 23:29:18 +01001732 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001733 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001736 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001737 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738
1739 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001740 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001741 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001742 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001743 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001744 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001745 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001746 LY_CHECK_RET(parse_text_field(ctx, any->dsc, LY_STMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001747 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001748 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001749 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001750 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001751 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001752 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001754 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001755 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001757 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001758 LY_CHECK_RET(parse_text_field(ctx, any->ref, LY_STMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001760 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001761 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001763 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001764 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001766 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001767 LY_CHECK_RET(parse_ext(ctx, word, word_len, any, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768 break;
1769 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001770 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771 return LY_EVALID;
1772 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001773 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001775
1776cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 return ret;
1778}
1779
Michal Vaskoea5abea2018-09-18 13:10:54 +02001780/**
1781 * @brief Parse the value or position statement. Substatement of type enum statement.
1782 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001783 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001784 * @param[in] val_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001785 * @param[in,out] enm Structure to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001786 * @return LY_ERR values.
1787 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001788LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001789parse_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 +02001790{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001791 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001792 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001793 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001794 long long num = 0;
1795 unsigned long long unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001796 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797
Michal Vasko193dacd2022-10-13 08:43:05 +02001798 if (enm->flags & LYS_SET_VALUE) {
1799 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001800 ret = LY_EVALID;
1801 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001802 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001803 enm->flags |= LYS_SET_VALUE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001804
1805 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001806 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001807
Radek Krejcid6b76452019-09-03 17:03:03 +02001808 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 +02001809 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001810 ret = LY_EVALID;
1811 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001812 }
1813
1814 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001815 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001816 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001817 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001818 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001819 ret = LY_EVALID;
1820 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001821 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001822 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001823 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001824 if (unum > UINT64_C(4294967295)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001825 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001826 ret = LY_EVALID;
1827 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001828 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001829 }
1830 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001831 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001832 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001833 ret = LY_EVALID;
1834 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835 }
1836 if (errno == ERANGE) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001837 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001838 ret = LY_EVALID;
1839 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001841 if (val_kw == LY_STMT_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001842 enm->value = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02001844 enm->value = unum;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001845 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001847 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001849 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001850 ret = parse_ext(ctx, word, word_len, enm, val_kw, 0, &enm->exts);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001851 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001852 break;
1853 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001854 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001855 ret = LY_EVALID;
1856 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001857 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001858 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001859 }
Radek Krejci8b764662018-11-14 14:15:13 +01001860
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001861cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001862 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001863 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001864}
1865
Michal Vaskoea5abea2018-09-18 13:10:54 +02001866/**
1867 * @brief Parse the enum or bit statement. Substatement of type statement.
1868 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001869 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001870 * @param[in] enum_kw Type of this particular keyword.
1871 * @param[in,out] enums Enums or bits to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001872 * @return LY_ERR values.
1873 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001875parse_type_enum(struct lysp_yang_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001877 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001878 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001879 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001880 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881 struct lysp_type_enum *enm;
1882
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001883 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001884
1885 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001886 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 +02001887 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001888 ret = lysp_check_enum_name((struct lysp_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001889 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001890 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001891
Michal Vasko12ef5362022-09-16 15:13:58 +02001892 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001893 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(enum_kw), enm->name);
Radek Krejci8b764662018-11-14 14:15:13 +01001894
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001895 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001897 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001898 LY_CHECK_RET(parse_text_field(ctx, enm->dsc, LY_STMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001900 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001901 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001902 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001903 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001904 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001905 LY_CHECK_RET(parse_text_field(ctx, enm->ref, LY_STMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001907 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001908 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001909 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001910 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001911 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1912 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1913 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
David Sedlák9fb515f2019-07-11 10:33:58 +02001914 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001915 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001916 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1917 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1918 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001920 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001921 LY_CHECK_RET(parse_ext(ctx, word, word_len, enm, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922 break;
1923 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001924 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925 return LY_EVALID;
1926 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001927 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001929
1930cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931 return ret;
1932}
1933
Michal Vaskoea5abea2018-09-18 13:10:54 +02001934/**
1935 * @brief Parse the fraction-digits statement. Substatement of type statement.
1936 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001937 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001938 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001939 * @return LY_ERR values.
1940 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001941static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001942parse_type_fracdigits(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001943{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001944 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001945 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001946 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001947 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001948 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949
Michal Vasko193dacd2022-10-13 08:43:05 +02001950 if (type->fraction_digits) {
David Sedlákb3077192019-06-19 10:55:37 +02001951 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001952 return LY_EVALID;
1953 }
1954
1955 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001956 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001957
1958 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001959 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001960 ret = LY_EVALID;
1961 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962 }
1963
1964 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001965 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001966 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001967 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001968 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001969 ret = LY_EVALID;
1970 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001971 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001972 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001973 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001974 ret = LY_EVALID;
1975 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001977 type->fraction_digits = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001979 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001980 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001981 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001982 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 +02001983 break;
1984 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001985 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001986 ret = LY_EVALID;
1987 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001988 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001989 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001991
1992cleanup:
1993 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001994 return ret;
1995}
1996
Michal Vaskoea5abea2018-09-18 13:10:54 +02001997/**
1998 * @brief Parse the require-instance statement. Substatement of type statement.
1999 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002000 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002001 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002002 * @return LY_ERR values.
2003 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002004static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002005parse_type_reqinstance(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002007 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002008 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002009 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002010 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011
Michal Vasko193dacd2022-10-13 08:43:05 +02002012 if (type->flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002013 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 return LY_EVALID;
2015 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002016 type->flags |= LYS_SET_REQINST;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017
2018 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002019 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020
Radek Krejcif13b87b2020-12-01 22:02:17 +01002021 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002022 type->require_instance = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002023 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002024 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002025 ret = LY_EVALID;
2026 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002029 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002031 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002032 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 +02002033 break;
2034 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002035 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002036 ret = LY_EVALID;
2037 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002039 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002040 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002041
2042cleanup:
2043 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044 return ret;
2045}
2046
Michal Vaskoea5abea2018-09-18 13:10:54 +02002047/**
2048 * @brief Parse the modifier statement. Substatement of type pattern statement.
2049 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002050 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002051 * @param[in,out] restr Restriction to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002052 * @return LY_ERR values.
2053 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002054static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002055parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002057 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002058 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002059 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002060 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002061
Michal Vasko193dacd2022-10-13 08:43:05 +02002062 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002063 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064 return LY_EVALID;
2065 }
2066
2067 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002068 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002069
Radek Krejcif13b87b2020-12-01 22:02:17 +01002070 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002071 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002072 ret = LY_EVALID;
2073 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002075
2076 /* replace the value in the dictionary */
Michal Vasko193dacd2022-10-13 08:43:05 +02002077 buf = malloc(strlen(restr->arg.str) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002078 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02002079 strcpy(buf, restr->arg.str);
2080 lydict_remove(PARSER_CTX(ctx), restr->arg.str);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081
Radek Krejcif13b87b2020-12-01 22:02:17 +01002082 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2083 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko183ea332022-11-08 10:50:03 +01002084 ret = lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002085 buf = NULL;
Michal Vasko183ea332022-11-08 10:50:03 +01002086 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002087
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002088 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002089 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002090 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002091 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 +02002092 break;
2093 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002094 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002095 ret = LY_EVALID;
2096 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002097 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002098 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002099 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002100
2101cleanup:
2102 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 return ret;
2104}
2105
Michal Vaskoea5abea2018-09-18 13:10:54 +02002106/**
2107 * @brief Parse the pattern statement. Substatement of type statement.
2108 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002109 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002110 * @param[in,out] patterns Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002111 * @return LY_ERR values.
2112 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002113static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002114parse_type_pattern(struct lysp_yang_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002116 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002118 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002119 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 struct lysp_restr *restr;
2121
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002122 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123
2124 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002125 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126
2127 /* add special meaning first byte */
2128 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002129 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130 word = buf;
2131 } else {
2132 buf = malloc(word_len + 2);
2133 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002134 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002135 if (word_len) {
2136 memmove(buf + 1, word, word_len);
2137 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002138 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002139 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002140 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002141 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002142
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002143 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002144 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002145 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002146 LY_CHECK_RET(parse_text_field(ctx, restr->dsc, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002148 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002149 LY_CHECK_RET(parse_text_field(ctx, restr->ref, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002151 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02002152 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002154 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002155 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002157 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002158 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko193dacd2022-10-13 08:43:05 +02002159 LY_CHECK_RET(parse_type_pattern_modifier(ctx, restr));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002161 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002162 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163 break;
2164 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002165 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166 return LY_EVALID;
2167 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002168 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002170
2171cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002172 return ret;
2173}
2174
Michal Vaskoea5abea2018-09-18 13:10:54 +02002175/**
2176 * @brief Parse the type statement.
2177 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002178 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002179 * @param[in,out] type Type to wrote to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002180 * @return LY_ERR values.
2181 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002183parse_type(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002184{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002185 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002187 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002188 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002189 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002190 struct lysp_type *nest_type;
2191
2192 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002193 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194 return LY_EVALID;
2195 }
2196
2197 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002198 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002199 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002201 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002202 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002203
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002204 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002205 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002206 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002207 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 +01002208 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002210 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002211 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002212 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002214 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002215 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002216 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002218 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002219 LY_CHECK_RET(parse_type_fracdigits(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002220 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002222 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002223 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002224 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002225 return LY_EVALID;
2226 }
2227 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002228 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002229
Radek Krejci33090f92020-12-17 20:12:46 +01002230 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002231 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002232 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002233 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002234 if (type->path) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002235 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002236 return LY_EVALID;
2237 }
2238
aPiecek4bb1e372021-05-07 11:01:00 +02002239 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2240 * corresponding structure, so in case of failure, the lysp_module_free function will take
2241 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2242 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2243 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002244 LY_CHECK_ERR_RET(ret = parse_text_field(ctx, type, LY_STMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts),
aPiecek4bb1e372021-05-07 11:01:00 +02002245 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002246 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002247 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002248 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002249 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002250 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002251 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002252 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002253 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002254 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002255 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002257 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002259 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 return LY_EVALID;
2261 }
2262 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002263 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002264
Radek Krejci33090f92020-12-17 20:12:46 +01002265 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002266 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002268 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002269 LY_CHECK_RET(parse_type_reqinstance(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002270 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002272 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002273 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002274 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002275 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002277 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002278 LY_CHECK_RET(parse_ext(ctx, word, word_len, type, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 break;
2280 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002281 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002282 return LY_EVALID;
2283 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002284 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002285 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002286
2287cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 return ret;
2289}
2290
Michal Vaskoea5abea2018-09-18 13:10:54 +02002291/**
2292 * @brief Parse the leaf statement.
2293 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002294 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002295 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002296 * @return LY_ERR values.
2297 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002298LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002299parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002300{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002301 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002303 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002304 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002305 struct lysp_node_leaf *leaf;
2306
David Sedlák60adc092019-08-06 15:57:02 +02002307 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002308 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002310 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002313 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002314 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002315
2316 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002317 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002318 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002319 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002320 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002322 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002323 LY_CHECK_RET(parse_text_field(ctx, &leaf->dflt, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002324 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002325 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002326 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002327 LY_CHECK_RET(parse_text_field(ctx, leaf->dsc, LY_STMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002329 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002330 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002332 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002333 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002335 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002336 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002338 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002339 LY_CHECK_RET(parse_text_field(ctx, leaf->ref, LY_STMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002341 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002342 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002344 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002345 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002347 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002348 LY_CHECK_RET(parse_text_field(ctx, leaf->units, LY_STMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002350 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002351 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002353 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002354 LY_CHECK_RET(parse_ext(ctx, word, word_len, leaf, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002355 break;
2356 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002357 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002358 return LY_EVALID;
2359 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002360 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002362
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363 /* mandatory substatements */
2364 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002365 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 return LY_EVALID;
2367 }
2368
Michal Vasko12ef5362022-09-16 15:13:58 +02002369cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370 return ret;
2371}
2372
Michal Vaskoea5abea2018-09-18 13:10:54 +02002373/**
2374 * @brief Parse the max-elements statement.
2375 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002376 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002377 * @param[in,out] max Value to write to.
2378 * @param[in,out] flags Flags to write to.
2379 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002380 * @return LY_ERR values.
2381 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002382LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002383parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002384{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002385 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002386 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002387 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002388 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002389 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390
2391 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002392 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002393 return LY_EVALID;
2394 }
2395 *flags |= LYS_SET_MAX;
2396
2397 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002398 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399
2400 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002401 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002402 ret = LY_EVALID;
2403 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002404 }
2405
Radek Krejci7f9b6512019-09-18 13:11:09 +02002406 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002407 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002408 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002410 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002411 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002412 ret = LY_EVALID;
2413 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002414 }
2415 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002416 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002417 ret = LY_EVALID;
2418 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419 }
2420
2421 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002422 } else {
2423 /* unbounded */
2424 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002425 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002427 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002429 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002430 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 +02002431 break;
2432 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002433 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002434 ret = LY_EVALID;
2435 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002437 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002439
2440cleanup:
2441 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002442 return ret;
2443}
2444
Michal Vaskoea5abea2018-09-18 13:10:54 +02002445/**
2446 * @brief Parse the min-elements statement.
2447 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002448 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002449 * @param[in,out] min Value to write to.
2450 * @param[in,out] flags Flags to write to.
2451 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002452 * @return LY_ERR values.
2453 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002454LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002455parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002456{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002457 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002458 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002459 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002460 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002461 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002462
2463 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002464 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465 return LY_EVALID;
2466 }
2467 *flags |= LYS_SET_MIN;
2468
2469 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002470 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002471
2472 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002473 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002474 ret = LY_EVALID;
2475 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002476 }
2477
2478 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002479 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002481 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002482 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002483 ret = LY_EVALID;
2484 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002485 }
2486 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002487 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002488 ret = LY_EVALID;
2489 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490 }
2491 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002492
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002493 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002494 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002495 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002496 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 +02002497 break;
2498 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002499 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002500 ret = LY_EVALID;
2501 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002503 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002505
2506cleanup:
2507 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508 return ret;
2509}
2510
Michal Vaskoea5abea2018-09-18 13:10:54 +02002511/**
2512 * @brief Parse the ordered-by statement.
2513 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002514 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002515 * @param[in,out] llist List or leaf-list to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002516 * @return LY_ERR values.
2517 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002519parse_orderedby(struct lysp_yang_ctx *ctx, struct lysp_node *llist)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002520{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002521 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002522 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002523 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002524 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525
Michal Vasko193dacd2022-10-13 08:43:05 +02002526 if (llist->flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002527 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 return LY_EVALID;
2529 }
2530
2531 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002532 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533
Radek Krejcif13b87b2020-12-01 22:02:17 +01002534 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002535 llist->flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002536 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002537 llist->flags |= LYS_ORDBY_USER;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002539 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002540 ret = LY_EVALID;
2541 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002544 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002545 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002546 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002547 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 +02002548 break;
2549 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002550 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002551 ret = LY_EVALID;
2552 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002553 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002554 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002556
2557cleanup:
2558 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002559 return ret;
2560}
2561
Michal Vaskoea5abea2018-09-18 13:10:54 +02002562/**
2563 * @brief Parse the leaf-list statement.
2564 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002565 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002566 * @param[in,out] siblings Siblings to add to.
2567 *
2568 * @return LY_ERR values.
2569 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002570LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002571parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002573 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002575 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002576 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577 struct lysp_node_leaflist *llist;
2578
David Sedlák60adc092019-08-06 15:57:02 +02002579 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002580 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002582 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002583
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002585 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002586 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587
2588 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002589 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002591 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002592 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002594 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002595 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002596 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002598 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002599 LY_CHECK_RET(parse_text_field(ctx, llist->dsc, LY_STMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002601 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002602 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002604 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002605 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002607 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002608 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002610 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002611 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002613 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02002614 LY_CHECK_RET(parse_orderedby(ctx, &llist->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002616 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002617 LY_CHECK_RET(parse_text_field(ctx, llist->ref, LY_STMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002619 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002620 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002622 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002623 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002626 LY_CHECK_RET(parse_text_field(ctx, llist->units, LY_STMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002629 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002631 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002632 LY_CHECK_RET(parse_ext(ctx, word, word_len, llist, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 break;
2634 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002635 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 return LY_EVALID;
2637 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002638 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002640
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641 /* mandatory substatements */
2642 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002643 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644 return LY_EVALID;
2645 }
2646
Michal Vasko12ef5362022-09-16 15:13:58 +02002647cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002648 return ret;
2649}
2650
Michal Vaskoea5abea2018-09-18 13:10:54 +02002651/**
2652 * @brief Parse the refine statement.
2653 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002654 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002655 * @param[in,out] refines Refines to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002656 * @return LY_ERR values.
2657 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002659parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002661 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002663 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002664 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002665 struct lysp_refine *rf;
2666
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002667 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002668
2669 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002670 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002671 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002672 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002674 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002676 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002677 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002679 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002680 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002682 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002683 LY_CHECK_RET(parse_text_field(ctx, rf->dsc, LY_STMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002685 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002686 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002687 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002689 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002690 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002692 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002693 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002695 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002696 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002698 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002699 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002701 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002702 LY_CHECK_RET(parse_text_field(ctx, rf->ref, LY_STMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002704 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002705 LY_CHECK_RET(parse_text_field(ctx, rf->presence, LY_STMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002707 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002708 LY_CHECK_RET(parse_ext(ctx, word, word_len, rf, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002709 break;
2710 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002711 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712 return LY_EVALID;
2713 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002714 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002715 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002716
2717cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718 return ret;
2719}
2720
Michal Vaskoea5abea2018-09-18 13:10:54 +02002721/**
2722 * @brief Parse the typedef statement.
2723 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002724 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002725 * @param[in,out] typedefs Typedefs to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002726 * @return LY_ERR values.
2727 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002729parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002730{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002731 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002733 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002734 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 struct lysp_tpdf *tpdf;
2736
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002737 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738
2739 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002740 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002741 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002742
2743 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002744 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002746 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002747 LY_CHECK_RET(parse_text_field(ctx, &tpdf->dflt, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002748 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002750 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002751 LY_CHECK_RET(parse_text_field(ctx, tpdf->dsc, LY_STMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002753 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002754 LY_CHECK_RET(parse_text_field(ctx, tpdf->ref, LY_STMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002756 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002757 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002759 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002760 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002762 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002763 LY_CHECK_RET(parse_text_field(ctx, tpdf->units, LY_STMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002765 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002766 LY_CHECK_RET(parse_ext(ctx, word, word_len, tpdf, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767 break;
2768 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002769 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 return LY_EVALID;
2771 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002772 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002774
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 /* mandatory substatements */
2776 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002777 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 return LY_EVALID;
2779 }
2780
Radek Krejcibbe09a92018-11-08 09:36:54 +01002781 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002782 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002783 assert(ctx->main_ctx);
2784 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002785 }
2786
Michal Vasko12ef5362022-09-16 15:13:58 +02002787cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 return ret;
2789}
2790
Michal Vaskoea5abea2018-09-18 13:10:54 +02002791/**
2792 * @brief Parse the input or output statement.
2793 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002794 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002795 * @param[in] kw Type of this particular keyword
2796 * @param[in,out] inout_p Input/output pointer to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002797 * @return LY_ERR values.
2798 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002800parse_inout(struct lysp_yang_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002801 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002803 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002804 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002805 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002806 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002807 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002809 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002810 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811 return LY_EVALID;
2812 }
2813
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002814 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002815 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2816 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002817 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818
2819 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002820 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002821 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002822 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002823 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002824 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002825 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002826 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002828 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002829 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002831 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002832 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002833 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002834 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002835 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002837 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002838 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002840 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002841 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002843 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002844 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002846 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002847 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002849 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002850 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002851 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002852 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002853 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002854 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002856 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002857 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_p, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 break;
2859 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002860 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 return LY_EVALID;
2862 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002863 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002865
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002866cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 return ret;
2868}
2869
Michal Vaskoea5abea2018-09-18 13:10:54 +02002870/**
2871 * @brief Parse the action statement.
2872 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002873 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002874 * @param[in,out] actions Actions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002875 * @return LY_ERR values.
2876 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002877LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002878parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002879{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002880 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002882 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002883 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002884 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002885
Radek Krejci2a9fc652021-01-22 17:44:34 +01002886 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002887
2888 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002889 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002890 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002891 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002892 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002893
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002894 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002896 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002897 LY_CHECK_RET(parse_text_field(ctx, act->dsc, LY_STMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002899 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002900 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002902 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002903 LY_CHECK_RET(parse_text_field(ctx, act->ref, LY_STMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002905 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002906 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 break;
2908
Radek Krejcid6b76452019-09-03 17:03:03 +02002909 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002910 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002911 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002912 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002913 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002914 break;
2915
Radek Krejcid6b76452019-09-03 17:03:03 +02002916 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002917 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002918 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002919 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002920 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002922 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002923 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 +02002924 break;
2925 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002926 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002927 return LY_EVALID;
2928 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002929 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002930 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002931
2932 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2933 if (!act->input.nodetype) {
2934 act->input.nodetype = LYS_INPUT;
2935 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002936 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002937 }
2938 if (!act->output.nodetype) {
2939 act->output.nodetype = LYS_OUTPUT;
2940 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002941 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002942 }
2943
Michal Vasko12ef5362022-09-16 15:13:58 +02002944cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002945 return ret;
2946}
2947
Michal Vaskoea5abea2018-09-18 13:10:54 +02002948/**
2949 * @brief Parse the notification statement.
2950 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002951 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002952 * @param[in,out] notifs Notifications to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002953 * @return LY_ERR values.
2954 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002955LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002956parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002958 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002959 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002960 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002961 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002962 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002963
Radek Krejci2a9fc652021-01-22 17:44:34 +01002964 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002965
2966 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002967 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002968 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002969 notif->nodetype = LYS_NOTIF;
2970 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002971
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002972 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002974 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002975 LY_CHECK_RET(parse_text_field(ctx, notif->dsc, LY_STMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002977 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002978 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002979 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002980 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002981 LY_CHECK_RET(parse_text_field(ctx, notif->ref, LY_STMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002983 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002984 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985 break;
2986
Radek Krejcid6b76452019-09-03 17:03:03 +02002987 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002988 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002989 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002990 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002991 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002993 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002994 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002996 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002997 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002999 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003000 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003002 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003003 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003004 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003005 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003006 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003008 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003009 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010 break;
3011
Radek Krejcid6b76452019-09-03 17:03:03 +02003012 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003013 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003014 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003016 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003017 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003019 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003020 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003022 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003023 LY_CHECK_RET(parse_ext(ctx, word, word_len, notif, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 break;
3025 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003026 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 return LY_EVALID;
3028 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003029 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003031
Michal Vasko12ef5362022-09-16 15:13:58 +02003032cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 return ret;
3034}
3035
Michal Vaskoea5abea2018-09-18 13:10:54 +02003036/**
3037 * @brief Parse the grouping statement.
3038 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003039 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003040 * @param[in,out] groupings Groupings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003041 * @return LY_ERR values.
3042 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003043LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003044parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003046 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003048 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003049 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003050 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003051
Radek Krejci2a9fc652021-01-22 17:44:34 +01003052 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053
3054 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003055 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003056 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003057 grp->nodetype = LYS_GROUPING;
3058 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003059
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003060 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003062 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003063 LY_CHECK_RET(parse_text_field(ctx, grp->dsc, LY_STMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003065 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003066 LY_CHECK_RET(parse_text_field(ctx, grp->ref, LY_STMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003067 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003068 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003069 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003070 break;
3071
Radek Krejcid6b76452019-09-03 17:03:03 +02003072 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003073 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003074 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003075 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003076 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003078 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003079 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003080 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003081 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003082 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003084 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003085 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003087 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003088 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003090 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003091 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003093 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003094 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 break;
3096
Radek Krejcid6b76452019-09-03 17:03:03 +02003097 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003098 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003100 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003101 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003102 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003103 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003104 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003105 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003106 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003107 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003108 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003109 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003111 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003112 LY_CHECK_RET(parse_ext(ctx, word, word_len, grp, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003113 break;
3114 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003115 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003116 return LY_EVALID;
3117 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003118 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003120
aPiecek63e080d2021-06-29 13:53:28 +02003121 /* store data for collision check */
3122 if (parent) {
3123 assert(ctx->main_ctx);
3124 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3125 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003126
Michal Vasko12ef5362022-09-16 15:13:58 +02003127cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128 return ret;
3129}
3130
Michal Vaskoea5abea2018-09-18 13:10:54 +02003131/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003132 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003133 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003134 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003135 * @param[in,out] augments Augments to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003136 * @return LY_ERR values.
3137 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003138LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003139parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003141 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003143 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003144 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003145 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146
Radek Krejci2a9fc652021-01-22 17:44:34 +01003147 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148
3149 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003150 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003151 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003152 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003153 aug->nodetype = LYS_AUGMENT;
3154 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003155
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003156 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003158 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003159 LY_CHECK_RET(parse_text_field(ctx, aug->dsc, LY_STMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003161 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003162 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003164 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003165 LY_CHECK_RET(parse_text_field(ctx, aug->ref, LY_STMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003167 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003168 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003170 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003171 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 break;
3173
Radek Krejcid6b76452019-09-03 17:03:03 +02003174 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003175 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003176 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003177 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003178 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003180 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003181 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003183 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003184 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003186 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003187 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003189 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003190 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003192 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003193 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003195 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003196 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003198 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003199 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003200 break;
3201
Radek Krejcid6b76452019-09-03 17:03:03 +02003202 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003203 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003204 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003206 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003207 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003208 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003209 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003210 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003211 LY_CHECK_RET(parse_ext(ctx, word, word_len, aug, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212 break;
3213 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003214 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003215 return LY_EVALID;
3216 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003217 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003218 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003219
Michal Vasko12ef5362022-09-16 15:13:58 +02003220cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221 return ret;
3222}
3223
Michal Vaskoea5abea2018-09-18 13:10:54 +02003224/**
3225 * @brief Parse the uses statement.
3226 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003227 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003228 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003229 * @return LY_ERR values.
3230 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003231LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003232parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003234 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003235 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003236 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003237 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003238 struct lysp_node_uses *uses;
3239
David Sedlák60adc092019-08-06 15:57:02 +02003240 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003241 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003243 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003246 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003247 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248
3249 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003250 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003252 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003253 LY_CHECK_RET(parse_text_field(ctx, uses->dsc, LY_STMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003255 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003256 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003258 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003259 LY_CHECK_RET(parse_text_field(ctx, uses->ref, LY_STMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003261 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003262 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003263 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003264 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003265 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 break;
3267
Radek Krejcid6b76452019-09-03 17:03:03 +02003268 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003269 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003271 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003272 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003274 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003275 LY_CHECK_RET(parse_ext(ctx, word, word_len, uses, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276 break;
3277 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003278 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279 return LY_EVALID;
3280 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003281 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003282 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003283
Michal Vasko12ef5362022-09-16 15:13:58 +02003284cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285 return ret;
3286}
3287
Michal Vaskoea5abea2018-09-18 13:10:54 +02003288/**
3289 * @brief Parse the case statement.
3290 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003291 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003292 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003293 * @return LY_ERR values.
3294 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003295LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003296parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003298 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003299 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003300 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003301 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003302 struct lysp_node_case *cas;
3303
David Sedlák60adc092019-08-06 15:57:02 +02003304 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003305 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003307 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308
Michal Vasko7fbc8162018-09-17 10:35:16 +02003309 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003310 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003311 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312
3313 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003314 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003316 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003317 LY_CHECK_RET(parse_text_field(ctx, cas->dsc, LY_STMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003319 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003320 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003322 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003323 LY_CHECK_RET(parse_text_field(ctx, cas->ref, LY_STMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003325 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003326 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003328 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003329 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003330 break;
3331
Radek Krejcid6b76452019-09-03 17:03:03 +02003332 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003333 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003334 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003335 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003336 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003338 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003339 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003341 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003342 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003344 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003345 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003347 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003348 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003350 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003351 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003353 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003354 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003356 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003357 LY_CHECK_RET(parse_ext(ctx, word, word_len, cas, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003358 break;
3359 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003360 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 return LY_EVALID;
3362 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003363 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003364 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003365
3366cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367 return ret;
3368}
3369
Michal Vaskoea5abea2018-09-18 13:10:54 +02003370/**
3371 * @brief Parse the choice statement.
3372 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003373 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003374 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003375 * @return LY_ERR values.
3376 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003377LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003378parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003379{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003380 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003382 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003383 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003384 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003385
David Sedlák60adc092019-08-06 15:57:02 +02003386 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003387 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003388 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003389 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390
Michal Vasko7fbc8162018-09-17 10:35:16 +02003391 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003392 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003393 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394
3395 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003396 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003397 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003398 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003399 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003401 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003402 LY_CHECK_RET(parse_text_field(ctx, choice->dsc, LY_STMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003404 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003405 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003407 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003408 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003410 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003411 LY_CHECK_RET(parse_text_field(ctx, choice->ref, LY_STMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003414 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003416 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003417 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003419 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003420 LY_CHECK_RET(parse_text_field(ctx, &choice->dflt, LY_STMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
Michal Vasko7f45cf22020-10-01 12:49:44 +02003421 &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003422 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
3424
Radek Krejcid6b76452019-09-03 17:03:03 +02003425 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003426 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003427 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003429 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003432 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003434 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003435 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003436 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003438 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003439 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003441 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003442 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003444 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003445 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003448 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003451 LY_CHECK_RET(parse_ext(ctx, word, word_len, choice, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
3453 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003454 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 return LY_EVALID;
3456 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003457 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003459
3460cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 return ret;
3462}
3463
Michal Vaskoea5abea2018-09-18 13:10:54 +02003464/**
3465 * @brief Parse the container statement.
3466 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003467 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003468 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003469 * @return LY_ERR values.
3470 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003471LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003472parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473{
3474 LY_ERR ret = 0;
3475 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003476 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003477 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478 struct lysp_node_container *cont;
3479
David Sedlák60adc092019-08-06 15:57:02 +02003480 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003481 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003482 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003483 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003486 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003487 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003488
3489 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003490 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003492 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003493 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003495 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003496 LY_CHECK_RET(parse_text_field(ctx, cont->dsc, LY_STMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003498 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003499 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003501 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003502 LY_CHECK_RET(parse_text_field(ctx, cont->ref, LY_STMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003504 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003505 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003507 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003508 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003510 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003511 LY_CHECK_RET(parse_text_field(ctx, cont->presence, LY_STMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512 break;
3513
Radek Krejcid6b76452019-09-03 17:03:03 +02003514 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003515 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003516 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003517 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003518 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003519 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003520 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003521 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003523 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003524 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003527 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003529 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003530 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003532 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003533 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003535 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003536 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 break;
3538
Radek Krejcid6b76452019-09-03 17:03:03 +02003539 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003540 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003542 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003543 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003545 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003546 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003547 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003549 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003550 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003552 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003553 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003554 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003556 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003557 LY_CHECK_RET(parse_ext(ctx, word, word_len, cont, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 break;
3559 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003560 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003561 return LY_EVALID;
3562 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003563 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003565
Michal Vasko12ef5362022-09-16 15:13:58 +02003566cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 return ret;
3568}
3569
Michal Vaskoea5abea2018-09-18 13:10:54 +02003570/**
3571 * @brief Parse the list statement.
3572 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003573 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003574 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003575 * @return LY_ERR values.
3576 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003577LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003578parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003580 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003582 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003583 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 struct lysp_node_list *list;
3585
David Sedlák60adc092019-08-06 15:57:02 +02003586 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003587 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003589 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003592 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003593 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594
3595 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003596 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003598 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003599 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003601 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003602 LY_CHECK_RET(parse_text_field(ctx, list->dsc, LY_STMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003604 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003605 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003607 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003608 LY_CHECK_RET(parse_text_field(ctx, list->ref, LY_STMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003610 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003611 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003613 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003614 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003616 case LY_STMT_KEY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003617 LY_CHECK_RET(parse_text_field(ctx, list, LY_STMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003619 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003620 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003622 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003623 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003625 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003626 LY_CHECK_RET(parse_orderedby(ctx, &list->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003628 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003629 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003630 break;
3631
Radek Krejcid6b76452019-09-03 17:03:03 +02003632 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003633 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003634 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003635 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003636 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003638 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003639 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003641 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003642 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003644 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003645 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003647 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003648 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003650 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003651 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003653 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003654 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003655 break;
3656
Radek Krejcid6b76452019-09-03 17:03:03 +02003657 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003658 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003659 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003660 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003661 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003662 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003663 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003664 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003665 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003666 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003667 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003668 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003670 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003671 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003672 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003674 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003675 LY_CHECK_RET(parse_ext(ctx, word, word_len, list, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676 break;
3677 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003678 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679 return LY_EVALID;
3680 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003681 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003683
Michal Vasko12ef5362022-09-16 15:13:58 +02003684cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 return ret;
3686}
3687
Michal Vaskoea5abea2018-09-18 13:10:54 +02003688/**
3689 * @brief Parse the yin-element statement.
3690 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003691 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003692 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003693 * @return LY_ERR values.
3694 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003695static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003696parse_yinelement(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003697{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003698 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003700 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003701 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003702
Michal Vasko193dacd2022-10-13 08:43:05 +02003703 if (ext->flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003704 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003705 return LY_EVALID;
3706 }
3707
3708 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003709 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003710
Radek Krejcif13b87b2020-12-01 22:02:17 +01003711 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003712 ext->flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003713 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003714 ext->flags |= LYS_YINELEM_FALSE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003716 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717 free(buf);
3718 return LY_EVALID;
3719 }
3720 free(buf);
3721
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003722 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003723 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003724 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003725 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_YIN_ELEMENT, 0, &ext->exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003726 LY_CHECK_RET(ret);
3727 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003729 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003730 return LY_EVALID;
3731 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003732 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003734
3735cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736 return ret;
3737}
3738
Michal Vaskoea5abea2018-09-18 13:10:54 +02003739/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003740 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003741 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003742 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003743 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003744 * @return LY_ERR values.
3745 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003746static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003747parse_argument(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003749 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003751 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003752 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003753
Michal Vasko193dacd2022-10-13 08:43:05 +02003754 if (ext->argname) {
David Sedlákb3077192019-06-19 10:55:37 +02003755 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003756 return LY_EVALID;
3757 }
3758
3759 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003760 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko193dacd2022-10-13 08:43:05 +02003761 INSERT_WORD_GOTO(ctx, buf, ext->argname, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003763 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003765 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003766 LY_CHECK_RET(parse_yinelement(ctx, ext));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003767 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003768 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003769 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_ARGUMENT, 0, &ext->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003770 break;
3771 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003772 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003773 return LY_EVALID;
3774 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003775 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003776 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003777
3778cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 return ret;
3780}
3781
Michal Vaskoea5abea2018-09-18 13:10:54 +02003782/**
3783 * @brief Parse the extension statement.
3784 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003785 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003786 * @param[in,out] extensions Extensions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003787 * @return LY_ERR values.
3788 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003789static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003790parse_extension(struct lysp_yang_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003792 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003793 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003794 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003795 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003796 struct lysp_ext *ex;
3797
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003798 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799
3800 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003801 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003802 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003803
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003804 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003806 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003807 LY_CHECK_RET(parse_text_field(ctx, ex->dsc, LY_STMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003809 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003810 LY_CHECK_RET(parse_text_field(ctx, ex->ref, LY_STMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003811 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003812 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003813 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003815 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003816 LY_CHECK_RET(parse_argument(ctx, ex));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003818 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003819 LY_CHECK_RET(parse_ext(ctx, word, word_len, ex, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003820 break;
3821 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003822 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 return LY_EVALID;
3824 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003825 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003827
3828cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 return ret;
3830}
3831
Michal Vaskoea5abea2018-09-18 13:10:54 +02003832/**
3833 * @brief Parse the deviate statement.
3834 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003835 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003836 * @param[in,out] deviates Deviates to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003837 * @return LY_ERR values.
3838 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003839LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003840parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003842 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003843 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003844 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003845 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003846 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003847 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 struct lysp_deviate_add *d_add = NULL;
3849 struct lysp_deviate_rpl *d_rpl = NULL;
3850 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003851 const char **d_units = NULL;
3852 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003853 struct lysp_restr **d_musts = NULL;
3854 uint16_t *d_flags = 0;
3855 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003856
3857 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003858 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003859
Radek Krejcif13b87b2020-12-01 22:02:17 +01003860 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003862 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003864 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003866 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003867 dev_mod = LYS_DEV_DELETE;
3868 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003869 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003870 ret = LY_EVALID;
3871 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003873
3874 /* create structure */
3875 switch (dev_mod) {
3876 case LYS_DEV_NOT_SUPPORTED:
3877 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003878 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003879 break;
3880 case LYS_DEV_ADD:
3881 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003882 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003883 d = (struct lysp_deviate *)d_add;
3884 d_units = &d_add->units;
3885 d_uniques = &d_add->uniques;
3886 d_dflts = &d_add->dflts;
3887 d_musts = &d_add->musts;
3888 d_flags = &d_add->flags;
3889 d_min = &d_add->min;
3890 d_max = &d_add->max;
3891 break;
3892 case LYS_DEV_REPLACE:
3893 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003894 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 d = (struct lysp_deviate *)d_rpl;
3896 d_units = &d_rpl->units;
3897 d_flags = &d_rpl->flags;
3898 d_min = &d_rpl->min;
3899 d_max = &d_rpl->max;
3900 break;
3901 case LYS_DEV_DELETE:
3902 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003903 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 d = (struct lysp_deviate *)d_del;
3905 d_units = &d_del->units;
3906 d_uniques = &d_del->uniques;
3907 d_dflts = &d_del->dflts;
3908 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003909 break;
3910 default:
3911 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003912 LOGINT(PARSER_CTX(ctx));
3913 ret = LY_EINT;
3914 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003915 }
3916 d->mod = dev_mod;
3917
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003918 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003920 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921 switch (dev_mod) {
3922 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003923 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003924 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003925 ret = LY_EVALID;
3926 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003927 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003928 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929 break;
3930 }
3931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003932 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933 switch (dev_mod) {
3934 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02003935 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003936 ret = LY_EVALID;
3937 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003939 ret = parse_text_field(ctx, &d_rpl->dflt, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts);
3940 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003941 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942 break;
3943 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003944 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 +02003945 break;
3946 }
3947 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003948 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 switch (dev_mod) {
3950 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003951 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003952 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003953 ret = LY_EVALID;
3954 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003956 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003957 break;
3958 }
3959 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003960 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 switch (dev_mod) {
3962 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003963 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003964 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003965 ret = LY_EVALID;
3966 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003968 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969 break;
3970 }
3971 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003972 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003973 switch (dev_mod) {
3974 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003975 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003976 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003977 ret = LY_EVALID;
3978 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003979 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003980 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981 break;
3982 }
3983 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003984 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985 switch (dev_mod) {
3986 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003987 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003988 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003989 ret = LY_EVALID;
3990 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003992 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 break;
3994 }
3995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003996 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 switch (dev_mod) {
3998 case LYS_DEV_NOT_SUPPORTED:
3999 case LYS_DEV_ADD:
4000 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004001 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004002 ret = LY_EVALID;
4003 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004004 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004005 if (d_rpl->type) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004006 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004007 ret = LY_EVALID;
4008 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004009 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004010 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004011 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4012 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 break;
4014 }
4015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004016 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017 switch (dev_mod) {
4018 case LYS_DEV_NOT_SUPPORTED:
4019 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004020 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004021 ret = LY_EVALID;
4022 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004023 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004024 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 +02004025 break;
4026 }
4027 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004028 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004029 switch (dev_mod) {
4030 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02004031 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004032 ret = LY_EVALID;
4033 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004034 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004035 LY_CHECK_GOTO(ret = parse_text_field(ctx, *d_units, LY_STMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004036 break;
4037 }
4038 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004039 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004040 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 +02004041 break;
4042 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004043 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004044 ret = LY_EVALID;
4045 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004046 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004047 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004048 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004049
4050cleanup:
4051 free(buf);
4052 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004053 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004054 free(d);
4055 } else {
4056 /* insert into siblings */
4057 LY_LIST_INSERT(deviates, d, next);
4058 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059 return ret;
4060}
4061
Michal Vaskoea5abea2018-09-18 13:10:54 +02004062/**
4063 * @brief Parse the deviation statement.
4064 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004065 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004066 * @param[in,out] deviations Deviations to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004067 * @return LY_ERR values.
4068 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004069LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004070parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004071{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004072 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004074 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004075 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004077 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004079 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004080
4081 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004082 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004083 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004084 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004085
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004086 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004087 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004088 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004089 LY_CHECK_GOTO(ret = parse_text_field(ctx, dev->dsc, LY_STMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004090 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004091 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004092 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004094 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004095 LY_CHECK_GOTO(ret = parse_text_field(ctx, dev->ref, LY_STMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004096 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004097 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004098 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 +02004099 break;
4100 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004101 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004102 ret = LY_EVALID;
4103 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004104 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004105 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004107
Michal Vasko7fbc8162018-09-17 10:35:16 +02004108 /* mandatory substatements */
4109 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004110 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004111 ret = LY_EVALID;
4112 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004113 }
4114
Michal Vasko12ef5362022-09-16 15:13:58 +02004115cleanup:
4116 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004117 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004118 LY_ARRAY_DECREMENT_FREE(*deviations);
4119 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 return ret;
4121}
4122
Michal Vaskoea5abea2018-09-18 13:10:54 +02004123/**
4124 * @brief Parse the feature statement.
4125 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004126 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004127 * @param[in,out] features Features to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004128 * @return LY_ERR values.
4129 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004130LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004131parse_feature(struct lysp_yang_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004132{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004133 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004134 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004135 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004136 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004137 struct lysp_feature *feat;
4138
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004139 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004140
4141 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004142 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004143 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004144
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004145 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004146 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004147 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004148 LY_CHECK_RET(parse_text_field(ctx, feat->dsc, LY_STMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004150 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004151 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004153 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004154 LY_CHECK_RET(parse_text_field(ctx, feat->ref, LY_STMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004156 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004157 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004159 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004160 LY_CHECK_RET(parse_ext(ctx, word, word_len, feat, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161 break;
4162 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004163 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004164 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004166 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004167 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004168
4169cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 return ret;
4171}
4172
Michal Vaskoea5abea2018-09-18 13:10:54 +02004173/**
4174 * @brief Parse the identity statement.
4175 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004176 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004177 * @param[in,out] identities Identities to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004178 * @return LY_ERR values.
4179 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004180LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004181parse_identity(struct lysp_yang_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004182{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004183 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004185 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004186 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 struct lysp_ident *ident;
4188
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004189 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190
4191 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004192 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004193 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004194
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004195 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004197 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004198 LY_CHECK_RET(parse_text_field(ctx, ident->dsc, LY_STMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004200 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004201 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004202 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004203 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004204 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004205 LY_CHECK_RET(parse_text_field(ctx, ident->ref, LY_STMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004206 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004207 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004208 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004209 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004210 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004211 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004212 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules");
Radek Krejci10113652018-11-14 16:56:50 +01004213 return LY_EVALID;
4214 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004215 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 +02004216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004217 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004218 LY_CHECK_RET(parse_ext(ctx, word, word_len, ident, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219 break;
4220 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004221 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004222 return LY_EVALID;
4223 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004224 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004225 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004226
4227cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004228 return ret;
4229}
4230
Michal Vaskoea5abea2018-09-18 13:10:54 +02004231/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004232 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004233 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004234 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004235 * @param[in,out] mod Module to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004236 * @return LY_ERR values.
4237 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004238LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004239parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004240{
4241 LY_ERR ret = 0;
4242 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004243 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004244 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004245 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004246 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004247
Michal Vaskoc3781c32020-10-06 14:04:08 +02004248 mod->is_submod = 0;
4249
4250 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004251 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004252 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004253
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004254 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004255
Radek Krejcie3846472018-10-15 15:24:51 +02004256#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004257 if (mod_stmt > SECTION) {\
4258 LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;\
4259 } mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004260
Michal Vasko7fbc8162018-09-17 10:35:16 +02004261 switch (kw) {
4262 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004263 case LY_STMT_NAMESPACE:
4264 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004265 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004267 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004268 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004269 break;
4270 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004271 case LY_STMT_INCLUDE:
4272 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004273 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004274 break;
4275 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004276 case LY_STMT_ORGANIZATION:
4277 case LY_STMT_CONTACT:
4278 case LY_STMT_DESCRIPTION:
4279 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004280 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004281 break;
4282
4283 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004284 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004285 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004286 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004287 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004288 case LY_STMT_ANYDATA:
4289 case LY_STMT_ANYXML:
4290 case LY_STMT_AUGMENT:
4291 case LY_STMT_CHOICE:
4292 case LY_STMT_CONTAINER:
4293 case LY_STMT_DEVIATION:
4294 case LY_STMT_EXTENSION:
4295 case LY_STMT_FEATURE:
4296 case LY_STMT_GROUPING:
4297 case LY_STMT_IDENTITY:
4298 case LY_STMT_LEAF:
4299 case LY_STMT_LEAF_LIST:
4300 case LY_STMT_LIST:
4301 case LY_STMT_NOTIFICATION:
4302 case LY_STMT_RPC:
4303 case LY_STMT_TYPEDEF:
4304 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004305 mod_stmt = Y_MOD_BODY;
4306 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004307 case LY_STMT_EXTENSION_INSTANCE:
4308 /* no place in the statement order defined */
4309 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004310 default:
4311 /* error handled in the next switch */
4312 break;
4313 }
Radek Krejcie3846472018-10-15 15:24:51 +02004314#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004315
Radek Krejcie3846472018-10-15 15:24:51 +02004316 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004317 switch (kw) {
4318 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004319 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004320 LY_CHECK_RET(parse_yangversion(ctx, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004322 case LY_STMT_NAMESPACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004323 LY_CHECK_RET(parse_text_field(ctx, mod, LY_STMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004325 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02004326 LY_CHECK_RET(parse_text_field(ctx, mod->mod->prefix, LY_STMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004327 break;
4328
4329 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004330 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004331 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004332 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004333 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004334 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004335 break;
4336
4337 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004339 LY_CHECK_RET(parse_text_field(ctx, mod, LY_STMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004341 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004342 LY_CHECK_RET(parse_text_field(ctx, mod, LY_STMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004344 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004345 LY_CHECK_RET(parse_text_field(ctx, mod->mod->dsc, LY_STMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004347 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004348 LY_CHECK_RET(parse_text_field(ctx, mod->mod->ref, LY_STMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004349 break;
4350
4351 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004352 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004353 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004354 break;
4355
4356 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004357 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004358 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004359 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004361 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004364 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004367 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004370 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004373 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004376 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004379 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004380 break;
4381
Radek Krejcid6b76452019-09-03 17:03:03 +02004382 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004383 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004385 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004386 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004389 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004392 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004395 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004397 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004398 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004399 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004400 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004401 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004403 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004404 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004406 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004407 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004409 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004410 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004411 break;
4412
4413 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004414 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004415 return LY_EVALID;
4416 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004417 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004418 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004419
Michal Vasko7fbc8162018-09-17 10:35:16 +02004420 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004421 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004422 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004423 return LY_EVALID;
4424 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004425 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004426 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004427 }
4428
Radek Krejcie9e987e2018-10-31 12:50:27 +01004429 /* submodules share the namespace with the module names, so there must not be
4430 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004431 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004432 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004433 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004434 return LY_EVALID;
4435 }
4436
Michal Vasko12ef5362022-09-16 15:13:58 +02004437cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004438 return ret;
4439}
4440
4441/**
4442 * @brief Parse submodule substatements.
4443 *
4444 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004445 * @param[out] submod Parsed submodule structure.
4446 *
4447 * @return LY_ERR values.
4448 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004449LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004450parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004451{
4452 LY_ERR ret = 0;
4453 char *buf, *word;
4454 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004455 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004456 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004457 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004458
Michal Vaskoc3781c32020-10-06 14:04:08 +02004459 submod->is_submod = 1;
4460
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004461 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004462 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004463 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004464
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004465 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004466
4467#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004468 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 +01004469
4470 switch (kw) {
4471 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004472 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004473 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004475 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004476 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4477 break;
4478 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004479 case LY_STMT_INCLUDE:
4480 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004481 CHECK_ORDER(Y_MOD_LINKAGE);
4482 break;
4483 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004484 case LY_STMT_ORGANIZATION:
4485 case LY_STMT_CONTACT:
4486 case LY_STMT_DESCRIPTION:
4487 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004488 CHECK_ORDER(Y_MOD_META);
4489 break;
4490
4491 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004492 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004493 CHECK_ORDER(Y_MOD_REVISION);
4494 break;
4495 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004496 case LY_STMT_ANYDATA:
4497 case LY_STMT_ANYXML:
4498 case LY_STMT_AUGMENT:
4499 case LY_STMT_CHOICE:
4500 case LY_STMT_CONTAINER:
4501 case LY_STMT_DEVIATION:
4502 case LY_STMT_EXTENSION:
4503 case LY_STMT_FEATURE:
4504 case LY_STMT_GROUPING:
4505 case LY_STMT_IDENTITY:
4506 case LY_STMT_LEAF:
4507 case LY_STMT_LEAF_LIST:
4508 case LY_STMT_LIST:
4509 case LY_STMT_NOTIFICATION:
4510 case LY_STMT_RPC:
4511 case LY_STMT_TYPEDEF:
4512 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004513 mod_stmt = Y_MOD_BODY;
4514 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004515 case LY_STMT_EXTENSION_INSTANCE:
4516 /* no place in the statement order defined */
4517 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004518 default:
4519 /* error handled in the next switch */
4520 break;
4521 }
4522#undef CHECK_ORDER
4523
4524 prev_kw = kw;
4525 switch (kw) {
4526 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004527 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004528 LY_CHECK_RET(parse_yangversion(ctx, (struct lysp_module *)submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004529 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004530 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02004531 LY_CHECK_RET(parse_belongsto(ctx, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004532 break;
4533
4534 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004535 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004536 if (submod->version == LYS_VERSION_1_1) {
4537 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4538 submod->name);
4539 }
Radek Krejci33090f92020-12-17 20:12:46 +01004540 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004541 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004542 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004543 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004544 break;
4545
4546 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004547 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004548 LY_CHECK_RET(parse_text_field(ctx, submod, LY_STMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004549 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004550 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004551 LY_CHECK_RET(parse_text_field(ctx, submod, LY_STMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004552 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004553 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004554 LY_CHECK_RET(parse_text_field(ctx, submod->dsc, LY_STMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004556 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004557 LY_CHECK_RET(parse_text_field(ctx, submod->ref, LY_STMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004558 break;
4559
4560 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004561 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004562 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004563 break;
4564
4565 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004566 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004567 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004568 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004569 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004570 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004571 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004572 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004573 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004575 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004576 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004578 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004579 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004580 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004581 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004582 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004583 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004584 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004585 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004586 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004587 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004588 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004589 break;
4590
Radek Krejcid6b76452019-09-03 17:03:03 +02004591 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004592 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004594 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004595 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004596 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004597 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004598 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004599 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004600 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004601 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004603 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004604 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004606 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004607 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004609 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004610 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004611 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004612 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004613 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004615 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004616 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004618 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004619 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004620 break;
4621
4622 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004623 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004624 return LY_EVALID;
4625 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004626 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004627 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004628
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004629 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004630 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004631 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004632 return LY_EVALID;
4633 }
4634
4635 /* submodules share the namespace with the module names, so there must not be
4636 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004637 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004638 /* main modules may have different revisions */
4639 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004640 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004641 return LY_EVALID;
4642 }
4643
Michal Vasko12ef5362022-09-16 15:13:58 +02004644cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004645 return ret;
4646}
4647
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004648/**
4649 * @brief Skip any redundant characters, namely whitespaces and comments.
4650 *
4651 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004652 * @return LY_SUCCESS on success.
4653 * @return LY_EVALID on invalid comment.
4654 */
4655static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004656skip_redundant_chars(struct lysp_yang_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004657{
4658 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004659 while (ctx->in->current[0]) {
4660 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004661 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004662 ly_in_skip(ctx->in, 2);
4663 LY_CHECK_RET(skip_comment(ctx, 1));
4664 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004665 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004666 ly_in_skip(ctx->in, 2);
4667 LY_CHECK_RET(skip_comment(ctx, 2));
4668 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004669 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004670 if (ctx->in->current[0] == '\n') {
4671 LY_IN_NEW_LINE(ctx->in);
4672 }
Radek Krejci33090f92020-12-17 20:12:46 +01004673 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004674 } else {
4675 break;
4676 }
4677 }
4678
4679 return LY_SUCCESS;
4680}
4681
Radek Krejcid4557c62018-09-17 11:42:09 +02004682LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004683yang_parse_submodule(struct lysp_yang_ctx **context, struct ly_ctx *ly_ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004684 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004685{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004686 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004687 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004688 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004689 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004690 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004691 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004692
aPiecek56be92a2021-07-01 07:18:10 +02004693 assert(context && ly_ctx && main_ctx && in && submod);
4694
David Sedlák1b623122019-08-05 15:27:49 +02004695 /* create context */
4696 *context = calloc(1, sizeof **context);
4697 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004698 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004699 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004700 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004701
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004702 mod_p = calloc(1, sizeof *mod_p);
4703 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004704 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004705 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004706
4707 /* use main context parsed mods adding the current one */
4708 (*context)->parsed_mods = main_ctx->parsed_mods;
4709 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004710
Michal Vaskof8ebf132022-11-21 14:06:48 +01004711 LOG_LOCSET(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004712
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004713 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004714 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004715 LY_CHECK_GOTO(ret, cleanup);
4716
Michal Vasko7fbc8162018-09-17 10:35:16 +02004717 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004718 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004719 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004720
Radek Krejcid6b76452019-09-03 17:03:03 +02004721 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004722 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004723 ret = LY_EINVAL;
4724 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004725 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004726 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004727 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004728 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004729 }
4730
Michal Vasko7fbc8162018-09-17 10:35:16 +02004731 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004732 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004733 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004734
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004735 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004736 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004737 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004738 if (in->current[0]) {
4739 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004740 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004741 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004742 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004743
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004744 mod_p->parsing = 0;
4745 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004746
Radek Krejcibbe09a92018-11-08 09:36:54 +01004747cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004748 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004749 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004750 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004751 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004752 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004753 }
4754
4755 return ret;
4756}
4757
4758LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004759yang_parse_module(struct lysp_yang_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004760{
4761 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004762 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004763 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004764 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004765 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004766 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004767
David Sedlák1b623122019-08-05 15:27:49 +02004768 /* create context */
4769 *context = calloc(1, sizeof **context);
4770 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004771 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004772 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004773 (*context)->in = in;
Michal Vaskod0625d72022-10-06 15:02:50 +02004774 (*context)->main_ctx = (struct lysp_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004775
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004776 mod_p = calloc(1, sizeof *mod_p);
4777 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4778 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004779 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004780
Michal Vaskof8ebf132022-11-21 14:06:48 +01004781 LOG_LOCSET(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004782
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004783 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004784 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004785 LY_CHECK_GOTO(ret, cleanup);
4786
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004787 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004788 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004789 LY_CHECK_GOTO(ret, cleanup);
4790
Radek Krejcid6b76452019-09-03 17:03:03 +02004791 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004792 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 +01004793 ret = LY_EINVAL;
4794 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004795 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004796 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004797 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004798 goto cleanup;
4799 }
4800
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004801 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004802 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004803 LY_CHECK_GOTO(ret, cleanup);
4804
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004805 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004806 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004807 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004808 if (in->current[0]) {
4809 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004810 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004811 goto cleanup;
4812 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004813
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004814 mod->parsed = mod_p;
4815
4816cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004817 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004818 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004819 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004820 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004821 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004822 }
4823
Michal Vasko7fbc8162018-09-17 10:35:16 +02004824 return ret;
4825}