blob: 03131da343459f7db314a05130eb0344b51c13f8 [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
Michal Vaskoc636ea42022-09-16 10:20:31 +02006 * Copyright (c) 2018 - 2022 CESNET, z.s.p.o.
Michal Vasko7fbc8162018-09-17 10:35:16 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko63f3d842020-07-08 10:10:14 +020014#include "parser_internal.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
17#include <ctype.h>
18#include <errno.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "log.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020030#include "path.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "set.h"
32#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010033#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "tree_schema.h"
Michal Vaskoc636ea42022-09-16 10:20:31 +020035#include "tree_schema_free.h"
Radek Krejci70853c52018-10-15 14:46:16 +020036#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020037
Radek Krejci77114102021-03-10 15:21:57 +010038struct lys_glob_unres;
39
Radek Krejciceaf2122019-01-02 15:03:26 +010040/**
41 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020042 *
Radek Krejciceaf2122019-01-02 15:03:26 +010043 * @param[in] CTX yang parser context to access libyang context.
44 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
45 * @param[out] TARGET variable where to store the pointer to the inserted value.
46 * @param[in] WORD string to store.
47 * @param[in] LEN length of the string in WORD to store.
48 */
Michal Vasko12ef5362022-09-16 15:13:58 +020049#define INSERT_WORD_GOTO(CTX, BUF, TARGET, WORD, LEN, RET, LABEL) \
50 if (BUF) {LY_CHECK_GOTO(RET = lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)), LABEL);}\
51 else {LY_CHECK_GOTO(RET = lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)), LABEL);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020052
Radek Krejciceaf2122019-01-02 15:03:26 +010053/**
Michal Vasko63f3d842020-07-08 10:10:14 +020054 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020055 *
Radek Krejciceaf2122019-01-02 15:03:26 +010056 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010057 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
58 */
Radek Krejcid54412f2020-12-17 20:25:35 +010059#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020060
Michal Vaskoea5abea2018-09-18 13:10:54 +020061/**
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020062 * @brief Loop through all substatements. Starts a for loop and ::YANG_READ_SUBSTMT_NEXT_ITER must be used at its end.
Michal Vaskoea5abea2018-09-18 13:10:54 +020063 *
Michal Vaskoc8806c82022-12-06 10:31:24 +010064 * @param[in] CTX yang parser context.
Michal Vaskoea5abea2018-09-18 13:10:54 +020065 * @param[out] KW YANG keyword read.
66 * @param[out] WORD Pointer to the keyword itself.
67 * @param[out] WORD_LEN Length of the keyword.
Michal Vasko12ef5362022-09-16 15:13:58 +020068 * @param[out] RET Variable for error storing.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020069 * @param[in] ERR_LABEL Label to go to on error.
Michal Vaskoea5abea2018-09-18 13:10:54 +020070 */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020071#define YANG_READ_SUBSTMT_FOR_GOTO(CTX, KW, WORD, WORD_LEN, RET, ERR_LABEL) \
72 ly_bool __loop_end = 0; \
Michal Vasko12ef5362022-09-16 15:13:58 +020073 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020074 goto ERR_LABEL; \
aPieceka24a2252021-05-07 10:52:31 +020075 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020076 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020077 __loop_end = 1; \
78 } else if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
Michal Vasko193dacd2022-10-13 08:43:05 +020079 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", lyplg_ext_stmt2str(KW)); \
Michal Vasko12ef5362022-09-16 15:13:58 +020080 RET = LY_EVALID; \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020081 goto ERR_LABEL; \
82 } else { \
83 YANG_READ_SUBSTMT_NEXT_ITER(CTX, KW, WORD, WORD_LEN, NULL, RET, ERR_LABEL); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020084 } \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020085 while (!__loop_end)
Michal Vasko7fbc8162018-09-17 10:35:16 +020086
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020087/**
88 * @brief Next iteration of ::YANG_READ_SUBSTMT_FOR_GOTO loop.
89 *
Michal Vaskoc8806c82022-12-06 10:31:24 +010090 * @param[in] CTX yang parser context.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020091 * @param[out] KW YANG keyword read.
92 * @param[out] WORD Pointer to the keyword itself.
93 * @param[out] WORD_LEN Length of the keyword.
94 * @param[in] EXTS Final extension instance array to store.
95 * @param[out] RET Variable for error storing.
96 * @param[in] ERR_LABEL Label to go to on error.
97 */
98#define YANG_READ_SUBSTMT_NEXT_ITER(CTX, KW, WORD, WORD_LEN, EXTS, RET, ERR_LABEL) \
99 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
100 goto ERR_LABEL; \
101 } \
102 if (KW == LY_STMT_SYNTAX_RIGHT_BRACE) { \
Michal Vaskoc8806c82022-12-06 10:31:24 +0100103 if (EXTS && (RET = ly_set_add(&(CTX)->main_ctx->ext_inst, (EXTS), 1, NULL))) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200104 goto ERR_LABEL; \
105 } \
106 __loop_end = 1; \
107 }
108
Michal Vaskod0625d72022-10-06 15:02:50 +0200109LY_ERR parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
110LY_ERR parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
111LY_ERR parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
112LY_ERR parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
113LY_ERR parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
114LY_ERR parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200115
Michal Vaskoea5abea2018-09-18 13:10:54 +0200116/**
117 * @brief Add another character to dynamic buffer, a low-level function.
118 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200119 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200120 *
Radek Krejci404251e2018-10-09 12:06:44 +0200121 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200122 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200123 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200124 * @param[in,out] buf Buffer to use, can be moved by realloc().
125 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200127 * @return LY_ERR values.
128 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200129LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200130buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200131{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100132#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100134 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200135 *buf = ly_realloc(*buf, *buf_len);
136 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
137 }
Radek Krejcic0917392019-04-10 13:04:04 +0200138 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200139 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200140 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200141 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200142 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143
Radek Krejci44ceedc2018-10-02 15:54:31 +0200144 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200145 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100146#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200147}
148
Michal Vaskoea5abea2018-09-18 13:10:54 +0200149/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200150 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
151 *
152 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200153 * @param[in] arg Type of the input string to select method of checking character validity.
154 * @param[in,out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first
Michal Vaskoea5abea2018-09-18 13:10:54 +0200155 * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200156 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
157 * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not requested (word is a substring of the data).
Michal Vaskoea5abea2018-09-18 13:10:54 +0200158 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200159 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200160 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
161 * 0 - colon not yet found (no prefix)
162 * 1 - \p c is the colon character
163 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200164 * @return LY_ERR values.
165 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200166LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200167buf_store_char(struct lysp_yang_ctx *ctx, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200168 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200169{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200170 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 size_t len;
172
Radek Krejcif29b7c32019-04-09 16:17:49 +0200173 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
174 assert(!need_buf || (need_buf && word_b));
175
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100177 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
178 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
179 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200180 if (c == '\n') {
181 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100182 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200183 } else {
184 /* note - even the multibyte character is count as 1 */
185 ++ctx->indent;
186 }
187
Radek Krejci44ceedc2018-10-02 15:54:31 +0200188 /* check character validity */
189 switch (arg) {
190 case Y_IDENTIF_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200191 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200192 break;
193 case Y_PREF_IDENTIF_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200194 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200195 break;
196 case Y_STR_ARG:
197 case Y_MAYBE_STR_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200198 LY_CHECK_RET(lysp_check_stringchar((struct lysp_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200199 break;
200 }
201
Michal Vasko7fbc8162018-09-17 10:35:16 +0200202 if (word_b && *word_b) {
203 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100204 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200205 return LY_EMEM;
206 }
207
208 /* in case of realloc */
209 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200210 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200211 /* first time we need a buffer, copy everything read up to now */
212 if (*word_len) {
213 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200214 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200215 *buf_len = *word_len;
216 memcpy(*word_b, *word_p, *word_len);
217 }
218
219 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100220 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200221 return LY_EMEM;
222 }
223
224 /* in case of realloc */
225 *word_p = *word_b;
226 } else {
227 /* just remember the first character pointer */
228 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100229 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200230 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200231 /* ... and update the word's length */
232 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100233 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200234 }
235
236 return LY_SUCCESS;
237}
238
Michal Vaskoea5abea2018-09-18 13:10:54 +0200239/**
240 * @brief Skip YANG comment in data.
241 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200242 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200243 * @param[in] comment Type of the comment to process:
244 * 1 for a one-line comment,
245 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200246 * @return LY_ERR values.
247 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200248LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200249skip_comment(struct lysp_yang_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200250{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100251 /* internal statuses: */
252#define COMMENT_NO 0 /* comment ended */
253#define COMMENT_LINE 1 /* in line comment */
254#define COMMENT_BLOCK 2 /* in block comment */
255#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
256
Radek Krejci33090f92020-12-17 20:12:46 +0100257 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200258 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100259 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100260 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100261 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100262 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200263 }
264 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100265 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100266 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100267 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100268 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100269 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200270 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200271 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100272 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100273 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100274 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100275 } else if (ctx->in->current[0] != '*') {
276 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100277 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200278 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100279 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200280 }
281 break;
282 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200283 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200284 }
285
Radek Krejci33090f92020-12-17 20:12:46 +0100286 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200287 ctx->indent = 0;
288 } else {
289 ++ctx->indent;
290 }
Radek Krejci33090f92020-12-17 20:12:46 +0100291 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200292 }
293
Radek Krejci33090f92020-12-17 20:12:46 +0100294 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200295 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200296 return LY_EVALID;
297 }
298
299 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100300
301#undef COMMENT_NO
302#undef COMMENT_LINE
303#undef COMMENT_BLOCK
304#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200305}
306
Michal Vaskoea5abea2018-09-18 13:10:54 +0200307/**
308 * @brief Read a quoted string from data.
309 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200310 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200311 * @param[in] arg Type of YANG keyword argument expected.
312 * @param[out] word_p Pointer to the read quoted string.
313 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
314 * set to NULL. Otherwise equal to \p word_p.
315 * @param[out] word_len Length of the read quoted string.
316 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
317 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
318 * indenation in the final quoted string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200319 * @return LY_ERR values.
320 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200322read_qstring(struct lysp_yang_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200323 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200324{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100325 /* string parsing status: */
326#define STRING_ENDED 0 /* string ended */
327#define STRING_SINGLE_QUOTED 1 /* string with ' */
328#define STRING_DOUBLE_QUOTED 2 /* string with " */
329#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
330#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
331#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
332
Radek Krejci1deb5be2020-08-26 16:43:36 +0200333 uint8_t string;
334 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200335 ly_bool need_buf = 0;
336 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200337 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200338 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200339
Radek Krejci33090f92020-12-17 20:12:46 +0100340 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100341 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200342 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200343 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100344 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100345 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200346 }
Radek Krejci33090f92020-12-17 20:12:46 +0100347 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200348
Radek Krejci33090f92020-12-17 20:12:46 +0100349 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200350 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100351 case STRING_SINGLE_QUOTED:
Michal Vasko4e55f5a2022-12-14 12:15:00 +0100352 if (ctx->in->current[0] == '\'') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200353 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100354 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100355 MOVE_INPUT(ctx, 1);
Michal Vasko4e55f5a2022-12-14 12:15:00 +0100356 } else {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200357 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100358 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200359 }
360 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100361 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100362 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200363 case '\"':
364 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100365 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100366 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200367 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200368 break;
369 case '\\':
370 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100371 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200372
373 /* the backslash sequence is substituted, so we will need a buffer to store the result */
374 need_buf = 1;
375
376 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100377 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200378
379 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
380 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
381 trailing_ws = 0;
382
383 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
384 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200385 break;
386 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200387 if (current_indent < block_indent) {
388 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100389 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200390 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100391 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100392 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100393 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200394 }
395 break;
396 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200397 if (current_indent < block_indent) {
398 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100399 current_indent += Y_TAB_SPACES;
400 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200401 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200402 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100403 c = ctx->in->current;
404 ctx->in->current = " ";
405 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
406 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100407 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200408 }
Radek Krejci33090f92020-12-17 20:12:46 +0100409 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200410 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100411 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100412 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100413 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200414 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100415 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200416 }
417 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200418 case '\r':
Michal Vasko438f3782023-08-09 10:01:15 +0200419 /* newline may be escaped */
420 if ((ctx->in->current[1] != '\n') && strncmp(&ctx->in->current[1], "\\n", 2)) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200421 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
422 return LY_EVALID;
423 }
424 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200425 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200426 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200427 /* we will be removing the indents so we need our own buffer */
428 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200429
430 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200431 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200432
Radek Krejciff13cd12019-10-25 15:34:24 +0200433 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200434 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200435 }
436
437 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100438 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200439
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200440 /* reset context indentation counter for possible string after this one */
441 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200442 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200443 break;
444 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200445 /* first non-whitespace character, stop eating indentation */
446 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200447
448 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100449 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200450 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200451 break;
452 }
453 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100454 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200455 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100456 c = ctx->in->current;
457 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200458 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100459 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100460 /* fix false newline count in buf_store_char() */
461 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200462 break;
463 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100464 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200465 break;
466 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200468 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200469 break;
470 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200471 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100472 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200473 return LY_EVALID;
474 }
475
476 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100477 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200478
Radek Krejcif13b87b2020-12-01 22:02:17 +0100479 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100480 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200481 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100482 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100483 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200484 case '+':
485 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100486 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200487 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200488 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200489 case '\r':
490 if (ctx->in->current[1] != '\n') {
491 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
492 return LY_EVALID;
493 }
494 MOVE_INPUT(ctx, 1);
495 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200496 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100497 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100498 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200499 case ' ':
500 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200501 /* just skip */
502 break;
503 default:
504 /* string is finished */
505 goto string_end;
506 }
Radek Krejci33090f92020-12-17 20:12:46 +0100507 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200508 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100509 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100510 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200511 case '\r':
512 if (ctx->in->current[1] != '\n') {
513 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
514 return LY_EVALID;
515 }
516 MOVE_INPUT(ctx, 1);
517 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200518 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100519 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100520 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200521 case ' ':
522 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200523 /* skip */
524 break;
525 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100526 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200527 break;
528 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100529 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200530 break;
531 default:
532 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200533 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200534 return LY_EVALID;
535 }
Radek Krejci33090f92020-12-17 20:12:46 +0100536 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537 break;
538 default:
539 return LY_EINT;
540 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200541 }
542
543string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200544 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200545 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200546 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200547 return LY_EVALID;
548 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200549 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100550
551#undef STRING_ENDED
552#undef STRING_SINGLE_QUOTED
553#undef STRING_DOUBLE_QUOTED
554#undef STRING_DOUBLE_QUOTED_ESCAPED
555#undef STRING_PAUSED_NEXTSTRING
556#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200557}
558
Michal Vaskoea5abea2018-09-18 13:10:54 +0200559/**
560 * @brief Get another YANG string from the raw data.
561 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200562 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200563 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200564 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
565 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200566 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200567 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
568 * set to NULL. Otherwise equal to \p word_p.
569 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200570 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200571 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200572LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200573get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200574 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200575{
Michal Vaskob68ea142021-04-26 13:46:00 +0200576 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200577 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200578 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200579
Michal Vasko7fbc8162018-09-17 10:35:16 +0200580 /* word buffer - dynamically allocated */
581 *word_b = NULL;
582
583 /* word pointer - just a pointer to data */
584 *word_p = NULL;
585
586 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100587 while (ctx->in->current[0]) {
588 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200589 case '\'':
590 case '\"':
591 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200592 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100593 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200594 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200595 ret = LY_EVALID;
596 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200597 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200598 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100599 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200600 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200601 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200602 if (!*word_p) {
603 /* do not return NULL word */
604 *word_p = "";
605 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200606 goto str_end;
607 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100608 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200609 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100610 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200611 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100612 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200613 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100614 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200615 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200616 } else {
617 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200618 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200619 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200620 break;
621 case ' ':
622 if (*word_len) {
623 /* word is finished */
624 goto str_end;
625 }
Radek Krejci33090f92020-12-17 20:12:46 +0100626 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200627 break;
628 case '\t':
629 if (*word_len) {
630 /* word is finished */
631 goto str_end;
632 }
633 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100634 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200635
Radek Krejci33090f92020-12-17 20:12:46 +0100636 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200637 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200638 case '\r':
639 if (ctx->in->current[1] != '\n') {
640 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200641 ret = LY_EVALID;
642 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200643 }
644 MOVE_INPUT(ctx, 1);
645 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200646 case '\n':
647 if (*word_len) {
648 /* word is finished */
649 goto str_end;
650 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100651 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100652 MOVE_INPUT(ctx, 1);
653
Michal Vasko7fbc8162018-09-17 10:35:16 +0200654 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200655 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200656 break;
657 case ';':
658 case '{':
659 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
660 /* word is finished */
661 goto str_end;
662 }
663
Radek Krejci33090f92020-12-17 20:12:46 +0100664 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200665 ret = LY_EVALID;
666 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200667 case '}':
668 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100669 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200670 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200671 ret = LY_EVALID;
672 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200673 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200674 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200675 break;
676 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200677 }
678
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200679 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200680 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200681 ret = LY_EVALID;
682 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200683
Michal Vasko7fbc8162018-09-17 10:35:16 +0200684str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200685 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200687 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200688 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200689 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200690 *word_p = *word_b;
691 }
692
693 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200694
695error:
696 free(*word_b);
697 *word_b = NULL;
698 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699}
700
Michal Vaskoea5abea2018-09-18 13:10:54 +0200701/**
702 * @brief Get another YANG keyword from the raw data.
703 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200704 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200705 * @param[out] kw YANG keyword read.
706 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
707 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200708 * @return LY_ERR values.
709 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200710LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200711get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200712{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200713 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200714 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200715 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200716
717 if (word_p) {
718 *word_p = NULL;
719 *word_len = 0;
720 }
721
722 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100723 while (ctx->in->current[0]) {
724 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200725 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100726 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200727 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100728 MOVE_INPUT(ctx, 2);
729 LY_CHECK_RET(skip_comment(ctx, 1));
730 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200731 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100732 MOVE_INPUT(ctx, 2);
733 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200734 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200735 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200736 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200737 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200738 }
Radek Krejci13028282019-06-11 14:56:48 +0200739 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200740 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200741 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100742 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200743 ctx->indent = 0;
744 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200745 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200746 /* skip whitespaces (optsep) */
747 ++ctx->indent;
748 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200749 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200750 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100751 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100753 case '\r':
754 /* possible CRLF endline */
755 if (ctx->in->current[1] == '\n') {
756 break;
757 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100758 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 default:
760 /* either a keyword start or an invalid character */
761 goto keyword_start;
762 }
763
Radek Krejci33090f92020-12-17 20:12:46 +0100764 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200765 }
766
767keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100768 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100769 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200770
aPiecek93582ed2021-05-25 14:49:06 +0200771 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
772 goto success;
773 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
774 ctx->depth++;
775 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100776 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200777 return LY_EINVAL;
778 }
779 goto success;
780 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
781 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200782 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200783 }
784
Radek Krejcid6b76452019-09-03 17:03:03 +0200785 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200786 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100787 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200788 case '\r':
789 if (ctx->in->current[1] != '\n') {
790 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
791 return LY_EVALID;
792 }
793 MOVE_INPUT(ctx, 1);
794 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200795 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200796 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200797 case ' ':
798 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200799 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200800 case ':':
801 /* keyword is not actually a keyword, but prefix of an extension.
802 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
803 * and we will be checking the keyword (extension instance) itself */
804 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100805 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200806 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200807 case '{':
Michal Vasko946b70c2023-07-19 08:57:31 +0200808 case ';':
809 /* allowed only for input and output statements which are without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200810 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200811 break;
812 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100813 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200814 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100815 MOVE_INPUT(ctx, 1);
816 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200817 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818 return LY_EVALID;
819 }
820 } else {
821 /* still can be an extension */
822 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200823
Radek Krejci156ccaf2018-10-15 15:49:17 +0200824extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100825 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200826 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
827 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200828 uint32_t c = 0;
829
Radek Krejci33090f92020-12-17 20:12:46 +0100830 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
831 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200832 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200833 /* check character validity */
Michal Vaskod0625d72022-10-06 15:02:50 +0200834 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100835 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200836 }
Radek Krejci33090f92020-12-17 20:12:46 +0100837 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200838 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200839 return LY_EVALID;
840 }
841
842 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200843 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100844 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845 return LY_EVALID;
846 }
847
Radek Krejcid6b76452019-09-03 17:03:03 +0200848 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200849 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200850
Radek Krejci626df482018-10-11 15:06:31 +0200851success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200852 if (word_p) {
853 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100854 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200855 }
856
857 return LY_SUCCESS;
858}
859
Michal Vaskoea5abea2018-09-18 13:10:54 +0200860/**
861 * @brief Parse extension instance substatements.
862 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200863 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200864 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200865 * @param[in] word Extension instance substatement name (keyword).
866 * @param[in] word_len Extension instance substatement name length.
867 * @param[in,out] child Children of this extension instance to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200868 * @return LY_ERR values.
869 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200871parse_ext_substmt(struct lysp_yang_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200872 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200873{
874 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100875 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200876 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200877 struct lysp_stmt *stmt, *par_child;
878
879 stmt = calloc(1, sizeof *stmt);
880 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
881
Radek Krejcibb9b1982019-04-08 14:24:59 +0200882 /* insert into parent statements */
883 if (!*child) {
884 *child = stmt;
885 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200886 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200887 par_child->next = stmt;
888 }
889
Michal Vaskofc2cd072021-02-24 13:17:17 +0100890 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200891 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892
893 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100894 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200895 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200896 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200897 }
898
Radek Krejci8df109d2021-04-23 12:19:08 +0200899 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100900 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100901 stmt->kw = kw;
902
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200903 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
904 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
905 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200906 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200907
908cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200909 return ret;
910}
911
Michal Vaskoea5abea2018-09-18 13:10:54 +0200912/**
913 * @brief Parse extension instance.
914 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200915 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200916 * @param[in] ext_name Extension instance substatement name (keyword).
917 * @param[in] ext_name_len Extension instance substatement name length.
Michal Vasko193dacd2022-10-13 08:43:05 +0200918 * @param[in] parent Current statement parent.
919 * @param[in] parent_stmt Type of @p parent statement.
920 * @param[in] parent_stmt_index In case of several @p parent_stmt, index of the relevant @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200921 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200922 * @return LY_ERR values.
923 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200924static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200925parse_ext(struct lysp_yang_ctx *ctx, const char *ext_name, size_t ext_name_len, const void *parent,
926 enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100928 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200929 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200930 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200932 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200934 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200935
Michal Vaskofc2cd072021-02-24 13:17:17 +0100936 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
937 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
938 return LY_EVALID;
939 }
940
941 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200942 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200943
944 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100945 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200946 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200947 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200948 }
949
Michal Vaskofc2cd072021-02-24 13:17:17 +0100950 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200951 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200952 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100953 e->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200954 e->parent = (void *)parent;
955 e->parent_stmt = parent_stmt;
956 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100957
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200958 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
959 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
960 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200961 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200962
963cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964 return ret;
965}
966
Michal Vaskoea5abea2018-09-18 13:10:54 +0200967/**
968 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
969 * description, etc...
970 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200971 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +0200972 * @param[in] parent Current statement parent.
973 * @param[in] parent_stmt Type of statement in @p value.
974 * @param[in] parent_stmt_index In case of several @p parent_stmt, index of the relevant @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200975 * @param[in,out] value Place to store the parsed value.
976 * @param[in] arg Type of the YANG keyword argument (of the value).
977 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200978 * @return LY_ERR values.
979 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200980static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200981parse_text_field(struct lysp_yang_ctx *ctx, const void *parent, enum ly_stmt parent_stmt, uint32_t parent_stmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200982 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200983{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100984 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200985 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200986 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200987 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988
989 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200990 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200991 return LY_EVALID;
992 }
993
994 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100995 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996
997 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +0200998 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200999
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001000 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001002 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001003 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent, parent_stmt, parent_stmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001004 break;
1005 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001006 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007 return LY_EVALID;
1008 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001009 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001010 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001011
1012cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001013 return ret;
1014}
1015
Michal Vaskoea5abea2018-09-18 13:10:54 +02001016/**
1017 * @brief Parse the yang-version statement.
1018 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001019 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001020 * @param[in,out] mod Module to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001021 * @return LY_ERR values.
1022 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001024parse_yangversion(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001026 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001027 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001028 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001029 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001030
Michal Vasko193dacd2022-10-13 08:43:05 +02001031 if (mod->version) {
David Sedlákb3077192019-06-19 10:55:37 +02001032 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001033 return LY_EVALID;
1034 }
1035
1036 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001037 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001038
Radek Krejci96e48da2020-09-04 13:18:06 +02001039 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001040 mod->version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001041 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001042 mod->version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001043 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001044 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001045 free(buf);
1046 return LY_EVALID;
1047 }
1048 free(buf);
1049
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001050 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001052 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001053 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_YANG_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001054 break;
1055 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001056 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001057 return LY_EVALID;
1058 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001059 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001060 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001061
1062cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001063 return ret;
1064}
1065
Michal Vaskoea5abea2018-09-18 13:10:54 +02001066/**
1067 * @brief Parse the belongs-to statement.
1068 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001069 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001070 * @param[in,out] submod Submodule to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001071 * @return LY_ERR values.
1072 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001074parse_belongsto(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001076 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001077 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001078 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001079 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001080
Michal Vasko193dacd2022-10-13 08:43:05 +02001081 if (submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001082 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083 return LY_EVALID;
1084 }
1085
Michal Vaskoc3781c32020-10-06 14:04:08 +02001086 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001087 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001088 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001089 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 +01001090 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001091 free(buf);
1092 return LY_EVALID;
1093 }
1094 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001095
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001096 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001098 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001099 LY_CHECK_RET(parse_text_field(ctx, submod->prefix, LY_STMT_PREFIX, 0, &submod->prefix, Y_IDENTIF_ARG, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001100 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001101 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001102 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_BELONGS_TO, 0, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001103 break;
1104 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001105 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001106 return LY_EVALID;
1107 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001108 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001109 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001110
Michal Vasko7fbc8162018-09-17 10:35:16 +02001111 /* mandatory substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +02001112 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001113 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001114 return LY_EVALID;
1115 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001116
1117cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118 return ret;
1119}
1120
Michal Vaskoea5abea2018-09-18 13:10:54 +02001121/**
1122 * @brief Parse the revision-date statement.
1123 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001124 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001125 * @param[in,out] rev Buffer to store the parsed value in.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001126 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001127 * @return LY_ERR values.
1128 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001129static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001130parse_revisiondate(struct lysp_yang_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001131{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001132 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001133 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001134 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001135 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136
1137 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001138 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139 return LY_EVALID;
1140 }
1141
1142 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001143 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144
1145 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001146 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147 free(buf);
1148 return LY_EVALID;
1149 }
1150
1151 /* store value and spend buf if allocated */
1152 strncpy(rev, word, word_len);
1153 free(buf);
1154
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001155 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001156 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001157 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001158 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001159 break;
1160 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001161 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001162 return LY_EVALID;
1163 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001164 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001165 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001166
1167cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001168 return ret;
1169}
1170
Michal Vaskoea5abea2018-09-18 13:10:54 +02001171/**
1172 * @brief Parse the include statement.
1173 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001174 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001175 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001176 * @param[in,out] includes Parsed includes to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001177 * @return LY_ERR values.
1178 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001179static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001180parse_include(struct lysp_yang_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181{
Radek Krejcid33273d2018-10-25 14:55:52 +02001182 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001184 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001185 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001186 struct lysp_include *inc;
1187
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001188 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189
1190 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001191 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192
Michal Vasko12ef5362022-09-16 15:13:58 +02001193 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001194
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001195 /* submodules share the namespace with the module names, so there must not be
1196 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001197 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001198 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001199 return LY_EVALID;
1200 }
1201
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001202 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001203 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001204 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001205 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001206 LY_CHECK_RET(parse_text_field(ctx, inc->dsc, LY_STMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001208 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001209 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001210 LY_CHECK_RET(parse_text_field(ctx, inc->ref, LY_STMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001212 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001213 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001215 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001216 LY_CHECK_RET(parse_ext(ctx, word, word_len, inc, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 break;
1218 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001219 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220 return LY_EVALID;
1221 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001222 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001223 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001224
1225cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001226 return ret;
1227}
1228
Michal Vaskoea5abea2018-09-18 13:10:54 +02001229/**
1230 * @brief Parse the import statement.
1231 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001232 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001233 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001234 * @param[in,out] imports Parsed imports to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001235 * @return LY_ERR values.
1236 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001238parse_import(struct lysp_yang_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001240 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001242 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001243 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001244 struct lysp_import *imp;
1245
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001246 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001247
1248 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001249 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001250 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001252 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001253 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001254 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001255 LY_CHECK_RET(parse_text_field(ctx, imp->prefix, LY_STMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskod0625d72022-10-06 15:02:50 +02001256 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001258 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001259 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001260 LY_CHECK_RET(parse_text_field(ctx, imp->dsc, LY_STMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001262 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001263 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001264 LY_CHECK_RET(parse_text_field(ctx, imp->ref, LY_STMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001266 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001267 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001269 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001270 LY_CHECK_RET(parse_ext(ctx, word, word_len, imp, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001271 break;
1272 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001273 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001274 return LY_EVALID;
1275 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001276 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001277 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001278
Michal Vasko7fbc8162018-09-17 10:35:16 +02001279 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001280 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001281
Michal Vasko12ef5362022-09-16 15:13:58 +02001282cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001283 return ret;
1284}
1285
Michal Vaskoea5abea2018-09-18 13:10:54 +02001286/**
1287 * @brief Parse the revision statement.
1288 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001289 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001290 * @param[in,out] revs Parsed revisions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001291 * @return LY_ERR values.
1292 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001293static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001294parse_revision(struct lysp_yang_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001295{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001296 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001297 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001298 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001299 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001300 struct lysp_revision *rev;
1301
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001302 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303
1304 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001305 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001306
1307 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001308 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001309 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310 return LY_EVALID;
1311 }
1312
Radek Krejcib7db73a2018-10-24 14:18:40 +02001313 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314 free(buf);
1315
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001316 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001317 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001318 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001319 LY_CHECK_RET(parse_text_field(ctx, rev->dsc, LY_STMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001321 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001322 LY_CHECK_RET(parse_text_field(ctx, rev->ref, LY_STMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001324 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001325 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001326 break;
1327 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001328 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329 return LY_EVALID;
1330 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001331 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001333
1334cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001335 return ret;
1336}
1337
Michal Vaskoea5abea2018-09-18 13:10:54 +02001338/**
1339 * @brief Parse a generic text field that can have more instances such as base.
1340 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001341 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001342 * @param[in] parent Current statement parent.
1343 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001344 * @param[in,out] texts Parsed values to add to.
1345 * @param[in] arg Type of the expected argument.
1346 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001347 * @return LY_ERR values.
1348 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001349static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001350parse_text_fields(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, const char ***texts, enum yang_arg arg,
Radek Krejci0f969882020-08-21 16:56:47 +02001351 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001352{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001353 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001354 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001355 const char **item;
1356 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001357 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001358
1359 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001360 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001361
1362 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001363 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364
Michal Vasko12ef5362022-09-16 15:13:58 +02001365 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001366 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001367 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001368 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001369 LY_CHECK_RET(parse_ext(ctx, word, word_len, *texts, parent_stmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001370 break;
1371 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001372 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001373 return LY_EVALID;
1374 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001375 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001376 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001377
1378cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001379 return ret;
1380}
1381
Michal Vaskoea5abea2018-09-18 13:10:54 +02001382/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001383 * @brief Parse a generic text field that can have more instances such as base.
1384 *
1385 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001386 * @param[in] parent_stmt Type of statement stored in @p qnames.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001387 * @param[in,out] qnames Parsed qnames to add to.
1388 * @param[in] arg Type of the expected argument.
1389 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001390 * @return LY_ERR values.
1391 */
1392static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001393parse_qnames(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_qname **qnames, enum yang_arg arg,
1394 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +02001395{
1396 LY_ERR ret = LY_SUCCESS;
1397 char *buf, *word;
1398 struct lysp_qname *item;
1399 size_t word_len;
1400 enum ly_stmt kw;
1401
1402 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001403 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001404
1405 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001406 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001407
Michal Vasko12ef5362022-09-16 15:13:58 +02001408 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001409 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001410 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001411 switch (kw) {
1412 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001413 LY_CHECK_RET(parse_ext(ctx, word, word_len, *qnames, parent_stmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001414 break;
1415 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001416 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001417 return LY_EVALID;
1418 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001419 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001420 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001421
1422cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001423 return ret;
1424}
1425
1426/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001427 * @brief Parse the config statement.
1428 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001429 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001430 * @param[in,out] flags Flags to add to.
1431 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001432 * @return LY_ERR values.
1433 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001435parse_config(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001436{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001437 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001438 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001439 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001440 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001441
1442 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001443 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001444 return LY_EVALID;
1445 }
1446
1447 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001448 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449
Radek Krejcif13b87b2020-12-01 22:02:17 +01001450 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001451 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001452 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001453 *flags |= LYS_CONFIG_R;
1454 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001455 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001456 free(buf);
1457 return LY_EVALID;
1458 }
1459 free(buf);
1460
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001461 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001462 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001463 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001464 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465 break;
1466 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001467 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468 return LY_EVALID;
1469 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001470 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001471 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001472
1473cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001474 return ret;
1475}
1476
Michal Vaskoea5abea2018-09-18 13:10:54 +02001477/**
1478 * @brief Parse the mandatory statement.
1479 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001480 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001481 * @param[in,out] flags Flags to add to.
1482 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001483 * @return LY_ERR values.
1484 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001485static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001486parse_mandatory(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001488 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001490 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001491 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001492
1493 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001494 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001495 return LY_EVALID;
1496 }
1497
1498 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001499 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500
Radek Krejcif13b87b2020-12-01 22:02:17 +01001501 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001502 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001503 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001504 *flags |= LYS_MAND_FALSE;
1505 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001506 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001507 free(buf);
1508 return LY_EVALID;
1509 }
1510 free(buf);
1511
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001512 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001514 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001515 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516 break;
1517 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001518 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 return LY_EVALID;
1520 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001521 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001522 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001523
1524cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001525 return ret;
1526}
1527
Michal Vaskoea5abea2018-09-18 13:10:54 +02001528/**
1529 * @brief Parse a restriction such as range or length.
1530 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001531 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001532 * @param[in] restr_kw Type of this particular restriction.
1533 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001534 * @return LY_ERR values.
1535 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001537parse_restr(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001539 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001540 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001541 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001542 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001543
1544 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001545 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546
Michal Vasko193dacd2022-10-13 08:43:05 +02001547 CHECK_NONEMPTY(ctx, word_len, lyplg_ext_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001548 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001549 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001550 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001552 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001553 LY_CHECK_RET(parse_text_field(ctx, restr->dsc, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001555 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001556 LY_CHECK_RET(parse_text_field(ctx, restr->ref, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001557 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001558 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02001559 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG,
1560 &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001561 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001562 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001563 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001565 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001566 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 break;
1568 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001569 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001570 return LY_EVALID;
1571 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001572 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001573 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001574
1575cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001576 return ret;
1577}
1578
Michal Vaskoea5abea2018-09-18 13:10:54 +02001579/**
1580 * @brief Parse a restriction that can have more instances such as must.
1581 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001582 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001583 * @param[in] restr_kw Type of this particular restriction.
1584 * @param[in,out] restrs Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001585 * @return LY_ERR values.
1586 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001587static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001588parse_restrs(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001589{
1590 struct lysp_restr *restr;
1591
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001592 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001593 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001594}
1595
Michal Vaskoea5abea2018-09-18 13:10:54 +02001596/**
1597 * @brief Parse the status statement.
1598 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001599 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001600 * @param[in,out] flags Flags to add to.
1601 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001602 * @return LY_ERR values.
1603 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001605parse_status(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001607 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001608 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001609 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001610 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001611
1612 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001613 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001614 return LY_EVALID;
1615 }
1616
1617 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001618 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001619
Radek Krejcif13b87b2020-12-01 22:02:17 +01001620 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001622 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001623 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001624 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 *flags |= LYS_STATUS_OBSLT;
1626 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001627 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628 free(buf);
1629 return LY_EVALID;
1630 }
1631 free(buf);
1632
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001633 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001635 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001636 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001637 break;
1638 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001639 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640 return LY_EVALID;
1641 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001642 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001643 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001644
1645cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001646 return ret;
1647}
1648
Michal Vaskoea5abea2018-09-18 13:10:54 +02001649/**
1650 * @brief Parse the when statement.
1651 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001652 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001653 * @param[in,out] when_p When pointer to parse to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001654 * @return LY_ERR values.
1655 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001656LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001657parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001659 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001660 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001661 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001662 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001664 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001665
1666 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001667 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001668 return LY_EVALID;
1669 }
1670
1671 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001672 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673
1674 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001675 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001676 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001677 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001678
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001679 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001680 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001681 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001682 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->dsc, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts),
1683 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001684 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001685 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001686 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->ref, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts),
1687 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001688 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001689 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001690 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, *when_p, LY_STMT_WHEN, 0, &when->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001691 break;
1692 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001693 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001694 ret = LY_EVALID;
1695 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001697 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001698 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001699
1700cleanup:
1701 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001702 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001703 free(when);
1704 } else {
1705 *when_p = when;
1706 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707 return ret;
1708}
1709
Michal Vaskoea5abea2018-09-18 13:10:54 +02001710/**
1711 * @brief Parse the anydata or anyxml statement.
1712 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001713 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001714 * @param[in] any_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001715 * @param[in] parent Node parent.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001716 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001717 * @return LY_ERR values.
1718 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001719LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001720parse_any(struct lysp_yang_ctx *ctx, enum ly_stmt any_kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001721{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001722 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001723 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001724 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001725 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001726 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001727
David Sedlák60adc092019-08-06 15:57:02 +02001728 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001729 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001730
Radek Krejci39b7fc22021-02-26 23:29:18 +01001731 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001732 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001735 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001736 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737
1738 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001739 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001741 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001742 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001743 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001744 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001745 LY_CHECK_RET(parse_text_field(ctx, any->dsc, LY_STMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001746 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001747 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001748 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001749 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001750 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001751 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001752 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001753 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001754 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001755 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001756 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001757 LY_CHECK_RET(parse_text_field(ctx, any->ref, LY_STMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001759 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001760 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001762 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001763 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001764 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001765 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001766 LY_CHECK_RET(parse_ext(ctx, word, word_len, any, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001767 break;
1768 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001769 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001770 return LY_EVALID;
1771 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001772 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001773 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001774
1775cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001776 return ret;
1777}
1778
Michal Vaskoea5abea2018-09-18 13:10:54 +02001779/**
1780 * @brief Parse the value or position statement. Substatement of type enum statement.
1781 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001782 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001783 * @param[in] val_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001784 * @param[in,out] enm Structure to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001785 * @return LY_ERR values.
1786 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001787LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001788parse_type_enum_value_pos(struct lysp_yang_ctx *ctx, enum ly_stmt val_kw, struct lysp_type_enum *enm)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001789{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001790 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001791 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001792 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001793 long long num = 0;
1794 unsigned long long unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001795 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001796
Michal Vasko193dacd2022-10-13 08:43:05 +02001797 if (enm->flags & LYS_SET_VALUE) {
1798 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001799 ret = LY_EVALID;
1800 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001801 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001802 enm->flags |= LYS_SET_VALUE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001803
1804 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001805 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001806
Radek Krejcid6b76452019-09-03 17:03:03 +02001807 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001808 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001809 ret = LY_EVALID;
1810 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811 }
1812
1813 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001814 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001815 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001816 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001817 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001818 ret = LY_EVALID;
1819 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001820 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001822 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001823 if (unum > UINT64_C(4294967295)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001824 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001825 ret = LY_EVALID;
1826 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001827 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 }
1829 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001830 if ((size_t)(ptr - word) != word_len) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001831 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001832 ret = LY_EVALID;
1833 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001834 }
1835 if (errno == ERANGE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001836 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001837 ret = LY_EVALID;
1838 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001839 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001840 if (val_kw == LY_STMT_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001841 enm->value = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001842 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02001843 enm->value = unum;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001845
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001846 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001847 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001848 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001849 ret = parse_ext(ctx, word, word_len, enm, val_kw, 0, &enm->exts);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001850 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001851 break;
1852 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001853 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001854 ret = LY_EVALID;
1855 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001856 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001857 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001858 }
Radek Krejci8b764662018-11-14 14:15:13 +01001859
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001860cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001861 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001862 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001863}
1864
Michal Vaskoea5abea2018-09-18 13:10:54 +02001865/**
1866 * @brief Parse the enum or bit statement. Substatement of type statement.
1867 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001868 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001869 * @param[in] enum_kw Type of this particular keyword.
1870 * @param[in,out] enums Enums or bits to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001871 * @return LY_ERR values.
1872 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001873static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001874parse_type_enum(struct lysp_yang_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001875{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001876 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001878 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001879 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001880 struct lysp_type_enum *enm;
1881
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001882 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001883
1884 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001885 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 +02001886 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001887 ret = lysp_check_enum_name((struct lysp_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001888 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001889 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001890
Michal Vasko12ef5362022-09-16 15:13:58 +02001891 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001892 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(enum_kw), enm->name);
Radek Krejci8b764662018-11-14 14:15:13 +01001893
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001894 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001895 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001896 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001897 LY_CHECK_RET(parse_text_field(ctx, enm->dsc, LY_STMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001898 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001899 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001900 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001901 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001903 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001904 LY_CHECK_RET(parse_text_field(ctx, enm->ref, LY_STMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001905 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001906 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001907 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001909 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001910 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1911 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1912 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
David Sedlák9fb515f2019-07-11 10:33:58 +02001913 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001914 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001915 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1916 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1917 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001918 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001919 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001920 LY_CHECK_RET(parse_ext(ctx, word, word_len, enm, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921 break;
1922 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001923 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924 return LY_EVALID;
1925 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001926 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001928
1929cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001930 return ret;
1931}
1932
Michal Vaskoea5abea2018-09-18 13:10:54 +02001933/**
1934 * @brief Parse the fraction-digits statement. Substatement of type statement.
1935 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001936 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001937 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001938 * @return LY_ERR values.
1939 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001940static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001941parse_type_fracdigits(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001942{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001943 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001944 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001945 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001946 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001947 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001948
Michal Vasko193dacd2022-10-13 08:43:05 +02001949 if (type->fraction_digits) {
David Sedlákb3077192019-06-19 10:55:37 +02001950 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951 return LY_EVALID;
1952 }
1953
1954 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001955 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956
1957 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001958 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001959 ret = LY_EVALID;
1960 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001961 }
1962
1963 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001964 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001965 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001966 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001967 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001968 ret = LY_EVALID;
1969 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001970 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001971 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001972 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001973 ret = LY_EVALID;
1974 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001976 type->fraction_digits = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001978 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001980 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001981 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, type, LY_STMT_FRACTION_DIGITS, 0, &type->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001982 break;
1983 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001984 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001985 ret = LY_EVALID;
1986 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001988 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001990
1991cleanup:
1992 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001993 return ret;
1994}
1995
Michal Vaskoea5abea2018-09-18 13:10:54 +02001996/**
1997 * @brief Parse the require-instance statement. Substatement of type statement.
1998 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001999 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002000 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002001 * @return LY_ERR values.
2002 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002004parse_type_reqinstance(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002005{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002006 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002007 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002008 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002009 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002010
Michal Vasko193dacd2022-10-13 08:43:05 +02002011 if (type->flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002012 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002013 return LY_EVALID;
2014 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002015 type->flags |= LYS_SET_REQINST;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002016
2017 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002018 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019
Radek Krejcif13b87b2020-12-01 22:02:17 +01002020 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002021 type->require_instance = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002022 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002023 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002024 ret = LY_EVALID;
2025 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002028 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002030 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002031 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, type, LY_STMT_REQUIRE_INSTANCE, 0, &type->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002032 break;
2033 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002034 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002035 ret = LY_EVALID;
2036 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002038 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002040
2041cleanup:
2042 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043 return ret;
2044}
2045
Michal Vaskoea5abea2018-09-18 13:10:54 +02002046/**
2047 * @brief Parse the modifier statement. Substatement of type pattern statement.
2048 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002049 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002050 * @param[in,out] restr Restriction to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002051 * @return LY_ERR values.
2052 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002054parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002056 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002057 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002058 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002059 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002060
Michal Vasko193dacd2022-10-13 08:43:05 +02002061 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002062 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002063 return LY_EVALID;
2064 }
2065
2066 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002067 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068
Radek Krejcif13b87b2020-12-01 22:02:17 +01002069 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002070 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002071 ret = LY_EVALID;
2072 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002073 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074
2075 /* replace the value in the dictionary */
Michal Vasko193dacd2022-10-13 08:43:05 +02002076 buf = malloc(strlen(restr->arg.str) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002077 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02002078 strcpy(buf, restr->arg.str);
2079 lydict_remove(PARSER_CTX(ctx), restr->arg.str);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080
Radek Krejcif13b87b2020-12-01 22:02:17 +01002081 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2082 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko183ea332022-11-08 10:50:03 +01002083 ret = lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002084 buf = NULL;
Michal Vasko183ea332022-11-08 10:50:03 +01002085 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002087 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002089 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002090 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, restr, LY_STMT_MODIFIER, 0, &restr->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002091 break;
2092 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002093 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002094 ret = LY_EVALID;
2095 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002097 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002099
2100cleanup:
2101 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 return ret;
2103}
2104
Michal Vaskoea5abea2018-09-18 13:10:54 +02002105/**
2106 * @brief Parse the pattern statement. Substatement of type statement.
2107 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002108 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002109 * @param[in,out] patterns Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002110 * @return LY_ERR values.
2111 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002113parse_type_pattern(struct lysp_yang_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002115 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002117 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002118 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002119 struct lysp_restr *restr;
2120
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002121 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122
2123 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002124 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125
2126 /* add special meaning first byte */
2127 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002128 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 word = buf;
2130 } else {
2131 buf = malloc(word_len + 2);
2132 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002133 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002134 if (word_len) {
2135 memmove(buf + 1, word, word_len);
2136 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002137 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002138 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002139 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002140 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002142 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002144 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002145 LY_CHECK_RET(parse_text_field(ctx, restr->dsc, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002147 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002148 LY_CHECK_RET(parse_text_field(ctx, restr->ref, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002150 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02002151 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002153 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002154 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002156 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002157 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko193dacd2022-10-13 08:43:05 +02002158 LY_CHECK_RET(parse_type_pattern_modifier(ctx, restr));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002160 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002161 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 break;
2163 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002164 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165 return LY_EVALID;
2166 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002167 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002169
2170cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 return ret;
2172}
2173
Michal Vaskoea5abea2018-09-18 13:10:54 +02002174/**
2175 * @brief Parse the type statement.
2176 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002177 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002178 * @param[in,out] type Type to wrote to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002179 * @return LY_ERR values.
2180 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002182parse_type(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002184 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002186 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002187 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002188 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 struct lysp_type *nest_type;
2190
2191 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002192 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002193 return LY_EVALID;
2194 }
2195
2196 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002197 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002198 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002199
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002200 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002201 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002202
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002203 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002204 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002205 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002206 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 +01002207 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002209 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002210 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002211 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002213 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002214 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002215 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002217 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002218 LY_CHECK_RET(parse_type_fracdigits(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002219 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002221 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002223 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224 return LY_EVALID;
2225 }
2226 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002227 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228
Radek Krejci33090f92020-12-17 20:12:46 +01002229 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002230 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002232 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002233 if (type->path) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002234 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002235 return LY_EVALID;
2236 }
2237
aPiecek4bb1e372021-05-07 11:01:00 +02002238 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2239 * corresponding structure, so in case of failure, the lysp_module_free function will take
2240 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2241 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2242 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002243 LY_CHECK_ERR_RET(ret = parse_text_field(ctx, type, LY_STMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts),
aPiecek4bb1e372021-05-07 11:01:00 +02002244 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002245 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002246 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002247 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002248 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002249 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002250 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002252 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002253 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002254 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002256 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002257 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002258 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002259 return LY_EVALID;
2260 }
2261 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002262 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263
Radek Krejci33090f92020-12-17 20:12:46 +01002264 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002265 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002267 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002268 LY_CHECK_RET(parse_type_reqinstance(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002269 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002271 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002272 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002273 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002274 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002275 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002276 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002277 LY_CHECK_RET(parse_ext(ctx, word, word_len, type, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 break;
2279 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002280 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281 return LY_EVALID;
2282 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002283 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002285
2286cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002287 return ret;
2288}
2289
Michal Vaskoea5abea2018-09-18 13:10:54 +02002290/**
2291 * @brief Parse the leaf statement.
2292 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002293 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002294 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002295 * @return LY_ERR values.
2296 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002297LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002298parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002300 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002302 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002303 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304 struct lysp_node_leaf *leaf;
2305
David Sedlák60adc092019-08-06 15:57:02 +02002306 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002307 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002309 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002312 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002313 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314
2315 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002316 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002318 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002319 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002321 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002322 LY_CHECK_RET(parse_text_field(ctx, &leaf->dflt, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002323 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002325 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002326 LY_CHECK_RET(parse_text_field(ctx, leaf->dsc, LY_STMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002328 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002329 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002331 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002332 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002334 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002335 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002336 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002337 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002338 LY_CHECK_RET(parse_text_field(ctx, leaf->ref, LY_STMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002339 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002340 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002341 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002342 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002343 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002344 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002345 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002346 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002347 LY_CHECK_RET(parse_text_field(ctx, leaf->units, LY_STMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002349 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002350 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002351 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002352 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002353 LY_CHECK_RET(parse_ext(ctx, word, word_len, leaf, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002354 break;
2355 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002356 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357 return LY_EVALID;
2358 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002359 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002361
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362 /* mandatory substatements */
2363 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002364 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365 return LY_EVALID;
2366 }
2367
Michal Vasko12ef5362022-09-16 15:13:58 +02002368cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 return ret;
2370}
2371
Michal Vaskoea5abea2018-09-18 13:10:54 +02002372/**
2373 * @brief Parse the max-elements statement.
2374 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002375 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002376 * @param[in,out] max Value to write to.
2377 * @param[in,out] flags Flags to write to.
2378 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002379 * @return LY_ERR values.
2380 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002381LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002382parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002384 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002385 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002386 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002387 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002388 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002389
2390 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002391 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002392 return LY_EVALID;
2393 }
2394 *flags |= LYS_SET_MAX;
2395
2396 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002397 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002398
2399 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002400 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002401 ret = LY_EVALID;
2402 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 }
2404
Radek Krejci7f9b6512019-09-18 13:11:09 +02002405 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002407 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002409 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002410 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002411 ret = LY_EVALID;
2412 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413 }
2414 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002415 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002416 ret = LY_EVALID;
2417 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 }
2419
2420 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002421 } else {
2422 /* unbounded */
2423 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002424 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002425
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002426 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002428 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002429 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, max, LY_STMT_MAX_ELEMENTS, 0, exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002430 break;
2431 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002432 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002433 ret = LY_EVALID;
2434 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002436 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002438
2439cleanup:
2440 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002441 return ret;
2442}
2443
Michal Vaskoea5abea2018-09-18 13:10:54 +02002444/**
2445 * @brief Parse the min-elements statement.
2446 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002447 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002448 * @param[in,out] min Value to write to.
2449 * @param[in,out] flags Flags to write to.
2450 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002451 * @return LY_ERR values.
2452 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002453LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002454parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002456 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002457 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002458 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002459 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002460 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002461
2462 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002463 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002464 return LY_EVALID;
2465 }
2466 *flags |= LYS_SET_MIN;
2467
2468 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002469 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002470
2471 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002472 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002473 ret = LY_EVALID;
2474 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 }
2476
2477 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002478 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002480 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002481 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002482 ret = LY_EVALID;
2483 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002484 }
2485 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002486 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002487 ret = LY_EVALID;
2488 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002489 }
2490 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002491
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002492 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002494 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002495 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, min, LY_STMT_MIN_ELEMENTS, 0, exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002496 break;
2497 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002498 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002499 ret = LY_EVALID;
2500 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002502 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002504
2505cleanup:
2506 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 return ret;
2508}
2509
Michal Vaskoea5abea2018-09-18 13:10:54 +02002510/**
2511 * @brief Parse the ordered-by statement.
2512 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002513 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002514 * @param[in,out] llist List or leaf-list to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002515 * @return LY_ERR values.
2516 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002518parse_orderedby(struct lysp_yang_ctx *ctx, struct lysp_node *llist)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002520 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002521 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002522 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002523 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002524
Michal Vasko193dacd2022-10-13 08:43:05 +02002525 if (llist->flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002526 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527 return LY_EVALID;
2528 }
2529
2530 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002531 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002532
Radek Krejcif13b87b2020-12-01 22:02:17 +01002533 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002534 llist->flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002535 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002536 llist->flags |= LYS_ORDBY_USER;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002538 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002539 ret = LY_EVALID;
2540 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002543 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002544 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002545 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002546 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, llist, LY_STMT_ORDERED_BY, 0, &llist->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002547 break;
2548 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002549 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002550 ret = LY_EVALID;
2551 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002553 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002555
2556cleanup:
2557 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 return ret;
2559}
2560
Michal Vaskoea5abea2018-09-18 13:10:54 +02002561/**
2562 * @brief Parse the leaf-list statement.
2563 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002564 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002565 * @param[in,out] siblings Siblings to add to.
2566 *
2567 * @return LY_ERR values.
2568 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002569LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002570parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002572 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002573 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002574 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002575 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002576 struct lysp_node_leaflist *llist;
2577
David Sedlák60adc092019-08-06 15:57:02 +02002578 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002579 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002580 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002581 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002582
Michal Vasko7fbc8162018-09-17 10:35:16 +02002583 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002584 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002585 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586
2587 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002588 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002589 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002590 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002591 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002593 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002594 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002595 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002596 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002597 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002598 LY_CHECK_RET(parse_text_field(ctx, llist->dsc, LY_STMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002599 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002600 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002601 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002603 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002604 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002606 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002607 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002609 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002610 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002612 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02002613 LY_CHECK_RET(parse_orderedby(ctx, &llist->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002615 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002616 LY_CHECK_RET(parse_text_field(ctx, llist->ref, LY_STMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002618 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002619 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002621 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002622 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002624 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002625 LY_CHECK_RET(parse_text_field(ctx, llist->units, LY_STMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002627 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002628 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002630 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002631 LY_CHECK_RET(parse_ext(ctx, word, word_len, llist, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002632 break;
2633 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002634 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635 return LY_EVALID;
2636 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002637 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002639
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640 /* mandatory substatements */
2641 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002642 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002643 return LY_EVALID;
2644 }
2645
Michal Vasko12ef5362022-09-16 15:13:58 +02002646cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647 return ret;
2648}
2649
Michal Vaskoea5abea2018-09-18 13:10:54 +02002650/**
2651 * @brief Parse the refine statement.
2652 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002653 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002654 * @param[in,out] refines Refines to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002655 * @return LY_ERR values.
2656 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002658parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002659{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002660 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002662 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002663 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002664 struct lysp_refine *rf;
2665
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002666 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667
2668 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002669 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002670 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002671 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002673 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002674 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002675 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002676 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002677 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002678 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002679 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002681 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002682 LY_CHECK_RET(parse_text_field(ctx, rf->dsc, LY_STMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002683 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002684 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002685 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002686 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002688 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002689 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002690 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002691 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002692 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002693 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002694 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002695 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002696 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002697 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002698 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002699 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002700 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002701 LY_CHECK_RET(parse_text_field(ctx, rf->ref, LY_STMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002703 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002704 LY_CHECK_RET(parse_text_field(ctx, rf->presence, LY_STMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002705 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002706 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002707 LY_CHECK_RET(parse_ext(ctx, word, word_len, rf, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002708 break;
2709 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002710 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002711 return LY_EVALID;
2712 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002713 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002714 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002715
2716cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002717 return ret;
2718}
2719
Michal Vaskoea5abea2018-09-18 13:10:54 +02002720/**
2721 * @brief Parse the typedef statement.
2722 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002723 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002724 * @param[in,out] typedefs Typedefs to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002725 * @return LY_ERR values.
2726 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002728parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002730 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002732 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002733 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734 struct lysp_tpdf *tpdf;
2735
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002736 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737
2738 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002739 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002740 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741
2742 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002743 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002744 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002745 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002746 LY_CHECK_RET(parse_text_field(ctx, &tpdf->dflt, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002747 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002748 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002749 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002750 LY_CHECK_RET(parse_text_field(ctx, tpdf->dsc, LY_STMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002752 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002753 LY_CHECK_RET(parse_text_field(ctx, tpdf->ref, LY_STMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002754 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002755 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002756 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002758 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002759 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002760 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002761 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002762 LY_CHECK_RET(parse_text_field(ctx, tpdf->units, LY_STMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002764 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002765 LY_CHECK_RET(parse_ext(ctx, word, word_len, tpdf, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002766 break;
2767 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002768 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 return LY_EVALID;
2770 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002771 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002773
Michal Vasko7fbc8162018-09-17 10:35:16 +02002774 /* mandatory substatements */
2775 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002776 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002777 return LY_EVALID;
2778 }
2779
Radek Krejcibbe09a92018-11-08 09:36:54 +01002780 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002781 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002782 assert(ctx->main_ctx);
2783 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002784 }
2785
Michal Vasko12ef5362022-09-16 15:13:58 +02002786cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 return ret;
2788}
2789
Michal Vaskoea5abea2018-09-18 13:10:54 +02002790/**
2791 * @brief Parse the input or output statement.
2792 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002793 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002794 * @param[in] kw Type of this particular keyword
2795 * @param[in,out] inout_p Input/output pointer to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002796 * @return LY_ERR values.
2797 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002799parse_inout(struct lysp_yang_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002800 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002801{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002802 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002803 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002804 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002805 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002806 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002807
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002808 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002809 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002810 return LY_EVALID;
2811 }
2812
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002813 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002814 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2815 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002816 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817
2818 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002819 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002821 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002822 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002823 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002824 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002825 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002827 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002828 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002829 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002830 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002831 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002832 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002833 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002834 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002835 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002836 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002837 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002838 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002839 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002840 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002841 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002842 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002843 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002845 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002846 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002847 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002848 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002849 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002850 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002852 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002853 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002855 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002856 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_p, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 break;
2858 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002859 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002860 return LY_EVALID;
2861 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002862 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002863 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002864
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002865cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002866 return ret;
2867}
2868
Michal Vaskoea5abea2018-09-18 13:10:54 +02002869/**
2870 * @brief Parse the action statement.
2871 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002872 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002873 * @param[in,out] actions Actions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002874 * @return LY_ERR values.
2875 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002876LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002877parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002878{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002879 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002881 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002882 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002883 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002884
Radek Krejci2a9fc652021-01-22 17:44:34 +01002885 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002886
2887 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002888 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002889 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002890 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002891 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002893 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002894 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002895 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002896 LY_CHECK_RET(parse_text_field(ctx, act->dsc, LY_STMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002897 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002898 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002899 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002900 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002901 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002902 LY_CHECK_RET(parse_text_field(ctx, act->ref, LY_STMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002903 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002904 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002905 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002906 break;
2907
Radek Krejcid6b76452019-09-03 17:03:03 +02002908 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002909 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002911 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002912 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 break;
2914
Radek Krejcid6b76452019-09-03 17:03:03 +02002915 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002916 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002918 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002919 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002920 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002921 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002922 LY_CHECK_RET(parse_ext(ctx, word, word_len, act, parent ? LY_STMT_ACTION : LY_STMT_RPC, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 break;
2924 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002925 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002926 return LY_EVALID;
2927 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002928 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002930
2931 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2932 if (!act->input.nodetype) {
2933 act->input.nodetype = LYS_INPUT;
2934 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002935 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002936 }
2937 if (!act->output.nodetype) {
2938 act->output.nodetype = LYS_OUTPUT;
2939 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002940 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002941 }
2942
Michal Vasko12ef5362022-09-16 15:13:58 +02002943cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944 return ret;
2945}
2946
Michal Vaskoea5abea2018-09-18 13:10:54 +02002947/**
2948 * @brief Parse the notification statement.
2949 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002950 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002951 * @param[in,out] notifs Notifications to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002952 * @return LY_ERR values.
2953 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002954LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002955parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002956{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002957 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002959 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002960 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002961 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002962
Radek Krejci2a9fc652021-01-22 17:44:34 +01002963 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964
2965 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002966 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002967 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002968 notif->nodetype = LYS_NOTIF;
2969 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002971 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002972 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002973 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002974 LY_CHECK_RET(parse_text_field(ctx, notif->dsc, LY_STMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002976 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002977 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002978 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002979 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002980 LY_CHECK_RET(parse_text_field(ctx, notif->ref, LY_STMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002982 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002983 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002984 break;
2985
Radek Krejcid6b76452019-09-03 17:03:03 +02002986 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002987 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002988 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002989 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002990 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002992 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002993 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002995 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002996 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002998 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002999 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003001 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003002 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003004 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003005 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003007 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003008 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 break;
3010
Radek Krejcid6b76452019-09-03 17:03:03 +02003011 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003012 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003013 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003015 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003016 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003018 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003019 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003021 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003022 LY_CHECK_RET(parse_ext(ctx, word, word_len, notif, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023 break;
3024 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003025 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026 return LY_EVALID;
3027 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003028 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003029 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003030
Michal Vasko12ef5362022-09-16 15:13:58 +02003031cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003032 return ret;
3033}
3034
Michal Vaskoea5abea2018-09-18 13:10:54 +02003035/**
3036 * @brief Parse the grouping statement.
3037 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003038 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003039 * @param[in,out] groupings Groupings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003040 * @return LY_ERR values.
3041 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003042LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003043parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003044{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003045 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003046 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003047 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003048 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003049 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050
Radek Krejci2a9fc652021-01-22 17:44:34 +01003051 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052
3053 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003054 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003055 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003056 grp->nodetype = LYS_GROUPING;
3057 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003059 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003061 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003062 LY_CHECK_RET(parse_text_field(ctx, grp->dsc, LY_STMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003064 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003065 LY_CHECK_RET(parse_text_field(ctx, grp->ref, LY_STMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003067 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003068 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 break;
3070
Radek Krejcid6b76452019-09-03 17:03:03 +02003071 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003072 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003073 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003074 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003075 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003076 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003077 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003078 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003080 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003081 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003082 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003083 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003084 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003086 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003087 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003089 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003090 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003091 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003092 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003093 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003094 break;
3095
Radek Krejcid6b76452019-09-03 17:03:03 +02003096 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003097 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003099 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003100 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003101 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003103 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003104 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003106 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003107 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003108 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003109 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003110 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003111 LY_CHECK_RET(parse_ext(ctx, word, word_len, grp, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 break;
3113 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003114 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 return LY_EVALID;
3116 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003117 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003118 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003119
aPiecek63e080d2021-06-29 13:53:28 +02003120 /* store data for collision check */
3121 if (parent) {
3122 assert(ctx->main_ctx);
3123 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3124 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003125
Michal Vasko12ef5362022-09-16 15:13:58 +02003126cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003127 return ret;
3128}
3129
Michal Vaskoea5abea2018-09-18 13:10:54 +02003130/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003131 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003132 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003133 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003134 * @param[in,out] augments Augments to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003135 * @return LY_ERR values.
3136 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003137LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003138parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003140 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003142 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003143 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003144 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145
Radek Krejci2a9fc652021-01-22 17:44:34 +01003146 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147
3148 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003149 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003150 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003151 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003152 aug->nodetype = LYS_AUGMENT;
3153 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003155 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003156 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003157 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003158 LY_CHECK_RET(parse_text_field(ctx, aug->dsc, LY_STMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003160 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003161 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003163 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003164 LY_CHECK_RET(parse_text_field(ctx, aug->ref, LY_STMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003166 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003167 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003169 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003170 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 break;
3172
Radek Krejcid6b76452019-09-03 17:03:03 +02003173 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003174 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003175 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003176 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003177 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003179 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003180 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003182 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003183 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003185 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003186 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003188 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003189 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003191 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003192 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003194 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003195 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003197 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003198 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199 break;
3200
Radek Krejcid6b76452019-09-03 17:03:03 +02003201 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003202 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003203 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003205 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003206 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003207 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003209 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003210 LY_CHECK_RET(parse_ext(ctx, word, word_len, aug, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211 break;
3212 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003213 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 return LY_EVALID;
3215 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003216 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003218
Michal Vasko12ef5362022-09-16 15:13:58 +02003219cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 return ret;
3221}
3222
Michal Vaskoea5abea2018-09-18 13:10:54 +02003223/**
3224 * @brief Parse the uses statement.
3225 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003226 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003227 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003228 * @return LY_ERR values.
3229 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003230LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003231parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003232{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003233 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003234 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003235 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003236 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 struct lysp_node_uses *uses;
3238
David Sedlák60adc092019-08-06 15:57:02 +02003239 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003240 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003241 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003242 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003245 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003246 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247
3248 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003249 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003250 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003251 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003252 LY_CHECK_RET(parse_text_field(ctx, uses->dsc, LY_STMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003254 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003255 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003256 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003257 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003258 LY_CHECK_RET(parse_text_field(ctx, uses->ref, LY_STMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003259 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003260 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003261 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003263 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003264 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003265 break;
3266
Radek Krejcid6b76452019-09-03 17:03:03 +02003267 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003268 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003269 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003270 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003271 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003273 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003274 LY_CHECK_RET(parse_ext(ctx, word, word_len, uses, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275 break;
3276 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003277 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278 return LY_EVALID;
3279 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003280 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003281 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003282
Michal Vasko12ef5362022-09-16 15:13:58 +02003283cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003284 return ret;
3285}
3286
Michal Vaskoea5abea2018-09-18 13:10:54 +02003287/**
3288 * @brief Parse the case statement.
3289 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003290 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003291 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003292 * @return LY_ERR values.
3293 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003294LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003295parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003296{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003297 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003298 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003299 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003300 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 struct lysp_node_case *cas;
3302
David Sedlák60adc092019-08-06 15:57:02 +02003303 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003304 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003306 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003309 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003310 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311
3312 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003313 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003315 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003316 LY_CHECK_RET(parse_text_field(ctx, cas->dsc, LY_STMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003318 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003319 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003321 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003322 LY_CHECK_RET(parse_text_field(ctx, cas->ref, LY_STMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003324 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003325 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003328 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
3330
Radek Krejcid6b76452019-09-03 17:03:03 +02003331 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003332 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003333 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003334 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003335 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003336 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003337 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003338 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003340 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003341 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003343 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003344 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003346 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003347 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003349 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003350 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003352 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003353 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003354 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003355 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003356 LY_CHECK_RET(parse_ext(ctx, word, word_len, cas, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357 break;
3358 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003359 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003360 return LY_EVALID;
3361 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003362 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003364
3365cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 return ret;
3367}
3368
Michal Vaskoea5abea2018-09-18 13:10:54 +02003369/**
3370 * @brief Parse the choice statement.
3371 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003372 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003373 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003374 * @return LY_ERR values.
3375 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003376LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003377parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003379 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003381 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003382 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003383 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384
David Sedlák60adc092019-08-06 15:57:02 +02003385 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003386 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003387 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003388 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003389
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003391 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003392 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393
3394 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003395 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003397 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003398 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003400 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003401 LY_CHECK_RET(parse_text_field(ctx, choice->dsc, LY_STMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003403 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003404 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003406 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003407 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003409 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003410 LY_CHECK_RET(parse_text_field(ctx, choice->ref, LY_STMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003412 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003413 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003415 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003416 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003418 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003419 LY_CHECK_RET(parse_text_field(ctx, &choice->dflt, LY_STMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
Michal Vasko7f45cf22020-10-01 12:49:44 +02003420 &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003421 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003422 break;
3423
Radek Krejcid6b76452019-09-03 17:03:03 +02003424 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003425 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003426 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003427 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003428 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003430 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003431 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003433 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003434 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003435 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003437 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003438 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003440 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003441 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003443 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003444 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003446 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003447 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003449 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003450 LY_CHECK_RET(parse_ext(ctx, word, word_len, choice, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003451 break;
3452 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003453 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003454 return LY_EVALID;
3455 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003456 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003457 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003458
3459cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003460 return ret;
3461}
3462
Michal Vaskoea5abea2018-09-18 13:10:54 +02003463/**
3464 * @brief Parse the container statement.
3465 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003466 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003467 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003468 * @return LY_ERR values.
3469 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003470LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003471parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472{
3473 LY_ERR ret = 0;
3474 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003475 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003476 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003477 struct lysp_node_container *cont;
3478
David Sedlák60adc092019-08-06 15:57:02 +02003479 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003480 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003481 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003482 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003485 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003486 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487
3488 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003489 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003491 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003492 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003494 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003495 LY_CHECK_RET(parse_text_field(ctx, cont->dsc, LY_STMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003497 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003498 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003500 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003501 LY_CHECK_RET(parse_text_field(ctx, cont->ref, LY_STMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003502 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003503 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003504 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003506 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003507 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003509 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003510 LY_CHECK_RET(parse_text_field(ctx, cont->presence, LY_STMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 break;
3512
Radek Krejcid6b76452019-09-03 17:03:03 +02003513 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003514 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003515 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003516 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003517 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003519 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003520 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003522 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003523 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003524 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003525 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003526 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003528 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003529 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003531 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003532 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003534 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003535 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003536 break;
3537
Radek Krejcid6b76452019-09-03 17:03:03 +02003538 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003539 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003541 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003542 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003544 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003545 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003546 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003548 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003549 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003551 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003552 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003553 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003554 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003555 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003556 LY_CHECK_RET(parse_ext(ctx, word, word_len, cont, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557 break;
3558 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003559 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 return LY_EVALID;
3561 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003562 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003564
Michal Vasko12ef5362022-09-16 15:13:58 +02003565cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 return ret;
3567}
3568
Michal Vaskoea5abea2018-09-18 13:10:54 +02003569/**
3570 * @brief Parse the list statement.
3571 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003572 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003573 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003574 * @return LY_ERR values.
3575 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003576LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003577parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003579 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003581 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003582 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003583 struct lysp_node_list *list;
3584
David Sedlák60adc092019-08-06 15:57:02 +02003585 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003586 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003588 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003591 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003592 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593
3594 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003595 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003597 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003598 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003600 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003601 LY_CHECK_RET(parse_text_field(ctx, list->dsc, LY_STMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003603 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003604 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003606 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003607 LY_CHECK_RET(parse_text_field(ctx, list->ref, LY_STMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003609 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003610 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003611 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003612 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003613 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003615 case LY_STMT_KEY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003616 LY_CHECK_RET(parse_text_field(ctx, list, LY_STMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003618 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003619 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003621 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003622 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003624 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003625 LY_CHECK_RET(parse_orderedby(ctx, &list->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003627 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003628 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 break;
3630
Radek Krejcid6b76452019-09-03 17:03:03 +02003631 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003632 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003633 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003634 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003635 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003637 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003638 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003640 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003641 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003642 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003643 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003644 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003646 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003647 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003648 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003649 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003650 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003651 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003652 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003653 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003654 break;
3655
Radek Krejcid6b76452019-09-03 17:03:03 +02003656 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003657 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003659 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003660 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003661 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003662 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003663 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003664 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003666 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003667 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003669 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003670 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003671 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003673 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003674 LY_CHECK_RET(parse_ext(ctx, word, word_len, list, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003675 break;
3676 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003677 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 return LY_EVALID;
3679 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003680 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003681 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003682
Michal Vasko12ef5362022-09-16 15:13:58 +02003683cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003684 return ret;
3685}
3686
Michal Vaskoea5abea2018-09-18 13:10:54 +02003687/**
3688 * @brief Parse the yin-element statement.
3689 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003690 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003691 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003692 * @return LY_ERR values.
3693 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003695parse_yinelement(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003696{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003697 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003698 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003699 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003700 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701
Michal Vasko193dacd2022-10-13 08:43:05 +02003702 if (ext->flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003703 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704 return LY_EVALID;
3705 }
3706
3707 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003708 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709
Radek Krejcif13b87b2020-12-01 22:02:17 +01003710 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003711 ext->flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003712 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003713 ext->flags |= LYS_YINELEM_FALSE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003715 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003716 free(buf);
3717 return LY_EVALID;
3718 }
3719 free(buf);
3720
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003721 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003722 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003723 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003724 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_YIN_ELEMENT, 0, &ext->exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003725 LY_CHECK_RET(ret);
3726 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003727 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003728 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yin-element");
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, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003733
3734cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 return ret;
3736}
3737
Michal Vaskoea5abea2018-09-18 13:10:54 +02003738/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003739 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003740 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003741 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003742 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003743 * @return LY_ERR values.
3744 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003746parse_argument(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003748 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003749 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003750 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003751 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003752
Michal Vasko193dacd2022-10-13 08:43:05 +02003753 if (ext->argname) {
David Sedlákb3077192019-06-19 10:55:37 +02003754 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755 return LY_EVALID;
3756 }
3757
3758 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003759 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko193dacd2022-10-13 08:43:05 +02003760 INSERT_WORD_GOTO(ctx, buf, ext->argname, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003762 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003763 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003764 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003765 LY_CHECK_RET(parse_yinelement(ctx, ext));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003767 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003768 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_ARGUMENT, 0, &ext->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769 break;
3770 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003771 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 return LY_EVALID;
3773 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003774 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003776
3777cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 return ret;
3779}
3780
Michal Vaskoea5abea2018-09-18 13:10:54 +02003781/**
3782 * @brief Parse the extension statement.
3783 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003784 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003785 * @param[in,out] extensions Extensions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003786 * @return LY_ERR values.
3787 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003789parse_extension(struct lysp_yang_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003790{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003791 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003792 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003793 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003794 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 struct lysp_ext *ex;
3796
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003797 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798
3799 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003800 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003801 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003802
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003803 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003804 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003805 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003806 LY_CHECK_RET(parse_text_field(ctx, ex->dsc, LY_STMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003807 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003808 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003809 LY_CHECK_RET(parse_text_field(ctx, ex->ref, LY_STMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003810 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003811 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003812 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003813 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003814 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003815 LY_CHECK_RET(parse_argument(ctx, ex));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003816 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003817 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003818 LY_CHECK_RET(parse_ext(ctx, word, word_len, ex, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 break;
3820 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003821 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003822 return LY_EVALID;
3823 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003824 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003825 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003826
3827cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 return ret;
3829}
3830
Michal Vaskoea5abea2018-09-18 13:10:54 +02003831/**
3832 * @brief Parse the deviate statement.
3833 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003834 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003835 * @param[in,out] deviates Deviates to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003836 * @return LY_ERR values.
3837 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003838LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003839parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003840{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003841 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003842 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003843 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003844 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003845 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003846 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847 struct lysp_deviate_add *d_add = NULL;
3848 struct lysp_deviate_rpl *d_rpl = NULL;
3849 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003850 const char **d_units = NULL;
3851 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003852 struct lysp_restr **d_musts = NULL;
3853 uint16_t *d_flags = 0;
3854 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855
3856 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003857 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858
Radek Krejcif13b87b2020-12-01 22:02:17 +01003859 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003860 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003861 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003863 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003865 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 dev_mod = LYS_DEV_DELETE;
3867 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003868 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003869 ret = LY_EVALID;
3870 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003871 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872
3873 /* create structure */
3874 switch (dev_mod) {
3875 case LYS_DEV_NOT_SUPPORTED:
3876 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003877 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003878 break;
3879 case LYS_DEV_ADD:
3880 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003881 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003882 d = (struct lysp_deviate *)d_add;
3883 d_units = &d_add->units;
3884 d_uniques = &d_add->uniques;
3885 d_dflts = &d_add->dflts;
3886 d_musts = &d_add->musts;
3887 d_flags = &d_add->flags;
3888 d_min = &d_add->min;
3889 d_max = &d_add->max;
3890 break;
3891 case LYS_DEV_REPLACE:
3892 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003893 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894 d = (struct lysp_deviate *)d_rpl;
3895 d_units = &d_rpl->units;
3896 d_flags = &d_rpl->flags;
3897 d_min = &d_rpl->min;
3898 d_max = &d_rpl->max;
3899 break;
3900 case LYS_DEV_DELETE:
3901 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003902 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003903 d = (struct lysp_deviate *)d_del;
3904 d_units = &d_del->units;
3905 d_uniques = &d_del->uniques;
3906 d_dflts = &d_del->dflts;
3907 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003908 break;
3909 default:
3910 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003911 LOGINT(PARSER_CTX(ctx));
3912 ret = LY_EINT;
3913 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914 }
3915 d->mod = dev_mod;
3916
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003917 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003918 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003919 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003920 switch (dev_mod) {
3921 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003922 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003923 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003924 ret = LY_EVALID;
3925 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003926 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003927 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928 break;
3929 }
3930 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003931 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003932 switch (dev_mod) {
3933 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02003934 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003935 ret = LY_EVALID;
3936 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003938 ret = parse_text_field(ctx, &d_rpl->dflt, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts);
3939 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003940 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003941 break;
3942 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003943 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 +02003944 break;
3945 }
3946 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003947 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003948 switch (dev_mod) {
3949 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003950 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003951 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003952 ret = LY_EVALID;
3953 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003955 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956 break;
3957 }
3958 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003959 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960 switch (dev_mod) {
3961 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003962 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003963 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003964 ret = LY_EVALID;
3965 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003966 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003967 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003968 break;
3969 }
3970 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003971 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003972 switch (dev_mod) {
3973 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003974 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003975 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003976 ret = LY_EVALID;
3977 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003979 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003980 break;
3981 }
3982 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003983 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984 switch (dev_mod) {
3985 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003986 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003987 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003988 ret = LY_EVALID;
3989 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003991 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003992 break;
3993 }
3994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003995 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 switch (dev_mod) {
3997 case LYS_DEV_NOT_SUPPORTED:
3998 case LYS_DEV_ADD:
3999 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004000 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004001 ret = LY_EVALID;
4002 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004004 if (d_rpl->type) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004005 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004006 ret = LY_EVALID;
4007 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004008 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004009 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004010 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4011 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004012 break;
4013 }
4014 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004015 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016 switch (dev_mod) {
4017 case LYS_DEV_NOT_SUPPORTED:
4018 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004019 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004020 ret = LY_EVALID;
4021 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004022 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004023 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 +02004024 break;
4025 }
4026 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004027 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004028 switch (dev_mod) {
4029 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02004030 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004031 ret = LY_EVALID;
4032 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004034 LY_CHECK_GOTO(ret = parse_text_field(ctx, *d_units, LY_STMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004035 break;
4036 }
4037 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004038 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004039 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, d, LY_STMT_DEVIATE, 0, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040 break;
4041 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004042 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004043 ret = LY_EVALID;
4044 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004045 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004046 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004047 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004048
4049cleanup:
4050 free(buf);
4051 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004052 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004053 free(d);
4054 } else {
4055 /* insert into siblings */
4056 LY_LIST_INSERT(deviates, d, next);
4057 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004058 return ret;
4059}
4060
Michal Vaskoea5abea2018-09-18 13:10:54 +02004061/**
4062 * @brief Parse the deviation statement.
4063 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004064 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004065 * @param[in,out] deviations Deviations to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004066 * @return LY_ERR values.
4067 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004068LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004069parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004070{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004071 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004072 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004073 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004074 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004075 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004076 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004077
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004078 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004079
4080 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004081 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004082 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004083 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004084
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004085 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004086 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004087 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004088 LY_CHECK_GOTO(ret = parse_text_field(ctx, dev->dsc, LY_STMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004090 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004091 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004093 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004094 LY_CHECK_GOTO(ret = parse_text_field(ctx, dev->ref, LY_STMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004096 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004097 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, dev, LY_STMT_DEVIATION, 0, &dev->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 break;
4099 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004100 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004101 ret = LY_EVALID;
4102 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004103 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004104 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004105 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004106
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107 /* mandatory substatements */
4108 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004109 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004110 ret = LY_EVALID;
4111 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004112 }
4113
Michal Vasko12ef5362022-09-16 15:13:58 +02004114cleanup:
4115 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004116 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004117 LY_ARRAY_DECREMENT_FREE(*deviations);
4118 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004119 return ret;
4120}
4121
Michal Vaskoea5abea2018-09-18 13:10:54 +02004122/**
4123 * @brief Parse the feature statement.
4124 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004125 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004126 * @param[in,out] features Features to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004127 * @return LY_ERR values.
4128 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004129LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004130parse_feature(struct lysp_yang_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004132 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004133 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004134 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004135 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004136 struct lysp_feature *feat;
4137
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004138 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139
4140 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004141 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004142 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004143
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004144 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004145 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004146 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004147 LY_CHECK_RET(parse_text_field(ctx, feat->dsc, LY_STMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004149 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004150 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004152 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004153 LY_CHECK_RET(parse_text_field(ctx, feat->ref, LY_STMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004155 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004156 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004158 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004159 LY_CHECK_RET(parse_ext(ctx, word, word_len, feat, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004160 break;
4161 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004162 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004163 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004164 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004165 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004167
4168cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169 return ret;
4170}
4171
Michal Vaskoea5abea2018-09-18 13:10:54 +02004172/**
4173 * @brief Parse the identity statement.
4174 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004175 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004176 * @param[in,out] identities Identities to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004177 * @return LY_ERR values.
4178 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004179LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004180parse_identity(struct lysp_yang_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004182 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004183 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004184 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004186 struct lysp_ident *ident;
4187
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004188 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004189
4190 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004191 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004192 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004193
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004194 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004195 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004196 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004197 LY_CHECK_RET(parse_text_field(ctx, ident->dsc, LY_STMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004199 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004200 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004201 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004203 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004204 LY_CHECK_RET(parse_text_field(ctx, ident->ref, LY_STMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004206 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004207 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004209 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004210 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004211 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 +01004212 return LY_EVALID;
4213 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004214 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 +02004215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004216 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004217 LY_CHECK_RET(parse_ext(ctx, word, word_len, ident, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004218 break;
4219 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004220 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 return LY_EVALID;
4222 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004223 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004225
4226cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 return ret;
4228}
4229
Michal Vaskoea5abea2018-09-18 13:10:54 +02004230/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004231 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004232 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004233 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004234 * @param[in,out] mod Module to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004235 * @return LY_ERR values.
4236 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004237LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004238parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004239{
4240 LY_ERR ret = 0;
4241 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004242 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004243 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004245 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004246
Michal Vaskoc3781c32020-10-06 14:04:08 +02004247 mod->is_submod = 0;
4248
4249 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004250 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004251 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004252
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004253 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004254
Radek Krejcie3846472018-10-15 15:24:51 +02004255#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004256 if (mod_stmt > SECTION) {\
4257 LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;\
4258 } mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004259
Michal Vasko7fbc8162018-09-17 10:35:16 +02004260 switch (kw) {
4261 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004262 case LY_STMT_NAMESPACE:
4263 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004264 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004266 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004267 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004268 break;
4269 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004270 case LY_STMT_INCLUDE:
4271 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004272 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004273 break;
4274 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004275 case LY_STMT_ORGANIZATION:
4276 case LY_STMT_CONTACT:
4277 case LY_STMT_DESCRIPTION:
4278 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004279 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004280 break;
4281
4282 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004283 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004284 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004285 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004286 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004287 case LY_STMT_ANYDATA:
4288 case LY_STMT_ANYXML:
4289 case LY_STMT_AUGMENT:
4290 case LY_STMT_CHOICE:
4291 case LY_STMT_CONTAINER:
4292 case LY_STMT_DEVIATION:
4293 case LY_STMT_EXTENSION:
4294 case LY_STMT_FEATURE:
4295 case LY_STMT_GROUPING:
4296 case LY_STMT_IDENTITY:
4297 case LY_STMT_LEAF:
4298 case LY_STMT_LEAF_LIST:
4299 case LY_STMT_LIST:
4300 case LY_STMT_NOTIFICATION:
4301 case LY_STMT_RPC:
4302 case LY_STMT_TYPEDEF:
4303 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004304 mod_stmt = Y_MOD_BODY;
4305 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004306 case LY_STMT_EXTENSION_INSTANCE:
4307 /* no place in the statement order defined */
4308 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004309 default:
4310 /* error handled in the next switch */
4311 break;
4312 }
Radek Krejcie3846472018-10-15 15:24:51 +02004313#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004314
Radek Krejcie3846472018-10-15 15:24:51 +02004315 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004316 switch (kw) {
4317 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004318 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004319 LY_CHECK_RET(parse_yangversion(ctx, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004321 case LY_STMT_NAMESPACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004322 LY_CHECK_RET(parse_text_field(ctx, mod, LY_STMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004324 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02004325 LY_CHECK_RET(parse_text_field(ctx, mod->mod->prefix, LY_STMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004326 break;
4327
4328 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004329 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004330 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004331 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004332 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004333 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004334 break;
4335
4336 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004337 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004338 LY_CHECK_RET(parse_text_field(ctx, mod, LY_STMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004339 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004340 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004341 LY_CHECK_RET(parse_text_field(ctx, mod, LY_STMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004342 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004343 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004344 LY_CHECK_RET(parse_text_field(ctx, mod->mod->dsc, LY_STMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004345 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004346 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004347 LY_CHECK_RET(parse_text_field(ctx, mod->mod->ref, LY_STMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004348 break;
4349
4350 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004351 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004352 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004353 break;
4354
4355 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004356 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004357 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004358 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004359 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004360 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004362 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004363 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004365 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004366 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004367 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004368 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004369 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004371 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004372 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004374 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004375 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004377 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004378 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004379 break;
4380
Radek Krejcid6b76452019-09-03 17:03:03 +02004381 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004382 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004385 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004388 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004390 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004391 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004393 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004394 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004396 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004397 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004399 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004400 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004402 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004403 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004405 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004406 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004407 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004408 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004409 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004410 break;
4411
4412 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004413 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004414 return LY_EVALID;
4415 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004416 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004417 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004418
Michal Vasko7fbc8162018-09-17 10:35:16 +02004419 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004420 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004421 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004422 return LY_EVALID;
4423 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004424 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004425 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004426 }
4427
Radek Krejcie9e987e2018-10-31 12:50:27 +01004428 /* submodules share the namespace with the module names, so there must not be
4429 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004430 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004431 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004432 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004433 return LY_EVALID;
4434 }
4435
Michal Vasko12ef5362022-09-16 15:13:58 +02004436cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004437 return ret;
4438}
4439
4440/**
4441 * @brief Parse submodule substatements.
4442 *
4443 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004444 * @param[out] submod Parsed submodule structure.
4445 *
4446 * @return LY_ERR values.
4447 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004448LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004449parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004450{
4451 LY_ERR ret = 0;
4452 char *buf, *word;
4453 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004454 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004455 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004456 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004457
Michal Vaskoc3781c32020-10-06 14:04:08 +02004458 submod->is_submod = 1;
4459
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004460 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004461 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004462 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004463
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004464 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004465
4466#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004467 if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004468
4469 switch (kw) {
4470 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004471 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004472 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4473 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004474 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004475 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4476 break;
4477 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004478 case LY_STMT_INCLUDE:
4479 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004480 CHECK_ORDER(Y_MOD_LINKAGE);
4481 break;
4482 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004483 case LY_STMT_ORGANIZATION:
4484 case LY_STMT_CONTACT:
4485 case LY_STMT_DESCRIPTION:
4486 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004487 CHECK_ORDER(Y_MOD_META);
4488 break;
4489
4490 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004491 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004492 CHECK_ORDER(Y_MOD_REVISION);
4493 break;
4494 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004495 case LY_STMT_ANYDATA:
4496 case LY_STMT_ANYXML:
4497 case LY_STMT_AUGMENT:
4498 case LY_STMT_CHOICE:
4499 case LY_STMT_CONTAINER:
4500 case LY_STMT_DEVIATION:
4501 case LY_STMT_EXTENSION:
4502 case LY_STMT_FEATURE:
4503 case LY_STMT_GROUPING:
4504 case LY_STMT_IDENTITY:
4505 case LY_STMT_LEAF:
4506 case LY_STMT_LEAF_LIST:
4507 case LY_STMT_LIST:
4508 case LY_STMT_NOTIFICATION:
4509 case LY_STMT_RPC:
4510 case LY_STMT_TYPEDEF:
4511 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004512 mod_stmt = Y_MOD_BODY;
4513 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004514 case LY_STMT_EXTENSION_INSTANCE:
4515 /* no place in the statement order defined */
4516 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004517 default:
4518 /* error handled in the next switch */
4519 break;
4520 }
4521#undef CHECK_ORDER
4522
4523 prev_kw = kw;
4524 switch (kw) {
4525 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004526 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004527 LY_CHECK_RET(parse_yangversion(ctx, (struct lysp_module *)submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004529 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02004530 LY_CHECK_RET(parse_belongsto(ctx, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004531 break;
4532
4533 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004534 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004535 if (submod->version == LYS_VERSION_1_1) {
4536 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4537 submod->name);
4538 }
Radek Krejci33090f92020-12-17 20:12:46 +01004539 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004541 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004542 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004543 break;
4544
4545 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004546 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004547 LY_CHECK_RET(parse_text_field(ctx, submod, LY_STMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004548 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004549 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004550 LY_CHECK_RET(parse_text_field(ctx, submod, LY_STMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004551 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004552 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004553 LY_CHECK_RET(parse_text_field(ctx, submod->dsc, LY_STMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004554 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004555 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004556 LY_CHECK_RET(parse_text_field(ctx, submod->ref, LY_STMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004557 break;
4558
4559 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004560 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004561 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004562 break;
4563
4564 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004565 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004566 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004567 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004568 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004569 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004570 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004571 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004572 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004573 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004574 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004575 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004577 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004578 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004579 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004580 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004581 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004582 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004583 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004584 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004585 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004586 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004587 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004588 break;
4589
Radek Krejcid6b76452019-09-03 17:03:03 +02004590 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004591 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004593 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004594 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004595 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004596 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004597 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004598 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004599 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004600 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004601 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004602 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004603 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004604 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004605 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004606 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004607 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004608 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004609 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004610 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004611 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004612 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004614 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004615 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004617 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004618 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004619 break;
4620
4621 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004622 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004623 return LY_EVALID;
4624 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004625 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004626 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004627
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004628 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004629 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004630 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004631 return LY_EVALID;
4632 }
4633
4634 /* submodules share the namespace with the module names, so there must not be
4635 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004636 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004637 /* main modules may have different revisions */
4638 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004639 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004640 return LY_EVALID;
4641 }
4642
Michal Vasko12ef5362022-09-16 15:13:58 +02004643cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004644 return ret;
4645}
4646
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004647/**
4648 * @brief Skip any redundant characters, namely whitespaces and comments.
4649 *
4650 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004651 * @return LY_SUCCESS on success.
4652 * @return LY_EVALID on invalid comment.
4653 */
4654static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004655skip_redundant_chars(struct lysp_yang_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004656{
4657 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004658 while (ctx->in->current[0]) {
4659 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004660 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004661 ly_in_skip(ctx->in, 2);
4662 LY_CHECK_RET(skip_comment(ctx, 1));
4663 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004664 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004665 ly_in_skip(ctx->in, 2);
4666 LY_CHECK_RET(skip_comment(ctx, 2));
4667 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004668 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004669 if (ctx->in->current[0] == '\n') {
4670 LY_IN_NEW_LINE(ctx->in);
4671 }
Radek Krejci33090f92020-12-17 20:12:46 +01004672 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004673 } else {
4674 break;
4675 }
4676 }
4677
4678 return LY_SUCCESS;
4679}
4680
Radek Krejcid4557c62018-09-17 11:42:09 +02004681LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004682yang_parse_submodule(struct lysp_yang_ctx **context, struct ly_ctx *ly_ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004683 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004684{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004685 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004686 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004687 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004688 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004689 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004690 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004691
aPiecek56be92a2021-07-01 07:18:10 +02004692 assert(context && ly_ctx && main_ctx && in && submod);
4693
David Sedlák1b623122019-08-05 15:27:49 +02004694 /* create context */
4695 *context = calloc(1, sizeof **context);
4696 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004697 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004698 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004699 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004700
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004701 mod_p = calloc(1, sizeof *mod_p);
4702 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004703 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004704 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004705
4706 /* use main context parsed mods adding the current one */
4707 (*context)->parsed_mods = main_ctx->parsed_mods;
4708 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004709
Michal Vaskof8ebf132022-11-21 14:06:48 +01004710 LOG_LOCSET(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004711
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004712 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004713 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004714 LY_CHECK_GOTO(ret, cleanup);
4715
Michal Vasko7fbc8162018-09-17 10:35:16 +02004716 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004717 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004718 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004719
Radek Krejcid6b76452019-09-03 17:03:03 +02004720 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004721 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004722 ret = LY_EINVAL;
4723 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004724 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004725 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004726 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004727 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004728 }
4729
Michal Vasko7fbc8162018-09-17 10:35:16 +02004730 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004731 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004732 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004733
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004734 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004735 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004736 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004737 if (in->current[0]) {
4738 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004739 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004740 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004741 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004742
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004743 mod_p->parsing = 0;
4744 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004745
Radek Krejcibbe09a92018-11-08 09:36:54 +01004746cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004747 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004748 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004749 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004750 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004751 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004752 }
4753
4754 return ret;
4755}
4756
4757LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004758yang_parse_module(struct lysp_yang_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004759{
4760 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004761 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004762 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004763 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004764 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004765 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004766
David Sedlák1b623122019-08-05 15:27:49 +02004767 /* create context */
4768 *context = calloc(1, sizeof **context);
4769 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004770 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004771 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004772 (*context)->in = in;
Michal Vaskod0625d72022-10-06 15:02:50 +02004773 (*context)->main_ctx = (struct lysp_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004774
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004775 mod_p = calloc(1, sizeof *mod_p);
4776 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4777 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004778 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004779
Michal Vaskof8ebf132022-11-21 14:06:48 +01004780 LOG_LOCSET(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004781
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004782 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004783 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004784 LY_CHECK_GOTO(ret, cleanup);
4785
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004786 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004787 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004788 LY_CHECK_GOTO(ret, cleanup);
4789
Radek Krejcid6b76452019-09-03 17:03:03 +02004790 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004791 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 +01004792 ret = LY_EINVAL;
4793 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004794 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004795 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004796 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004797 goto cleanup;
4798 }
4799
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004800 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004801 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004802 LY_CHECK_GOTO(ret, cleanup);
4803
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004804 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004805 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004806 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004807 if (in->current[0]) {
4808 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004809 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004810 goto cleanup;
4811 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004812
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004813 mod->parsed = mod_p;
4814
4815cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004816 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004817 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004818 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004819 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004820 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004821 }
4822
Michal Vasko7fbc8162018-09-17 10:35:16 +02004823 return ret;
4824}