blob: 3a1fe1778580516f69b454f7173fe83c8ebf5009 [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 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020064 * @param[in] CTX yang parser context for logging.
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) { \
David Sedlákb3077192019-06-19 10:55:37 +020079 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_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 *
90 * @param[in] CTX yang parser context for logging.
91 * @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) { \
103 if (EXTS && (RET = ly_set_add(&(CTX)->ext_inst, (EXTS), 1, NULL))) { \
104 goto ERR_LABEL; \
105 } \
106 __loop_end = 1; \
107 }
108
109LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci33090f92020-12-17 20:12:46 +0100110LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
111LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
112LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
113LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100114LY_ERR parse_grouping(struct lys_yang_parser_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
Radek Krejci33090f92020-12-17 20:12:46 +0100167buf_store_char(struct lys_yang_parser_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 Vaskob36053d2020-03-26 15:49:30 +0100191 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200192 break;
193 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100194 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_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 Vaskob36053d2020-03-26 15:49:30 +0100198 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_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
Radek Krejci33090f92020-12-17 20:12:46 +0100249skip_comment(struct lys_yang_parser_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
Radek Krejci33090f92020-12-17 20:12:46 +0100322read_qstring(struct lys_yang_parser_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:
Radek Krejci33090f92020-12-17 20:12:46 +0100352 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200353 case '\'':
354 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100355 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100356 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200357 break;
358 default:
359 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100360 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 +0200361 break;
362 }
363 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100364 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100365 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200366 case '\"':
367 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100368 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100369 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200370 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200371 break;
372 case '\\':
373 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100374 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200375
376 /* the backslash sequence is substituted, so we will need a buffer to store the result */
377 need_buf = 1;
378
379 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100380 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200381
382 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
383 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
384 trailing_ws = 0;
385
386 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
387 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200388 break;
389 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200390 if (current_indent < block_indent) {
391 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100392 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200393 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100394 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100395 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 +0100396 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200397 }
398 break;
399 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200400 if (current_indent < block_indent) {
401 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100402 current_indent += Y_TAB_SPACES;
403 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200404 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200405 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100406 c = ctx->in->current;
407 ctx->in->current = " ";
408 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
409 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100410 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200411 }
Radek Krejci33090f92020-12-17 20:12:46 +0100412 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200413 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100414 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100415 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 +0100416 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200417 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100418 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200419 }
420 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200421 case '\r':
422 if (ctx->in->current[1] != '\n') {
423 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
424 return LY_EVALID;
425 }
426 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200427 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200428 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 /* we will be removing the indents so we need our own buffer */
430 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200431
432 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200433 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200434
Radek Krejciff13cd12019-10-25 15:34:24 +0200435 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200436 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200437 }
438
439 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100440 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 +0200441
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200442 /* reset context indentation counter for possible string after this one */
443 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200444 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200445 break;
446 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200447 /* first non-whitespace character, stop eating indentation */
448 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200449
450 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100451 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 +0200452 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200453 break;
454 }
455 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100456 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200457 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100458 c = ctx->in->current;
459 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200460 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100461 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100462 /* fix false newline count in buf_store_char() */
463 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200464 break;
465 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100466 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 break;
468 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200469 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200471 break;
472 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200473 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100474 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200475 return LY_EVALID;
476 }
477
478 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100479 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 +0200480
Radek Krejcif13b87b2020-12-01 22:02:17 +0100481 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100482 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200483 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100484 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100485 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200486 case '+':
487 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100488 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200489 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200490 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200491 case '\r':
492 if (ctx->in->current[1] != '\n') {
493 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
494 return LY_EVALID;
495 }
496 MOVE_INPUT(ctx, 1);
497 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200498 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100499 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100500 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200501 case ' ':
502 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200503 /* just skip */
504 break;
505 default:
506 /* string is finished */
507 goto string_end;
508 }
Radek Krejci33090f92020-12-17 20:12:46 +0100509 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200510 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100511 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100512 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200513 case '\r':
514 if (ctx->in->current[1] != '\n') {
515 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
516 return LY_EVALID;
517 }
518 MOVE_INPUT(ctx, 1);
519 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200520 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100521 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100522 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200523 case ' ':
524 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200525 /* skip */
526 break;
527 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100528 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200529 break;
530 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100531 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200532 break;
533 default:
534 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200535 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200536 return LY_EVALID;
537 }
Radek Krejci33090f92020-12-17 20:12:46 +0100538 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200539 break;
540 default:
541 return LY_EINT;
542 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200543 }
544
545string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200546 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200547 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200548 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200549 return LY_EVALID;
550 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200551 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100552
553#undef STRING_ENDED
554#undef STRING_SINGLE_QUOTED
555#undef STRING_DOUBLE_QUOTED
556#undef STRING_DOUBLE_QUOTED_ESCAPED
557#undef STRING_PAUSED_NEXTSTRING
558#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200559}
560
Michal Vaskoea5abea2018-09-18 13:10:54 +0200561/**
562 * @brief Get another YANG string from the raw data.
563 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200564 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200565 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200566 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
567 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200568 * @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 +0200569 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
570 * set to NULL. Otherwise equal to \p word_p.
571 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200572 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200573 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200574LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100575get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200576 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200577{
Michal Vaskob68ea142021-04-26 13:46:00 +0200578 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200579 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200580 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200581
Michal Vasko7fbc8162018-09-17 10:35:16 +0200582 /* word buffer - dynamically allocated */
583 *word_b = NULL;
584
585 /* word pointer - just a pointer to data */
586 *word_p = NULL;
587
588 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100589 while (ctx->in->current[0]) {
590 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200591 case '\'':
592 case '\"':
593 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200594 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100595 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200596 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200597 ret = LY_EVALID;
598 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200599 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200600 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100601 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200602 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200603 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200604 if (!*word_p) {
605 /* do not return NULL word */
606 *word_p = "";
607 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200608 goto str_end;
609 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100610 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200611 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100612 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200613 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100614 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200615 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100616 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200617 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200618 } else {
619 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200620 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 +0200621 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200622 break;
623 case ' ':
624 if (*word_len) {
625 /* word is finished */
626 goto str_end;
627 }
Radek Krejci33090f92020-12-17 20:12:46 +0100628 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200629 break;
630 case '\t':
631 if (*word_len) {
632 /* word is finished */
633 goto str_end;
634 }
635 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100636 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200637
Radek Krejci33090f92020-12-17 20:12:46 +0100638 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200639 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200640 case '\r':
641 if (ctx->in->current[1] != '\n') {
642 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200643 ret = LY_EVALID;
644 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200645 }
646 MOVE_INPUT(ctx, 1);
647 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200648 case '\n':
649 if (*word_len) {
650 /* word is finished */
651 goto str_end;
652 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100653 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100654 MOVE_INPUT(ctx, 1);
655
Michal Vasko7fbc8162018-09-17 10:35:16 +0200656 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200657 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200658 break;
659 case ';':
660 case '{':
661 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
662 /* word is finished */
663 goto str_end;
664 }
665
Radek Krejci33090f92020-12-17 20:12:46 +0100666 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200667 ret = LY_EVALID;
668 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200669 case '}':
670 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100671 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200672 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200673 ret = LY_EVALID;
674 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200675 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200676 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 +0200677 break;
678 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200679 }
680
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200681 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200682 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200683 ret = LY_EVALID;
684 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200685
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200687 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200689 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200690 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200691 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200692 *word_p = *word_b;
693 }
694
695 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200696
697error:
698 free(*word_b);
699 *word_b = NULL;
700 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200701}
702
Michal Vaskoea5abea2018-09-18 13:10:54 +0200703/**
704 * @brief Get another YANG keyword from the raw data.
705 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200706 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200707 * @param[out] kw YANG keyword read.
708 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
709 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
710 *
711 * @return LY_ERR values.
712 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200713LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100714get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200716 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200717 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200718 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200719
720 if (word_p) {
721 *word_p = NULL;
722 *word_len = 0;
723 }
724
725 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100726 while (ctx->in->current[0]) {
727 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200728 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100729 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100731 MOVE_INPUT(ctx, 2);
732 LY_CHECK_RET(skip_comment(ctx, 1));
733 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200734 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100735 MOVE_INPUT(ctx, 2);
736 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200738 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200739 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200740 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200741 }
Radek Krejci13028282019-06-11 14:56:48 +0200742 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200743 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200744 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100745 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200746 ctx->indent = 0;
747 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200749 /* skip whitespaces (optsep) */
750 ++ctx->indent;
751 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200753 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100754 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200755 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100756 case '\r':
757 /* possible CRLF endline */
758 if (ctx->in->current[1] == '\n') {
759 break;
760 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100761 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200762 default:
763 /* either a keyword start or an invalid character */
764 goto keyword_start;
765 }
766
Radek Krejci33090f92020-12-17 20:12:46 +0100767 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200768 }
769
770keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100771 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100772 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200773
aPiecek93582ed2021-05-25 14:49:06 +0200774 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
775 goto success;
776 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
777 ctx->depth++;
778 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100779 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200780 return LY_EINVAL;
781 }
782 goto success;
783 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
784 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200785 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200786 }
787
Radek Krejcid6b76452019-09-03 17:03:03 +0200788 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200789 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100790 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200791 case '\r':
792 if (ctx->in->current[1] != '\n') {
793 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
794 return LY_EVALID;
795 }
796 MOVE_INPUT(ctx, 1);
797 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200798 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200799 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200800 case ' ':
801 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200802 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200803 case ':':
804 /* keyword is not actually a keyword, but prefix of an extension.
805 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
806 * and we will be checking the keyword (extension instance) itself */
807 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100808 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200809 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200810 case '{':
811 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200812 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200813 break;
814 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100815 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200816 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100817 MOVE_INPUT(ctx, 1);
818 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200819 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200820 return LY_EVALID;
821 }
822 } else {
823 /* still can be an extension */
824 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200825
Radek Krejci156ccaf2018-10-15 15:49:17 +0200826extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100827 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200828 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
829 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200830 uint32_t c = 0;
831
Radek Krejci33090f92020-12-17 20:12:46 +0100832 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
833 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200834 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200835 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200836 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100837 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200838 }
Radek Krejci33090f92020-12-17 20:12:46 +0100839 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200840 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841 return LY_EVALID;
842 }
843
844 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200845 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100846 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200847 return LY_EVALID;
848 }
849
Radek Krejcid6b76452019-09-03 17:03:03 +0200850 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200851 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200852
Radek Krejci626df482018-10-11 15:06:31 +0200853success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200854 if (word_p) {
855 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100856 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200857 }
858
859 return LY_SUCCESS;
860}
861
Michal Vaskoea5abea2018-09-18 13:10:54 +0200862/**
863 * @brief Parse extension instance substatements.
864 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200865 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200866 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200867 * @param[in] word Extension instance substatement name (keyword).
868 * @param[in] word_len Extension instance substatement name length.
869 * @param[in,out] child Children of this extension instance to add to.
870 *
871 * @return LY_ERR values.
872 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200873static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100874parse_ext_substmt(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200875 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876{
877 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100878 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200879 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200880 struct lysp_stmt *stmt, *par_child;
881
882 stmt = calloc(1, sizeof *stmt);
883 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
884
Radek Krejcibb9b1982019-04-08 14:24:59 +0200885 /* insert into parent statements */
886 if (!*child) {
887 *child = stmt;
888 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200889 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200890 par_child->next = stmt;
891 }
892
Michal Vaskofc2cd072021-02-24 13:17:17 +0100893 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200894 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895
896 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100897 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200898 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200899 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200900 }
901
Radek Krejci8df109d2021-04-23 12:19:08 +0200902 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100903 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100904 stmt->kw = kw;
905
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200906 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
907 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
908 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200909 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200910
911cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912 return ret;
913}
914
Michal Vaskoea5abea2018-09-18 13:10:54 +0200915/**
916 * @brief Parse extension instance.
917 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200918 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200919 * @param[in] ext_name Extension instance substatement name (keyword).
920 * @param[in] ext_name_len Extension instance substatement name length.
Radek Krejcifc596f92021-02-26 22:40:26 +0100921 * @param[in] insubstmt The statement this extension instance is a substatement of.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200922 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
923 * @param[in,out] exts Extension instances to add to.
924 *
925 * @return LY_ERR values.
926 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100928parse_ext(struct lys_yang_parser_ctx *ctx, const char *ext_name, size_t ext_name_len, enum ly_stmt insubstmt,
Radek Krejci0f969882020-08-21 16:56:47 +0200929 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200930{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100931 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200933 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200934 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200935 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200937 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200938
Michal Vaskofc2cd072021-02-24 13:17:17 +0100939 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
940 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
941 return LY_EVALID;
942 }
943
944 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200945 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200946
947 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100948 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200949 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200950 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951 }
952
Michal Vaskofc2cd072021-02-24 13:17:17 +0100953 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200954 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200955 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100956 e->prefix_data = PARSER_CUR_PMOD(ctx);
Radek Krejciab430862021-03-02 20:13:40 +0100957 e->parent_stmt = insubstmt;
958 e->parent_stmt_index = insubstmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100959
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200960 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
961 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
962 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200963 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200964
965cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200966 return ret;
967}
968
Michal Vaskoea5abea2018-09-18 13:10:54 +0200969/**
970 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
971 * description, etc...
972 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200973 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200974 * @param[in] substmt Type of this substatement.
975 * @param[in] substmt_index Index of this substatement.
976 * @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.
979 *
980 * @return LY_ERR values.
981 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200982static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100983parse_text_field(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200984 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200985{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100986 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200988 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200989 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990
991 if (*value) {
Radek Krejci3972b332021-03-02 16:34:31 +0100992 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200993 return LY_EVALID;
994 }
995
996 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100997 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200998
999 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +02001000 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001002 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001003 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001004 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001005 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006 break;
1007 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001008 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009 return LY_EVALID;
1010 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001011 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001012 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001013
1014cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001015 return ret;
1016}
1017
Michal Vaskoea5abea2018-09-18 13:10:54 +02001018/**
1019 * @brief Parse the yang-version statement.
1020 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001021 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001022 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +02001023 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001024 *
1025 * @return LY_ERR values.
1026 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001027static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001028parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001029{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001030 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001032 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001033 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001034
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001035 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +02001036 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037 return LY_EVALID;
1038 }
1039
1040 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001041 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001042
Radek Krejci96e48da2020-09-04 13:18:06 +02001043 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001044 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001045 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001046 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001047 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001048 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001049 free(buf);
1050 return LY_EVALID;
1051 }
1052 free(buf);
1053
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001054 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001056 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001057 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 break;
1059 default:
David Sedlákb3077192019-06-19 10:55:37 +02001060 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 return LY_EVALID;
1062 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001063 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001065
1066cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067 return ret;
1068}
1069
Michal Vaskoea5abea2018-09-18 13:10:54 +02001070/**
1071 * @brief Parse the belongs-to statement.
1072 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001073 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001074 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1075 * @param[in,out] exts Extension instances to add to.
1076 *
1077 * @return LY_ERR values.
1078 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001080parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001082 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001084 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001085 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001086
Michal Vaskoc3781c32020-10-06 14:04:08 +02001087 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001088 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 return LY_EVALID;
1090 }
1091
Michal Vaskoc3781c32020-10-06 14:04:08 +02001092 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001093 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001094 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001095 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 +01001096 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001097 free(buf);
1098 return LY_EVALID;
1099 }
1100 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001101
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001102 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001103 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001104 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001105 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001106 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001107 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001108 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_BELONGS_TO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001109 break;
1110 default:
David Sedlákb3077192019-06-19 10:55:37 +02001111 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112 return LY_EVALID;
1113 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001114 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001115 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001116
Michal Vasko7fbc8162018-09-17 10:35:16 +02001117 /* mandatory substatements */
1118 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001119 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001120 return LY_EVALID;
1121 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001122
1123cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124 return ret;
1125}
1126
Michal Vaskoea5abea2018-09-18 13:10:54 +02001127/**
1128 * @brief Parse the revision-date statement.
1129 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001130 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001131 * @param[in,out] rev Array to store the parsed value in.
1132 * @param[in,out] exts Extension instances to add to.
1133 *
1134 * @return LY_ERR values.
1135 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001137parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001138{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001139 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001140 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001141 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001142 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143
1144 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001145 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001146 return LY_EVALID;
1147 }
1148
1149 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001150 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001151
1152 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001153 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001154 free(buf);
1155 return LY_EVALID;
1156 }
1157
1158 /* store value and spend buf if allocated */
1159 strncpy(rev, word, word_len);
1160 free(buf);
1161
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001162 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001163 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001164 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001165 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001166 break;
1167 default:
David Sedlákb3077192019-06-19 10:55:37 +02001168 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169 return LY_EVALID;
1170 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001171 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001173
1174cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175 return ret;
1176}
1177
Michal Vaskoea5abea2018-09-18 13:10:54 +02001178/**
1179 * @brief Parse the include statement.
1180 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001181 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001182 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001183 * @param[in,out] includes Parsed includes to add to.
1184 *
1185 * @return LY_ERR values.
1186 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001187static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001188parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189{
Radek Krejcid33273d2018-10-25 14:55:52 +02001190 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001192 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001193 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 struct lysp_include *inc;
1195
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001196 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197
1198 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001199 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200
Michal Vasko12ef5362022-09-16 15:13:58 +02001201 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001202
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001203 /* submodules share the namespace with the module names, so there must not be
1204 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001205 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001206 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001207 return LY_EVALID;
1208 }
1209
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001210 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001212 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001213 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001214 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001216 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001217 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001218 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001220 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001221 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001223 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001224 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001225 break;
1226 default:
David Sedlákb3077192019-06-19 10:55:37 +02001227 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001228 return LY_EVALID;
1229 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001230 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001232
1233cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234 return ret;
1235}
1236
Michal Vaskoea5abea2018-09-18 13:10:54 +02001237/**
1238 * @brief Parse the import statement.
1239 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001240 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001241 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001242 * @param[in,out] imports Parsed imports to add to.
1243 *
1244 * @return LY_ERR values.
1245 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001247parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001249 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001250 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001251 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001252 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001253 struct lysp_import *imp;
1254
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001255 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001256
1257 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001258 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001259 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001260
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001261 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001262 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001263 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001264 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskob36053d2020-03-26 15:49:30 +01001265 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001267 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001268 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001269 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001271 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001272 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001273 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001274 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001275 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001276 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001277 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001278 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001279 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280 break;
1281 default:
David Sedlákb3077192019-06-19 10:55:37 +02001282 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001283 return LY_EVALID;
1284 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001285 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001286 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001287
Michal Vasko7fbc8162018-09-17 10:35:16 +02001288 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001289 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001290
Michal Vasko12ef5362022-09-16 15:13:58 +02001291cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001292 return ret;
1293}
1294
Michal Vaskoea5abea2018-09-18 13:10:54 +02001295/**
1296 * @brief Parse the revision statement.
1297 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001298 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001299 * @param[in,out] revs Parsed revisions to add to.
1300 *
1301 * @return LY_ERR values.
1302 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001304parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001305{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001306 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001307 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001308 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001309 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310 struct lysp_revision *rev;
1311
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001312 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001313
1314 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001315 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316
1317 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001318 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001319 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001320 return LY_EVALID;
1321 }
1322
Radek Krejcib7db73a2018-10-24 14:18:40 +02001323 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324 free(buf);
1325
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001326 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001328 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001329 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001331 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001332 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001334 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001335 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336 break;
1337 default:
David Sedlákb3077192019-06-19 10:55:37 +02001338 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 return LY_EVALID;
1340 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001341 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001342 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001343
1344cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345 return ret;
1346}
1347
Michal Vaskoea5abea2018-09-18 13:10:54 +02001348/**
1349 * @brief Parse a generic text field that can have more instances such as base.
1350 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001351 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001352 * @param[in] substmt Type of this substatement.
1353 * @param[in,out] texts Parsed values to add to.
1354 * @param[in] arg Type of the expected argument.
1355 * @param[in,out] exts Extension instances to add to.
1356 *
1357 * @return LY_ERR values.
1358 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001359static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001360parse_text_fields(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, const char ***texts, enum yang_arg arg,
Radek Krejci0f969882020-08-21 16:56:47 +02001361 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001363 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001365 const char **item;
1366 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001367 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368
1369 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001370 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371
1372 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001373 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374
Michal Vasko12ef5362022-09-16 15:13:58 +02001375 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001376 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001377 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001378 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001379 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001380 break;
1381 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001382 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383 return LY_EVALID;
1384 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001385 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001387
1388cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001389 return ret;
1390}
1391
Michal Vaskoea5abea2018-09-18 13:10:54 +02001392/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001393 * @brief Parse a generic text field that can have more instances such as base.
1394 *
1395 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001396 * @param[in] substmt Type of this substatement.
1397 * @param[in,out] qnames Parsed qnames to add to.
1398 * @param[in] arg Type of the expected argument.
1399 * @param[in,out] exts Extension instances to add to.
1400 *
1401 * @return LY_ERR values.
1402 */
1403static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001404parse_qnames(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001405 enum yang_arg arg, struct lysp_ext_instance **exts)
1406{
1407 LY_ERR ret = LY_SUCCESS;
1408 char *buf, *word;
1409 struct lysp_qname *item;
1410 size_t word_len;
1411 enum ly_stmt kw;
1412
1413 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001414 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001415
1416 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001417 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001418
Michal Vasko12ef5362022-09-16 15:13:58 +02001419 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001420 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001421 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001422 switch (kw) {
1423 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001424 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001425 break;
1426 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001427 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001428 return LY_EVALID;
1429 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001430 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001431 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001432
1433cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001434 return ret;
1435}
1436
1437/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001438 * @brief Parse the config statement.
1439 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001440 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001441 * @param[in,out] flags Flags to add to.
1442 * @param[in,out] exts Extension instances to add to.
1443 *
1444 * @return LY_ERR values.
1445 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001446static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001447parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001449 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001451 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001452 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001453
1454 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001455 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001456 return LY_EVALID;
1457 }
1458
1459 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001460 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461
Radek Krejcif13b87b2020-12-01 22:02:17 +01001462 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001464 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465 *flags |= LYS_CONFIG_R;
1466 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001467 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468 free(buf);
1469 return LY_EVALID;
1470 }
1471 free(buf);
1472
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001473 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001474 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001475 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001476 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001477 break;
1478 default:
David Sedlákb3077192019-06-19 10:55:37 +02001479 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001480 return LY_EVALID;
1481 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001482 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001484
1485cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486 return ret;
1487}
1488
Michal Vaskoea5abea2018-09-18 13:10:54 +02001489/**
1490 * @brief Parse the mandatory statement.
1491 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001492 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001493 * @param[in,out] flags Flags to add to.
1494 * @param[in,out] exts Extension instances to add to.
1495 *
1496 * @return LY_ERR values.
1497 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001499parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001501 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001502 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001503 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001504 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505
1506 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001507 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 return LY_EVALID;
1509 }
1510
1511 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001512 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513
Radek Krejcif13b87b2020-12-01 22:02:17 +01001514 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001516 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 *flags |= LYS_MAND_FALSE;
1518 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001519 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 free(buf);
1521 return LY_EVALID;
1522 }
1523 free(buf);
1524
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001525 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001527 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001528 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001529 break;
1530 default:
David Sedlákb3077192019-06-19 10:55:37 +02001531 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001532 return LY_EVALID;
1533 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001534 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001536
1537cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538 return ret;
1539}
1540
Michal Vaskoea5abea2018-09-18 13:10:54 +02001541/**
1542 * @brief Parse a restriction such as range or length.
1543 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001544 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001545 * @param[in] restr_kw Type of this particular restriction.
1546 * @param[in,out] exts Extension instances to add to.
1547 *
1548 * @return LY_ERR values.
1549 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001551parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001553 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001555 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001556 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001557
1558 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001559 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001560
Michal Vaskob36053d2020-03-26 15:49:30 +01001561 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001562 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001563 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001564 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001566 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001567 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001569 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001570 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001572 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01001573 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001575 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001576 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001578 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001579 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 break;
1581 default:
David Sedlákb3077192019-06-19 10:55:37 +02001582 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001583 return LY_EVALID;
1584 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001585 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001587
1588cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001589 return ret;
1590}
1591
Michal Vaskoea5abea2018-09-18 13:10:54 +02001592/**
1593 * @brief Parse a restriction that can have more instances such as must.
1594 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001595 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001596 * @param[in] restr_kw Type of this particular restriction.
1597 * @param[in,out] restrs Restrictions to add to.
1598 *
1599 * @return LY_ERR values.
1600 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001601static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001602parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603{
1604 struct lysp_restr *restr;
1605
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001606 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001607 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001608}
1609
Michal Vaskoea5abea2018-09-18 13:10:54 +02001610/**
1611 * @brief Parse the status statement.
1612 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001613 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001614 * @param[in,out] flags Flags to add to.
1615 * @param[in,out] exts Extension instances to add to.
1616 *
1617 * @return LY_ERR values.
1618 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001619static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001620parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001622 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001623 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001624 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001626
1627 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001628 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001629 return LY_EVALID;
1630 }
1631
1632 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001633 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634
Radek Krejcif13b87b2020-12-01 22:02:17 +01001635 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001637 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001639 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640 *flags |= LYS_STATUS_OBSLT;
1641 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001642 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001643 free(buf);
1644 return LY_EVALID;
1645 }
1646 free(buf);
1647
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001648 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001649 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001650 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001651 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001652 break;
1653 default:
David Sedlákb3077192019-06-19 10:55:37 +02001654 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001655 return LY_EVALID;
1656 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001657 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001659
1660cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 return ret;
1662}
1663
Michal Vaskoea5abea2018-09-18 13:10:54 +02001664/**
1665 * @brief Parse the when statement.
1666 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001667 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001668 * @param[in,out] when_p When pointer to parse to.
1669 *
1670 * @return LY_ERR values.
1671 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001672LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001673parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001674{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001675 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001676 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001677 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001678 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001680 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681
1682 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001683 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001684 return LY_EVALID;
1685 }
1686
1687 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001688 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001689
1690 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001691 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001692 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001693 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001694
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001695 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001697 case LY_STMT_DESCRIPTION:
Michal Vasko12ef5362022-09-16 15:13:58 +02001698 LY_CHECK_GOTO(ret = parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001700 case LY_STMT_REFERENCE:
Michal Vasko12ef5362022-09-16 15:13:58 +02001701 LY_CHECK_GOTO(ret = parse_text_field(ctx, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001702 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001703 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko12ef5362022-09-16 15:13:58 +02001704 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, LY_STMT_WHEN, 0, &when->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001705 break;
1706 default:
David Sedlákb3077192019-06-19 10:55:37 +02001707 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001708 ret = LY_EVALID;
1709 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001711 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001712 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001713
1714cleanup:
1715 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001716 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001717 free(when);
1718 } else {
1719 *when_p = when;
1720 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001721 return ret;
1722}
1723
Michal Vaskoea5abea2018-09-18 13:10:54 +02001724/**
1725 * @brief Parse the anydata or anyxml statement.
1726 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001727 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001728 * @param[in] any_kw Type of this particular keyword.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001729 * @param[in,out] siblings Siblings to add to.
1730 *
1731 * @return LY_ERR values.
1732 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001733LY_ERR
Radek Krejci39b7fc22021-02-26 23:29:18 +01001734parse_any(struct lys_yang_parser_ctx *ctx, enum ly_stmt any_kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001736 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001738 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001740 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001741
David Sedlák60adc092019-08-06 15:57:02 +02001742 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001743 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001744
Radek Krejci39b7fc22021-02-26 23:29:18 +01001745 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001746 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001747
Michal Vasko7fbc8162018-09-17 10:35:16 +02001748 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001749 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001750 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001751
1752 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001753 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001755 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001756 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001757 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001758 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001759 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001760 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001761 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001762 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001764 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001765 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001767 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001768 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001770 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001771 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001773 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001774 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001775 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001776 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001777 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001778 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001779 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001780 LY_CHECK_RET(parse_ext(ctx, word, word_len, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001781 break;
1782 default:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001783 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001784 return LY_EVALID;
1785 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001786 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001787 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001788
1789cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001790 return ret;
1791}
1792
Michal Vaskoea5abea2018-09-18 13:10:54 +02001793/**
1794 * @brief Parse the value or position statement. Substatement of type enum statement.
1795 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001796 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001797 * @param[in] val_kw Type of this particular keyword.
1798 * @param[in,out] value Value to write to.
1799 * @param[in,out] flags Flags to write to.
1800 * @param[in,out] exts Extension instances to add to.
1801 *
1802 * @return LY_ERR values.
1803 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001804LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001805parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, enum ly_stmt val_kw, int64_t *value, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001806 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001807{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001808 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001809 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001810 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001811 long long int num = 0;
1812 unsigned long long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001813 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814
1815 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001816 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001817 ret = LY_EVALID;
1818 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 }
1820 *flags |= LYS_SET_VALUE;
1821
1822 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001823 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824
Radek Krejcid6b76452019-09-03 17:03:03 +02001825 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) {
David Sedlákb3077192019-06-19 10:55:37 +02001826 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001827 ret = LY_EVALID;
1828 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001829 }
1830
1831 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001832 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001833 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001834 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001835 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001836 ret = LY_EVALID;
1837 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001838 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001839 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001840 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001841 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001842 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001843 ret = LY_EVALID;
1844 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001845 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846 }
1847 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001848 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001849 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001850 ret = LY_EVALID;
1851 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001852 }
1853 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001854 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_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 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001858 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001859 *value = num;
1860 } else {
1861 *value = unum;
1862 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001863
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001864 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001865 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001866 case LY_STMT_EXTENSION_INSTANCE:
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001867 ret = parse_ext(ctx, word, word_len, val_kw == LY_STMT_VALUE ? LY_STMT_VALUE : LY_STMT_POSITION, 0, exts);
1868 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001869 break;
1870 default:
David Sedlákb3077192019-06-19 10:55:37 +02001871 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001872 ret = LY_EVALID;
1873 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001875 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876 }
Radek Krejci8b764662018-11-14 14:15:13 +01001877
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001878cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001879 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001880 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881}
1882
Michal Vaskoea5abea2018-09-18 13:10:54 +02001883/**
1884 * @brief Parse the enum or bit statement. Substatement of type statement.
1885 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001886 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001887 * @param[in] enum_kw Type of this particular keyword.
1888 * @param[in,out] enums Enums or bits to add to.
1889 *
1890 * @return LY_ERR values.
1891 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001892static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001893parse_type_enum(struct lys_yang_parser_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001894{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001895 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001897 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001898 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 struct lysp_type_enum *enm;
1900
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001901 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902
1903 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001904 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 +02001905 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001906 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001907 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001908 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001909
Michal Vasko12ef5362022-09-16 15:13:58 +02001910 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Radek Krejci8b764662018-11-14 14:15:13 +01001911 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1912
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001913 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001914 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001915 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001916 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001917 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001918 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001919 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001920 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001922 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001923 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001925 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001926 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001928 case LY_STMT_VALUE:
1929 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Michal Vasko69730152020-10-09 16:30:07 +02001930 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001931 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001932 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001933 case LY_STMT_POSITION:
1934 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Michal Vasko69730152020-10-09 16:30:07 +02001935 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001936 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001938 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001939 LY_CHECK_RET(parse_ext(ctx, word, word_len, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001940 break;
1941 default:
David Sedlákb3077192019-06-19 10:55:37 +02001942 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001943 return LY_EVALID;
1944 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001945 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001947
1948cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949 return ret;
1950}
1951
Michal Vaskoea5abea2018-09-18 13:10:54 +02001952/**
1953 * @brief Parse the fraction-digits statement. Substatement of type statement.
1954 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001955 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001956 * @param[in,out] fracdig Value to write to.
1957 * @param[in,out] exts Extension instances to add to.
1958 *
1959 * @return LY_ERR values.
1960 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001961static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001962parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001963{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001964 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001965 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001966 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001967 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001968 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969
1970 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001971 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001972 return LY_EVALID;
1973 }
1974
1975 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001976 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977
1978 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001979 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001980 ret = LY_EVALID;
1981 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001982 }
1983
1984 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001985 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001986 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001987 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001988 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001989 ret = LY_EVALID;
1990 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001992 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001993 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001994 ret = LY_EVALID;
1995 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001996 }
1997 *fracdig = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001998
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001999 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002000 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002001 case LY_STMT_EXTENSION_INSTANCE:
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002002 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, LY_STMT_FRACTION_DIGITS, 0, exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 break;
2004 default:
David Sedlákb3077192019-06-19 10:55:37 +02002005 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002006 ret = LY_EVALID;
2007 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002009 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002010 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002011
2012cleanup:
2013 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 return ret;
2015}
2016
Michal Vaskoea5abea2018-09-18 13:10:54 +02002017/**
2018 * @brief Parse the require-instance statement. Substatement of type statement.
2019 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002020 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002021 * @param[in,out] reqinst Value to write to.
2022 * @param[in,out] flags Flags to write to.
2023 * @param[in,out] exts Extension instances to add to.
2024 *
2025 * @return LY_ERR values.
2026 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002028parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002029 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002031 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002032 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002033 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002034 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002035
2036 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002037 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038 return LY_EVALID;
2039 }
2040 *flags |= LYS_SET_REQINST;
2041
2042 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002043 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044
Radek Krejcif13b87b2020-12-01 22:02:17 +01002045 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002047 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002048 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002049 ret = LY_EVALID;
2050 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002051 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002053 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002054 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002055 case LY_STMT_EXTENSION_INSTANCE:
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002056 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, LY_STMT_REQUIRE_INSTANCE, 0, exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002057 break;
2058 default:
David Sedlákb3077192019-06-19 10:55:37 +02002059 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002060 ret = LY_EVALID;
2061 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002063 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002065
2066cleanup:
2067 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 return ret;
2069}
2070
Michal Vaskoea5abea2018-09-18 13:10:54 +02002071/**
2072 * @brief Parse the modifier statement. Substatement of type pattern statement.
2073 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002074 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002075 * @param[in,out] pat Value to write to.
2076 * @param[in,out] exts Extension instances to add to.
2077 *
2078 * @return LY_ERR values.
2079 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002081parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002083 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002084 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002085 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002086 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002087
Radek Krejcif13b87b2020-12-01 22:02:17 +01002088 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002089 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090 return LY_EVALID;
2091 }
2092
2093 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002094 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095
Radek Krejcif13b87b2020-12-01 22:02:17 +01002096 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002097 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002098 ret = LY_EVALID;
2099 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002101
2102 /* replace the value in the dictionary */
2103 buf = malloc(strlen(*pat) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002104 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002106 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107
Radek Krejcif13b87b2020-12-01 22:02:17 +01002108 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2109 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002110 LY_CHECK_GOTO(ret = lydict_insert_zc(PARSER_CTX(ctx), buf, pat), cleanup);
2111 buf = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002113 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002115 case LY_STMT_EXTENSION_INSTANCE:
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002116 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, LY_STMT_MODIFIER, 0, exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 break;
2118 default:
David Sedlákb3077192019-06-19 10:55:37 +02002119 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002120 ret = LY_EVALID;
2121 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002123 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002124 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002125
2126cleanup:
2127 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002128 return ret;
2129}
2130
Michal Vaskoea5abea2018-09-18 13:10:54 +02002131/**
2132 * @brief Parse the pattern statement. Substatement of type statement.
2133 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002134 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002135 * @param[in,out] patterns Restrictions to add to.
2136 *
2137 * @return LY_ERR values.
2138 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002140parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002142 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002144 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002145 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 struct lysp_restr *restr;
2147
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002148 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002149
2150 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002151 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152
2153 /* add special meaning first byte */
2154 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002155 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 word = buf;
2157 } else {
2158 buf = malloc(word_len + 2);
2159 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002160 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002161 if (word_len) {
2162 memmove(buf + 1, word, word_len);
2163 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002164 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002165 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002166 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002167 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002169 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002170 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002171 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002172 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002174 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002175 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002177 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01002178 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002180 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002181 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002183 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002184 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002185 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002187 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002188 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 break;
2190 default:
David Sedlákb3077192019-06-19 10:55:37 +02002191 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 return LY_EVALID;
2193 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002194 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002195 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002196
2197cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002198 return ret;
2199}
2200
Michal Vaskoea5abea2018-09-18 13:10:54 +02002201/**
2202 * @brief Parse the type statement.
2203 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002204 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002205 * @param[in,out] type Type to wrote to.
2206 *
2207 * @return LY_ERR values.
2208 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002210parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002212 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002213 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002214 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002215 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002216 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002217 struct lysp_type *nest_type;
2218
2219 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002220 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002221 return LY_EVALID;
2222 }
2223
2224 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002225 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002226 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002227
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002228 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002229 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002230
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002231 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002232 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002233 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002234 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 +01002235 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002237 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002238 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002239 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002241 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002242 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002243 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002244 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002245 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002246 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002247 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002249 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002251 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002252 return LY_EVALID;
2253 }
2254 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002255 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256
Radek Krejci33090f92020-12-17 20:12:46 +01002257 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002258 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002259 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002260 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002261 if (type->path) {
Radek Krejci3972b332021-03-02 16:34:31 +01002262 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002263 return LY_EVALID;
2264 }
2265
aPiecek4bb1e372021-05-07 11:01:00 +02002266 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2267 * corresponding structure, so in case of failure, the lysp_module_free function will take
2268 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2269 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2270 */
2271 LY_CHECK_ERR_RET(ret = parse_text_field(ctx, LY_STMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts),
2272 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002273 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002274 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002275 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002276 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002277 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002278 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002280 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002281 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002282 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002284 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002285 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002286 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002287 return LY_EVALID;
2288 }
2289 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002290 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002291
Radek Krejci33090f92020-12-17 20:12:46 +01002292 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002293 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002294 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002295 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002296 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002297 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002299 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002300 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002301 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002302 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002303 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002304 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002305 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002306 break;
2307 default:
David Sedlákb3077192019-06-19 10:55:37 +02002308 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309 return LY_EVALID;
2310 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002311 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002313
2314cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002315 return ret;
2316}
2317
Michal Vaskoea5abea2018-09-18 13:10:54 +02002318/**
2319 * @brief Parse the leaf statement.
2320 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002321 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002322 * @param[in,out] siblings Siblings to add to.
2323 *
2324 * @return LY_ERR values.
2325 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002326LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002327parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002329 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002331 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002332 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002333 struct lysp_node_leaf *leaf;
2334
David Sedlák60adc092019-08-06 15:57:02 +02002335 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002336 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002338 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002339
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002341 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002342 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343
2344 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002345 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002347 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002348 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002350 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002351 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002352 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002353 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002354 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002355 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002357 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002358 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002360 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002361 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002363 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002364 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002366 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002367 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002369 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002370 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002372 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002373 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002375 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002376 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002378 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002379 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002381 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002382 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383 break;
2384 default:
David Sedlákb3077192019-06-19 10:55:37 +02002385 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002386 return LY_EVALID;
2387 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002388 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002389 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002390
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 /* mandatory substatements */
2392 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002393 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 return LY_EVALID;
2395 }
2396
Michal Vasko12ef5362022-09-16 15:13:58 +02002397cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002398 return ret;
2399}
2400
Michal Vaskoea5abea2018-09-18 13:10:54 +02002401/**
2402 * @brief Parse the max-elements statement.
2403 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002404 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002405 * @param[in,out] max Value to write to.
2406 * @param[in,out] flags Flags to write to.
2407 * @param[in,out] exts Extension instances to add to.
2408 *
2409 * @return LY_ERR values.
2410 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002411LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002412parse_maxelements(struct lys_yang_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002414 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002415 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002416 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002417 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002418 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419
2420 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002421 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 return LY_EVALID;
2423 }
2424 *flags |= LYS_SET_MAX;
2425
2426 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002427 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428
2429 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002430 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002431 ret = LY_EVALID;
2432 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002433 }
2434
Radek Krejci7f9b6512019-09-18 13:11:09 +02002435 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002437 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002439 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002440 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002441 ret = LY_EVALID;
2442 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443 }
2444 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002445 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002446 ret = LY_EVALID;
2447 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448 }
2449
2450 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002451 } else {
2452 /* unbounded */
2453 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454 }
2455 free(buf);
2456
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002457 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002459 case LY_STMT_EXTENSION_INSTANCE:
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002460 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, LY_STMT_MAX_ELEMENTS, 0, exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002461 break;
2462 default:
David Sedlákb3077192019-06-19 10:55:37 +02002463 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002464 ret = LY_EVALID;
2465 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002466 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002467 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002468 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002469
2470cleanup:
2471 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002472 return ret;
2473}
2474
Michal Vaskoea5abea2018-09-18 13:10:54 +02002475/**
2476 * @brief Parse the min-elements statement.
2477 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002478 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002479 * @param[in,out] min Value to write to.
2480 * @param[in,out] flags Flags to write to.
2481 * @param[in,out] exts Extension instances to add to.
2482 *
2483 * @return LY_ERR values.
2484 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002485LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002486parse_minelements(struct lys_yang_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002488 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002489 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002490 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002491 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002492 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493
2494 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002495 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002496 return LY_EVALID;
2497 }
2498 *flags |= LYS_SET_MIN;
2499
2500 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002501 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502
2503 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002504 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002505 ret = LY_EVALID;
2506 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 }
2508
2509 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002510 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002511 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002512 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002513 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002514 ret = LY_EVALID;
2515 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516 }
2517 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002518 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002519 ret = LY_EVALID;
2520 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002521 }
2522 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002524 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002526 case LY_STMT_EXTENSION_INSTANCE:
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002527 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, LY_STMT_MIN_ELEMENTS, 0, exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 break;
2529 default:
David Sedlákb3077192019-06-19 10:55:37 +02002530 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002531 ret = LY_EVALID;
2532 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002534 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002535 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002536
2537cleanup:
2538 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539 return ret;
2540}
2541
Michal Vaskoea5abea2018-09-18 13:10:54 +02002542/**
2543 * @brief Parse the ordered-by statement.
2544 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002545 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002546 * @param[in,out] flags Flags to write to.
2547 * @param[in,out] exts Extension instances to add to.
2548 *
2549 * @return LY_ERR values.
2550 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002551static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002552parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002553{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002554 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002555 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002556 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002557 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558
2559 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002560 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002561 return LY_EVALID;
2562 }
2563
2564 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002565 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002566
Radek Krejcif13b87b2020-12-01 22:02:17 +01002567 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002568 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002569 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570 *flags |= LYS_ORDBY_USER;
2571 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002572 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002573 ret = LY_EVALID;
2574 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002576
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002577 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002579 case LY_STMT_EXTENSION_INSTANCE:
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002580 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, LY_STMT_ORDERED_BY, 0, exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 break;
2582 default:
David Sedlákb3077192019-06-19 10:55:37 +02002583 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002584 ret = LY_EVALID;
2585 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002587 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002588 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002589
2590cleanup:
2591 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002592 return ret;
2593}
2594
Michal Vaskoea5abea2018-09-18 13:10:54 +02002595/**
2596 * @brief Parse the leaf-list statement.
2597 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002598 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002599 * @param[in,out] siblings Siblings to add to.
2600 *
2601 * @return LY_ERR values.
2602 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002603LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002604parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002605{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002606 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002608 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002609 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610 struct lysp_node_leaflist *llist;
2611
David Sedlák60adc092019-08-06 15:57:02 +02002612 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002613 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002615 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002616
Michal Vasko7fbc8162018-09-17 10:35:16 +02002617 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002618 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002619 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620
2621 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002622 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002624 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002625 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002627 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002628 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002629 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002631 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002632 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002634 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002635 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002637 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002638 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002640 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002641 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002643 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002644 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002646 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002647 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002648 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002649 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002650 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002651 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002652 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002653 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002655 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002656 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002658 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002659 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002661 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002662 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002664 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002665 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 break;
2667 default:
David Sedlákb3077192019-06-19 10:55:37 +02002668 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 return LY_EVALID;
2670 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002671 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002673
Michal Vasko7fbc8162018-09-17 10:35:16 +02002674 /* mandatory substatements */
2675 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002676 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002677 return LY_EVALID;
2678 }
2679
Michal Vasko12ef5362022-09-16 15:13:58 +02002680cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681 return ret;
2682}
2683
Michal Vaskoea5abea2018-09-18 13:10:54 +02002684/**
2685 * @brief Parse the refine statement.
2686 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002687 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002688 * @param[in,out] refines Refines to add to.
2689 *
2690 * @return LY_ERR values.
2691 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002693parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002695 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002696 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002697 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002698 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002699 struct lysp_refine *rf;
2700
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002701 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702
2703 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002704 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002705 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002706 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002707
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002708 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002709 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002710 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002711 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002713 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002714 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002715 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002716 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002717 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002719 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002720 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002721 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002722 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002723 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002724 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002725 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002726 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002727 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002729 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002730 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002732 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002733 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002735 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002736 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002738 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002739 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002740 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002741 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002742 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002743 break;
2744 default:
David Sedlákb3077192019-06-19 10:55:37 +02002745 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002746 return LY_EVALID;
2747 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002748 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002750
2751cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 return ret;
2753}
2754
Michal Vaskoea5abea2018-09-18 13:10:54 +02002755/**
2756 * @brief Parse the typedef statement.
2757 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002758 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002759 * @param[in,out] typedefs Typedefs to add to.
2760 *
2761 * @return LY_ERR values.
2762 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002764parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002766 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002768 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002769 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 struct lysp_tpdf *tpdf;
2771
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002772 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773
2774 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002775 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002776 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002777
2778 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002779 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002780 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002781 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002782 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002783 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002785 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002786 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002788 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002789 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002790 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002791 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002792 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002793 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002794 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002795 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002797 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002798 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002800 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002801 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802 break;
2803 default:
David Sedlákb3077192019-06-19 10:55:37 +02002804 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002805 return LY_EVALID;
2806 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002807 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002809
Michal Vasko7fbc8162018-09-17 10:35:16 +02002810 /* mandatory substatements */
2811 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002812 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002813 return LY_EVALID;
2814 }
2815
Radek Krejcibbe09a92018-11-08 09:36:54 +01002816 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002817 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002818 assert(ctx->main_ctx);
2819 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002820 }
2821
Michal Vasko12ef5362022-09-16 15:13:58 +02002822cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823 return ret;
2824}
2825
Michal Vaskoea5abea2018-09-18 13:10:54 +02002826/**
2827 * @brief Parse the input or output statement.
2828 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002829 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002830 * @param[in] kw Type of this particular keyword
2831 * @param[in,out] inout_p Input/output pointer to write to.
2832 *
2833 * @return LY_ERR values.
2834 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002835static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002836parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002837 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002838{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002839 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002841 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002842 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002843 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002845 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002846 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002847 return LY_EVALID;
2848 }
2849
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002850 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002851 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2852 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002853 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854
2855 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002856 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002858 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002859 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002860 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002861 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002862 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002863 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002864 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002865 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002866 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002867 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002868 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002870 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002871 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002872 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002873 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002874 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002875 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002876 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002877 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002878 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002879 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002880 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002882 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002883 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002884 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002885 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002886 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002887 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002889 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002890 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002891 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002892 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002893 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002894 break;
2895 default:
David Sedlákb3077192019-06-19 10:55:37 +02002896 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002897 return LY_EVALID;
2898 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002899 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002900 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002901
Radek Krejci01180ac2021-01-27 08:48:22 +01002902 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002903 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2904 return LY_EVALID;
2905 }
2906
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002907cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002908 return ret;
2909}
2910
Michal Vaskoea5abea2018-09-18 13:10:54 +02002911/**
2912 * @brief Parse the action statement.
2913 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002914 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002915 * @param[in,out] actions Actions to add to.
2916 *
2917 * @return LY_ERR values.
2918 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002919LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002920parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002922 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002924 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002925 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002926 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002927
Radek Krejci2a9fc652021-01-22 17:44:34 +01002928 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929
2930 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002931 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002932 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002933 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002934 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002936 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002937 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002938 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002939 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002940 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002941 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002942 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002944 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002945 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002947 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002948 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949 break;
2950
Radek Krejcid6b76452019-09-03 17:03:03 +02002951 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002952 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002953 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002954 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002955 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002956 break;
2957
Radek Krejcid6b76452019-09-03 17:03:03 +02002958 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002959 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002961 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002962 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002963 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002964 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002965 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent ? LY_STMT_ACTION : LY_STMT_RPC, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002966 break;
2967 default:
David Sedlákb3077192019-06-19 10:55:37 +02002968 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002969 return LY_EVALID;
2970 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002971 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002972 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002973
2974 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2975 if (!act->input.nodetype) {
2976 act->input.nodetype = LYS_INPUT;
2977 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002978 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002979 }
2980 if (!act->output.nodetype) {
2981 act->output.nodetype = LYS_OUTPUT;
2982 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002983 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002984 }
2985
Michal Vasko12ef5362022-09-16 15:13:58 +02002986cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 return ret;
2988}
2989
Michal Vaskoea5abea2018-09-18 13:10:54 +02002990/**
2991 * @brief Parse the notification statement.
2992 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002993 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002994 * @param[in,out] notifs Notifications to add to.
2995 *
2996 * @return LY_ERR values.
2997 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002998LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002999parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003001 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003002 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003003 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003004 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003005 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006
Radek Krejci2a9fc652021-01-22 17:44:34 +01003007 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003008
3009 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003010 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003011 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003012 notif->nodetype = LYS_NOTIF;
3013 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003015 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003017 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003018 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003020 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003021 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003022 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003023 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003024 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003025 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003026 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003027 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028 break;
3029
Radek Krejcid6b76452019-09-03 17:03:03 +02003030 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003031 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02003032 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003033 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003034 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003036 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01003037 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003038 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003039 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003040 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003041 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003042 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003043 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003044 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003045 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003046 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003048 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003049 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003051 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003052 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 break;
3054
Radek Krejcid6b76452019-09-03 17:03:03 +02003055 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003056 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003057 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003059 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003060 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003062 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003063 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003065 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003066 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003067 break;
3068 default:
David Sedlákb3077192019-06-19 10:55:37 +02003069 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003070 return LY_EVALID;
3071 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003072 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003074
Michal Vasko12ef5362022-09-16 15:13:58 +02003075cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003076 return ret;
3077}
3078
Michal Vaskoea5abea2018-09-18 13:10:54 +02003079/**
3080 * @brief Parse the grouping statement.
3081 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003082 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003083 * @param[in,out] groupings Groupings to add to.
3084 *
3085 * @return LY_ERR values.
3086 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003087LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003088parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003090 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003091 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003092 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003093 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003094 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095
Radek Krejci2a9fc652021-01-22 17:44:34 +01003096 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097
3098 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003099 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003100 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003101 grp->nodetype = LYS_GROUPING;
3102 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003103
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003104 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003106 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003107 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003109 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003110 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003112 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003113 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003114 break;
3115
Radek Krejcid6b76452019-09-03 17:03:03 +02003116 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003117 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003118 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003119 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003120 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003122 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003123 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003125 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003126 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003128 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003129 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003131 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003132 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003134 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003135 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003137 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003138 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 break;
3140
Radek Krejcid6b76452019-09-03 17:03:03 +02003141 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003142 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003144 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003145 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003146 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003148 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003149 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003151 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003152 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003153 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003155 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003156 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157 break;
3158 default:
David Sedlákb3077192019-06-19 10:55:37 +02003159 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 return LY_EVALID;
3161 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003162 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003163 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003164
aPiecek63e080d2021-06-29 13:53:28 +02003165 /* store data for collision check */
3166 if (parent) {
3167 assert(ctx->main_ctx);
3168 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3169 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003170
Michal Vasko12ef5362022-09-16 15:13:58 +02003171cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 return ret;
3173}
3174
Michal Vaskoea5abea2018-09-18 13:10:54 +02003175/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003176 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003177 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003178 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003179 * @param[in,out] augments Augments to add to.
3180 *
3181 * @return LY_ERR values.
3182 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003183LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003184parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003186 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003187 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003188 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003189 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003190 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191
Radek Krejci2a9fc652021-01-22 17:44:34 +01003192 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003193
3194 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003195 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003196 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003197 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003198 aug->nodetype = LYS_AUGMENT;
3199 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003200
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003201 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003203 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003204 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003206 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003207 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003209 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003210 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003212 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003213 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003216 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 break;
3218
Radek Krejcid6b76452019-09-03 17:03:03 +02003219 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003220 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003221 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003222 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003223 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003225 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003226 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003228 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003229 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003231 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003232 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003234 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003235 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003237 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003238 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003240 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003241 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003243 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003244 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 break;
3246
Radek Krejcid6b76452019-09-03 17:03:03 +02003247 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003248 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003249 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003250 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003251 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003252 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003253 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003255 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003256 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257 break;
3258 default:
David Sedlákb3077192019-06-19 10:55:37 +02003259 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 return LY_EVALID;
3261 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003262 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003263 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003264
Michal Vasko12ef5362022-09-16 15:13:58 +02003265cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 return ret;
3267}
3268
Michal Vaskoea5abea2018-09-18 13:10:54 +02003269/**
3270 * @brief Parse the uses statement.
3271 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003272 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003273 * @param[in,out] siblings Siblings to add to.
3274 *
3275 * @return LY_ERR values.
3276 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003277LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003278parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003280 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003281 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003282 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003283 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003284 struct lysp_node_uses *uses;
3285
David Sedlák60adc092019-08-06 15:57:02 +02003286 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003287 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003289 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003290
Michal Vasko7fbc8162018-09-17 10:35:16 +02003291 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003292 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003293 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294
3295 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003296 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003298 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003299 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003301 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003302 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003303 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003304 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003305 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003307 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003308 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003309 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003310 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003311 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 break;
3313
Radek Krejcid6b76452019-09-03 17:03:03 +02003314 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003315 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003317 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003318 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003320 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003321 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322 break;
3323 default:
David Sedlákb3077192019-06-19 10:55:37 +02003324 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 return LY_EVALID;
3326 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003327 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003328 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003329
Michal Vasko12ef5362022-09-16 15:13:58 +02003330cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331 return ret;
3332}
3333
Michal Vaskoea5abea2018-09-18 13:10:54 +02003334/**
3335 * @brief Parse the case statement.
3336 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003337 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003338 * @param[in,out] siblings Siblings to add to.
3339 *
3340 * @return LY_ERR values.
3341 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003342LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003343parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003345 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003347 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003348 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 struct lysp_node_case *cas;
3350
David Sedlák60adc092019-08-06 15:57:02 +02003351 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003352 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003354 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003357 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003358 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359
3360 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003361 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003363 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003364 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003366 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003367 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003369 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003370 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003372 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003373 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003375 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003376 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377 break;
3378
Radek Krejcid6b76452019-09-03 17:03:03 +02003379 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003380 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003381 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003382 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003383 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003385 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003386 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003388 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003389 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003391 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003392 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003394 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003395 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003397 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003398 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003400 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003401 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003403 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003404 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405 break;
3406 default:
David Sedlákb3077192019-06-19 10:55:37 +02003407 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 return LY_EVALID;
3409 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003410 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003412
3413cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 return ret;
3415}
3416
Michal Vaskoea5abea2018-09-18 13:10:54 +02003417/**
3418 * @brief Parse the choice statement.
3419 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003420 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003421 * @param[in,out] siblings Siblings to add to.
3422 *
3423 * @return LY_ERR values.
3424 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003425LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003426parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003428 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003430 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003432 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433
David Sedlák60adc092019-08-06 15:57:02 +02003434 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003435 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003436 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003437 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438
Michal Vasko7fbc8162018-09-17 10:35:16 +02003439 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003440 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003441 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442
3443 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003444 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003446 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003447 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003449 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003450 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003452 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003453 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003455 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003456 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003458 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003459 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003461 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003462 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003464 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003465 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003467 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01003468 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
Michal Vasko7f45cf22020-10-01 12:49:44 +02003469 &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003470 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471 break;
3472
Radek Krejcid6b76452019-09-03 17:03:03 +02003473 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003474 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003475 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003476 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003477 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003479 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003480 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003481 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003482 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003483 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003484 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003486 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003487 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003488 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003489 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003490 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003492 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003493 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003495 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003496 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003498 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003499 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 break;
3501 default:
David Sedlákb3077192019-06-19 10:55:37 +02003502 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 return LY_EVALID;
3504 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003505 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003506 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003507
3508cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 return ret;
3510}
3511
Michal Vaskoea5abea2018-09-18 13:10:54 +02003512/**
3513 * @brief Parse the container statement.
3514 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003515 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003516 * @param[in,out] siblings Siblings to add to.
3517 *
3518 * @return LY_ERR values.
3519 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003520LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003521parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522{
3523 LY_ERR ret = 0;
3524 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003525 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527 struct lysp_node_container *cont;
3528
David Sedlák60adc092019-08-06 15:57:02 +02003529 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003530 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003532 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003535 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003536 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537
3538 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003539 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003541 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003542 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003544 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003545 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003547 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003548 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003549 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003550 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003551 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003552 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003553 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003554 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003556 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003557 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003559 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003560 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003561 break;
3562
Radek Krejcid6b76452019-09-03 17:03:03 +02003563 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003564 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003565 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003566 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003567 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003568 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003569 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003570 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003572 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003573 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003575 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003576 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003578 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003579 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003581 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003582 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003583 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003584 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003585 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 break;
3587
Radek Krejcid6b76452019-09-03 17:03:03 +02003588 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003589 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003591 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003592 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003594 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003595 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003596 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003598 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003599 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003601 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003602 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003603 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003605 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003606 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003607 break;
3608 default:
David Sedlákb3077192019-06-19 10:55:37 +02003609 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003610 return LY_EVALID;
3611 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003612 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003614
Michal Vasko12ef5362022-09-16 15:13:58 +02003615cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003616 return ret;
3617}
3618
Michal Vaskoea5abea2018-09-18 13:10:54 +02003619/**
3620 * @brief Parse the list statement.
3621 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003622 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003623 * @param[in,out] siblings Siblings to add to.
3624 *
3625 * @return LY_ERR values.
3626 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003627LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003628parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003630 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003632 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003633 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003634 struct lysp_node_list *list;
3635
David Sedlák60adc092019-08-06 15:57:02 +02003636 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003637 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003639 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003642 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003643 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644
3645 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003646 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003648 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003649 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003651 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003652 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003654 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003655 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003656 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003657 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003658 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003659 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003660 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003661 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003662 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003663 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003664 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003666 case LY_STMT_KEY:
Radek Krejcifc596f92021-02-26 22:40:26 +01003667 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003669 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003670 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003672 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003673 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003675 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003676 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003677 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003678 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003679 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003680 break;
3681
Radek Krejcid6b76452019-09-03 17:03:03 +02003682 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003683 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003684 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003685 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003686 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003688 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003689 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003691 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003692 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003693 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003694 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003695 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003696 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003697 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003698 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003700 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003701 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003702 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003703 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003704 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003705 break;
3706
Radek Krejcid6b76452019-09-03 17:03:03 +02003707 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003708 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003710 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003711 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003712 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003713 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003714 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003715 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003716 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003717 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003718 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003720 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003721 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003722 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003723 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003724 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003725 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726 break;
3727 default:
David Sedlákb3077192019-06-19 10:55:37 +02003728 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003729 return LY_EVALID;
3730 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003731 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003733
Michal Vasko12ef5362022-09-16 15:13:58 +02003734cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 return ret;
3736}
3737
Michal Vaskoea5abea2018-09-18 13:10:54 +02003738/**
3739 * @brief Parse the yin-element statement.
3740 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003741 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003742 * @param[in,out] flags Flags to write to.
3743 * @param[in,out] exts Extension instances to add to.
3744 *
3745 * @return LY_ERR values.
3746 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003748parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003749{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003750 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003751 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003752 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003753 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003754
3755 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003756 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 return LY_EVALID;
3758 }
3759
3760 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003761 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762
Radek Krejcif13b87b2020-12-01 22:02:17 +01003763 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003765 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766 *flags |= LYS_YINELEM_FALSE;
3767 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003768 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769 free(buf);
3770 return LY_EVALID;
3771 }
3772 free(buf);
3773
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003774 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003776 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003777 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003778 LY_CHECK_RET(ret);
3779 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003780 default:
David Sedlákb3077192019-06-19 10:55:37 +02003781 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003782 return LY_EVALID;
3783 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003784 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003786
3787cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 return ret;
3789}
3790
Michal Vaskoea5abea2018-09-18 13:10:54 +02003791/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003792 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003793 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003794 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003795 * @param[in,out] argument Value to write to.
3796 * @param[in,out] flags Flags to write to.
3797 * @param[in,out] exts Extension instances to add to.
3798 *
3799 * @return LY_ERR values.
3800 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003802parse_argument(struct lys_yang_parser_ctx *ctx, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003803{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003804 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003806 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003807 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808
3809 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003810 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003811 return LY_EVALID;
3812 }
3813
3814 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003815 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003816 INSERT_WORD_GOTO(ctx, buf, *argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003817
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003818 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003820 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003821 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003822 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003823 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003824 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003825 break;
3826 default:
David Sedlákb3077192019-06-19 10:55:37 +02003827 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 return LY_EVALID;
3829 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003830 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003831 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003832
3833cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 return ret;
3835}
3836
Michal Vaskoea5abea2018-09-18 13:10:54 +02003837/**
3838 * @brief Parse the extension statement.
3839 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003840 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003841 * @param[in,out] extensions Extensions to add to.
3842 *
3843 * @return LY_ERR values.
3844 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003846parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003848 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003849 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003850 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003851 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 struct lysp_ext *ex;
3853
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003854 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855
3856 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003857 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003858 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003859
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003860 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003862 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003863 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003865 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003866 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003867 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003868 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003869 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003871 case LY_STMT_ARGUMENT:
Radek Krejci9f87b0c2021-03-05 14:45:26 +01003872 LY_CHECK_RET(parse_argument(ctx, &ex->argname, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003873 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003874 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003875 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003876 break;
3877 default:
David Sedlákb3077192019-06-19 10:55:37 +02003878 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003879 return LY_EVALID;
3880 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003881 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003882 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003883
3884cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885 return ret;
3886}
3887
Michal Vaskoea5abea2018-09-18 13:10:54 +02003888/**
3889 * @brief Parse the deviate statement.
3890 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003891 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003892 * @param[in,out] deviates Deviates to add to.
3893 *
3894 * @return LY_ERR values.
3895 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003896LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003897parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003899 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003900 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003901 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003902 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003903 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003904 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003905 struct lysp_deviate_add *d_add = NULL;
3906 struct lysp_deviate_rpl *d_rpl = NULL;
3907 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003908 const char **d_units = NULL;
3909 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003910 struct lysp_restr **d_musts = NULL;
3911 uint16_t *d_flags = 0;
3912 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003913
3914 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003915 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003916
Radek Krejcif13b87b2020-12-01 22:02:17 +01003917 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003918 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003919 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003920 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003921 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003922 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003923 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003924 dev_mod = LYS_DEV_DELETE;
3925 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003926 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003927 ret = LY_EVALID;
3928 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930
3931 /* create structure */
3932 switch (dev_mod) {
3933 case LYS_DEV_NOT_SUPPORTED:
3934 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003935 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936 break;
3937 case LYS_DEV_ADD:
3938 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003939 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 d = (struct lysp_deviate *)d_add;
3941 d_units = &d_add->units;
3942 d_uniques = &d_add->uniques;
3943 d_dflts = &d_add->dflts;
3944 d_musts = &d_add->musts;
3945 d_flags = &d_add->flags;
3946 d_min = &d_add->min;
3947 d_max = &d_add->max;
3948 break;
3949 case LYS_DEV_REPLACE:
3950 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003951 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003952 d = (struct lysp_deviate *)d_rpl;
3953 d_units = &d_rpl->units;
3954 d_flags = &d_rpl->flags;
3955 d_min = &d_rpl->min;
3956 d_max = &d_rpl->max;
3957 break;
3958 case LYS_DEV_DELETE:
3959 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003960 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 d = (struct lysp_deviate *)d_del;
3962 d_units = &d_del->units;
3963 d_uniques = &d_del->uniques;
3964 d_dflts = &d_del->dflts;
3965 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003966 break;
3967 default:
3968 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003969 LOGINT(PARSER_CTX(ctx));
3970 ret = LY_EINT;
3971 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003972 }
3973 d->mod = dev_mod;
3974
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003975 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003977 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978 switch (dev_mod) {
3979 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003980 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003981 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003982 ret = LY_EVALID;
3983 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003985 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003986 break;
3987 }
3988 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003989 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990 switch (dev_mod) {
3991 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003992 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003993 ret = LY_EVALID;
3994 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003995 case LYS_DEV_REPLACE:
Michal Vasko12ef5362022-09-16 15:13:58 +02003996 LY_CHECK_GOTO(ret = parse_text_field(ctx, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts), cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003997 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003998 break;
3999 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004000 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 +02004001 break;
4002 }
4003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004004 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004005 switch (dev_mod) {
4006 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004007 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02004008 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004009 ret = LY_EVALID;
4010 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004012 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 break;
4014 }
4015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004016 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017 switch (dev_mod) {
4018 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004019 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02004020 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_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_maxelements(ctx, d_max, d_flags, &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_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004029 switch (dev_mod) {
4030 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004031 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02004032 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004033 ret = LY_EVALID;
4034 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004035 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004036 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004037 break;
4038 }
4039 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004040 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004041 switch (dev_mod) {
4042 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004043 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02004044 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004045 ret = LY_EVALID;
4046 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004047 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004048 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004049 break;
4050 }
4051 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004052 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 switch (dev_mod) {
4054 case LYS_DEV_NOT_SUPPORTED:
4055 case LYS_DEV_ADD:
4056 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02004057 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004058 ret = LY_EVALID;
4059 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004060 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004061 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02004062 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004063 ret = LY_EVALID;
4064 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004065 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004066 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004067 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4068 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004069 break;
4070 }
4071 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004072 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073 switch (dev_mod) {
4074 case LYS_DEV_NOT_SUPPORTED:
4075 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02004076 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004077 ret = LY_EVALID;
4078 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004079 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004080 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 +02004081 break;
4082 }
4083 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004084 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004085 switch (dev_mod) {
4086 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02004087 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004088 ret = LY_EVALID;
4089 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004090 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004091 LY_CHECK_GOTO(ret = parse_text_field(ctx, LY_STMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004092 break;
4093 }
4094 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004095 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004096 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, LY_STMT_DEVIATE, 0, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004097 break;
4098 default:
David Sedlákb3077192019-06-19 10:55:37 +02004099 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004100 ret = LY_EVALID;
4101 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004103 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004104 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004105
4106cleanup:
4107 free(buf);
4108 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004109 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004110 free(d);
4111 } else {
4112 /* insert into siblings */
4113 LY_LIST_INSERT(deviates, d, next);
4114 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 return ret;
4116}
4117
Michal Vaskoea5abea2018-09-18 13:10:54 +02004118/**
4119 * @brief Parse the deviation statement.
4120 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004121 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004122 * @param[in,out] deviations Deviations to add to.
4123 *
4124 * @return LY_ERR values.
4125 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004126LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004127parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004128{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004129 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004130 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004131 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004132 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004133 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004134 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004135
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004136 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004137
4138 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004139 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004140 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004141 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004142
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004143 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004144 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004145 case LY_STMT_DESCRIPTION:
Michal Vasko12ef5362022-09-16 15:13:58 +02004146 LY_CHECK_GOTO(ret = parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004148 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004149 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004151 case LY_STMT_REFERENCE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004152 LY_CHECK_GOTO(ret = parse_text_field(ctx, LY_STMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004154 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004155 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, LY_STMT_DEVIATION, 0, &dev->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004156 break;
4157 default:
David Sedlákb3077192019-06-19 10:55:37 +02004158 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004159 ret = LY_EVALID;
4160 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004162 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004164
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 /* mandatory substatements */
4166 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004167 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004168 ret = LY_EVALID;
4169 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 }
4171
Michal Vasko12ef5362022-09-16 15:13:58 +02004172cleanup:
4173 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004174 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004175 LY_ARRAY_DECREMENT_FREE(*deviations);
4176 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004177 return ret;
4178}
4179
Michal Vaskoea5abea2018-09-18 13:10:54 +02004180/**
4181 * @brief Parse the feature statement.
4182 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004183 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004184 * @param[in,out] features Features to add to.
4185 *
4186 * @return LY_ERR values.
4187 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004188LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004189parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004191 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004192 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004193 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004194 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004195 struct lysp_feature *feat;
4196
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004197 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004198
4199 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004200 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004201 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004202
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004203 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004204 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004205 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004206 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004208 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004209 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004211 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004212 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004214 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004215 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004217 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004218 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219 break;
4220 default:
David Sedlákb3077192019-06-19 10:55:37 +02004221 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004222 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004223 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004224 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->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/**
4232 * @brief Parse the identity statement.
4233 *
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] identities Identities to add to.
4236 *
4237 * @return LY_ERR values.
4238 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004239LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004240parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004241{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004242 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004243 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004244 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004245 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004246 struct lysp_ident *ident;
4247
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004248 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004249
4250 /* get value */
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, ident->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 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004256 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004257 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004258 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004259 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004260 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004261 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004262 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004263 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004264 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004266 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004267 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004269 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004270 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004271 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 +01004272 return LY_EVALID;
4273 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004274 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 +02004275 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004276 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004277 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004278 break;
4279 default:
David Sedlákb3077192019-06-19 10:55:37 +02004280 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004281 return LY_EVALID;
4282 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004283 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004284 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004285
4286cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004287 return ret;
4288}
4289
Michal Vaskoea5abea2018-09-18 13:10:54 +02004290/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004291 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004292 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004293 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004294 * @param[in,out] mod Module to write to.
4295 *
4296 * @return LY_ERR values.
4297 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004298LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004299parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004300{
4301 LY_ERR ret = 0;
4302 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004303 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004304 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004305 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004306 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004307
Michal Vaskoc3781c32020-10-06 14:04:08 +02004308 mod->is_submod = 0;
4309
4310 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004311 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004312 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004313
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004314 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004315
Radek Krejcie3846472018-10-15 15:24:51 +02004316#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004317 if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004318
Michal Vasko7fbc8162018-09-17 10:35:16 +02004319 switch (kw) {
4320 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004321 case LY_STMT_NAMESPACE:
4322 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004323 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004325 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004326 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004327 break;
4328 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004329 case LY_STMT_INCLUDE:
4330 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004331 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004332 break;
4333 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004334 case LY_STMT_ORGANIZATION:
4335 case LY_STMT_CONTACT:
4336 case LY_STMT_DESCRIPTION:
4337 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004338 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004339 break;
4340
4341 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004342 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004343 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004344 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004345 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004346 case LY_STMT_ANYDATA:
4347 case LY_STMT_ANYXML:
4348 case LY_STMT_AUGMENT:
4349 case LY_STMT_CHOICE:
4350 case LY_STMT_CONTAINER:
4351 case LY_STMT_DEVIATION:
4352 case LY_STMT_EXTENSION:
4353 case LY_STMT_FEATURE:
4354 case LY_STMT_GROUPING:
4355 case LY_STMT_IDENTITY:
4356 case LY_STMT_LEAF:
4357 case LY_STMT_LEAF_LIST:
4358 case LY_STMT_LIST:
4359 case LY_STMT_NOTIFICATION:
4360 case LY_STMT_RPC:
4361 case LY_STMT_TYPEDEF:
4362 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004363 mod_stmt = Y_MOD_BODY;
4364 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004365 case LY_STMT_EXTENSION_INSTANCE:
4366 /* no place in the statement order defined */
4367 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004368 default:
4369 /* error handled in the next switch */
4370 break;
4371 }
Radek Krejcie3846472018-10-15 15:24:51 +02004372#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004373
Radek Krejcie3846472018-10-15 15:24:51 +02004374 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004375 switch (kw) {
4376 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004377 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004378 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004380 case LY_STMT_NAMESPACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004381 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004383 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01004384 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004385 break;
4386
4387 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004389 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004392 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004393 break;
4394
4395 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004396 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004397 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004399 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004400 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004402 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004403 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004405 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004406 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004407 break;
4408
4409 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004410 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004411 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004412 break;
4413
4414 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004415 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004416 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004417 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004418 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004419 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004421 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004422 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004423 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004424 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004425 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004426 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004427 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004428 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004429 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004430 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004431 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004433 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004434 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004436 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004437 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004438 break;
4439
Radek Krejcid6b76452019-09-03 17:03:03 +02004440 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004441 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004443 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004444 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004446 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004447 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004449 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004450 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004452 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004453 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004455 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004456 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004458 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004459 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004461 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004462 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004464 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004465 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004467 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004468 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004469 break;
4470
4471 default:
David Sedlákb3077192019-06-19 10:55:37 +02004472 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004473 return LY_EVALID;
4474 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004475 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004476 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004477
Michal Vasko7fbc8162018-09-17 10:35:16 +02004478 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004479 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004480 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004481 return LY_EVALID;
4482 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004483 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004484 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004485 }
4486
Radek Krejcie9e987e2018-10-31 12:50:27 +01004487 /* submodules share the namespace with the module names, so there must not be
4488 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004489 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004490 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004491 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004492 return LY_EVALID;
4493 }
4494
Michal Vasko12ef5362022-09-16 15:13:58 +02004495cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004496 return ret;
4497}
4498
4499/**
4500 * @brief Parse submodule substatements.
4501 *
4502 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004503 * @param[out] submod Parsed submodule structure.
4504 *
4505 * @return LY_ERR values.
4506 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004507LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004508parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004509{
4510 LY_ERR ret = 0;
4511 char *buf, *word;
4512 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004513 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004514 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004515 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004516
Michal Vaskoc3781c32020-10-06 14:04:08 +02004517 submod->is_submod = 1;
4518
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004519 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004520 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004521 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004522
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004523 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004524
4525#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004526 if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004527
4528 switch (kw) {
4529 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004530 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004531 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4532 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004533 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004534 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4535 break;
4536 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004537 case LY_STMT_INCLUDE:
4538 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004539 CHECK_ORDER(Y_MOD_LINKAGE);
4540 break;
4541 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004542 case LY_STMT_ORGANIZATION:
4543 case LY_STMT_CONTACT:
4544 case LY_STMT_DESCRIPTION:
4545 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004546 CHECK_ORDER(Y_MOD_META);
4547 break;
4548
4549 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004550 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004551 CHECK_ORDER(Y_MOD_REVISION);
4552 break;
4553 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004554 case LY_STMT_ANYDATA:
4555 case LY_STMT_ANYXML:
4556 case LY_STMT_AUGMENT:
4557 case LY_STMT_CHOICE:
4558 case LY_STMT_CONTAINER:
4559 case LY_STMT_DEVIATION:
4560 case LY_STMT_EXTENSION:
4561 case LY_STMT_FEATURE:
4562 case LY_STMT_GROUPING:
4563 case LY_STMT_IDENTITY:
4564 case LY_STMT_LEAF:
4565 case LY_STMT_LEAF_LIST:
4566 case LY_STMT_LIST:
4567 case LY_STMT_NOTIFICATION:
4568 case LY_STMT_RPC:
4569 case LY_STMT_TYPEDEF:
4570 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004571 mod_stmt = Y_MOD_BODY;
4572 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004573 case LY_STMT_EXTENSION_INSTANCE:
4574 /* no place in the statement order defined */
4575 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004576 default:
4577 /* error handled in the next switch */
4578 break;
4579 }
4580#undef CHECK_ORDER
4581
4582 prev_kw = kw;
4583 switch (kw) {
4584 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004585 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004586 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004587 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004588 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004589 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004590 break;
4591
4592 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004593 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004594 if (submod->version == LYS_VERSION_1_1) {
4595 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4596 submod->name);
4597 }
Radek Krejci33090f92020-12-17 20:12:46 +01004598 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004599 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004600 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004601 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004602 break;
4603
4604 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004605 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004606 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004607 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004608 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004609 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004610 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004611 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004612 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004614 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004615 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004616 break;
4617
4618 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004619 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004620 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004621 break;
4622
4623 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004624 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004625 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004626 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004627 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004628 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004630 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004631 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004632 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004633 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004634 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004635 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004636 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004637 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004639 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004640 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004642 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004643 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004644 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004645 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004646 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004647 break;
4648
Radek Krejcid6b76452019-09-03 17:03:03 +02004649 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004650 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004651 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004652 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004653 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004654 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004655 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004656 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004657 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004658 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004659 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004660 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004661 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004662 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004663 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004664 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004665 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004666 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004667 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004668 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004669 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004670 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004671 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004672 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004673 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004674 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004676 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004677 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004678 break;
4679
4680 default:
David Sedlákb3077192019-06-19 10:55:37 +02004681 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004682 return LY_EVALID;
4683 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004684 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004685 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004686
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004687 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004688 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004689 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004690 return LY_EVALID;
4691 }
4692
4693 /* submodules share the namespace with the module names, so there must not be
4694 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004695 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004696 /* main modules may have different revisions */
4697 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004698 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004699 return LY_EVALID;
4700 }
4701
Michal Vasko12ef5362022-09-16 15:13:58 +02004702cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004703 return ret;
4704}
4705
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004706/**
4707 * @brief Skip any redundant characters, namely whitespaces and comments.
4708 *
4709 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004710 * @return LY_SUCCESS on success.
4711 * @return LY_EVALID on invalid comment.
4712 */
4713static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004714skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004715{
4716 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004717 while (ctx->in->current[0]) {
4718 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004719 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004720 ly_in_skip(ctx->in, 2);
4721 LY_CHECK_RET(skip_comment(ctx, 1));
4722 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004723 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004724 ly_in_skip(ctx->in, 2);
4725 LY_CHECK_RET(skip_comment(ctx, 2));
4726 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004727 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004728 if (ctx->in->current[0] == '\n') {
4729 LY_IN_NEW_LINE(ctx->in);
4730 }
Radek Krejci33090f92020-12-17 20:12:46 +01004731 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004732 } else {
4733 break;
4734 }
4735 }
4736
4737 return LY_SUCCESS;
4738}
4739
Radek Krejcid4557c62018-09-17 11:42:09 +02004740LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004741yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004742 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004743{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004744 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004745 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004746 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004747 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004748 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004749 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004750
aPiecek56be92a2021-07-01 07:18:10 +02004751 assert(context && ly_ctx && main_ctx && in && submod);
4752
David Sedlák1b623122019-08-05 15:27:49 +02004753 /* create context */
4754 *context = calloc(1, sizeof **context);
4755 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004756 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004757 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004758 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004759
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004760 mod_p = calloc(1, sizeof *mod_p);
4761 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004762 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004763 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004764
4765 /* use main context parsed mods adding the current one */
4766 (*context)->parsed_mods = main_ctx->parsed_mods;
4767 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004768
Radek Krejciddace2c2021-01-08 11:30:56 +01004769 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004770
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004771 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004772 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004773 LY_CHECK_GOTO(ret, cleanup);
4774
Michal Vasko7fbc8162018-09-17 10:35:16 +02004775 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004776 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004777 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004778
Radek Krejcid6b76452019-09-03 17:03:03 +02004779 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004780 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004781 ret = LY_EINVAL;
4782 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004783 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004784 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004785 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004786 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004787 }
4788
Michal Vasko7fbc8162018-09-17 10:35:16 +02004789 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004790 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004791 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004792
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004793 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004794 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004795 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004796 if (in->current[0]) {
4797 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004798 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004799 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004800 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004801
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004802 mod_p->parsing = 0;
4803 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004804
Radek Krejcibbe09a92018-11-08 09:36:54 +01004805cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004806 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004807 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004808 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004809 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004810 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004811 }
4812
4813 return ret;
4814}
4815
4816LY_ERR
aPiecekc3e26142021-06-22 14:25:49 +02004817yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004818{
4819 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004820 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004821 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004822 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004823 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004824 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004825
David Sedlák1b623122019-08-05 15:27:49 +02004826 /* create context */
4827 *context = calloc(1, sizeof **context);
4828 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004829 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004830 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004831 (*context)->in = in;
aPiecek8d4e75d2021-06-24 14:47:06 +02004832 (*context)->main_ctx = (struct lys_parser_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004833
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004834 mod_p = calloc(1, sizeof *mod_p);
4835 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4836 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004837 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004838
Radek Krejciddace2c2021-01-08 11:30:56 +01004839 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004840
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004841 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004842 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004843 LY_CHECK_GOTO(ret, cleanup);
4844
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004845 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004846 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004847 LY_CHECK_GOTO(ret, cleanup);
4848
Radek Krejcid6b76452019-09-03 17:03:03 +02004849 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004850 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 +01004851 ret = LY_EINVAL;
4852 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004853 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004854 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004855 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004856 goto cleanup;
4857 }
4858
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004859 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004860 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004861 LY_CHECK_GOTO(ret, cleanup);
4862
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004863 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004864 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004865 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004866 if (in->current[0]) {
4867 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004868 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004869 goto cleanup;
4870 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004871
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004872 mod->parsed = mod_p;
4873
4874cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004875 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004876 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004877 lysp_module_free(&fctx, mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004878 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004879 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004880 }
4881
Michal Vasko7fbc8162018-09-17 10:35:16 +02004882 return ret;
4883}