blob: dd84480a8544228753d43385b55e2a9b8559ff35 [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':
419 if (ctx->in->current[1] != '\n') {
420 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
421 return LY_EVALID;
422 }
423 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200424 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200425 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200426 /* we will be removing the indents so we need our own buffer */
427 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200428
429 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200430 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200431
Radek Krejciff13cd12019-10-25 15:34:24 +0200432 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200433 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200434 }
435
436 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100437 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 +0200438
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200439 /* reset context indentation counter for possible string after this one */
440 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200441 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200442 break;
443 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200444 /* first non-whitespace character, stop eating indentation */
445 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200446
447 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100448 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 +0200449 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200450 break;
451 }
452 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100453 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100455 c = ctx->in->current;
456 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200457 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100458 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100459 /* fix false newline count in buf_store_char() */
460 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200461 break;
462 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100463 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200464 break;
465 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200466 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200467 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200468 break;
469 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100471 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200472 return LY_EVALID;
473 }
474
475 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100476 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 +0200477
Radek Krejcif13b87b2020-12-01 22:02:17 +0100478 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100479 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100481 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100482 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200483 case '+':
484 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100485 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200486 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200488 case '\r':
489 if (ctx->in->current[1] != '\n') {
490 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
491 return LY_EVALID;
492 }
493 MOVE_INPUT(ctx, 1);
494 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200495 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100496 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100497 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200498 case ' ':
499 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200500 /* just skip */
501 break;
502 default:
503 /* string is finished */
504 goto string_end;
505 }
Radek Krejci33090f92020-12-17 20:12:46 +0100506 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200507 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100508 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100509 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200510 case '\r':
511 if (ctx->in->current[1] != '\n') {
512 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
513 return LY_EVALID;
514 }
515 MOVE_INPUT(ctx, 1);
516 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200517 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100518 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100519 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200520 case ' ':
521 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200522 /* skip */
523 break;
524 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100525 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200526 break;
527 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100528 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200529 break;
530 default:
531 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200532 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200533 return LY_EVALID;
534 }
Radek Krejci33090f92020-12-17 20:12:46 +0100535 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200536 break;
537 default:
538 return LY_EINT;
539 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200540 }
541
542string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200543 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200544 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200545 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200546 return LY_EVALID;
547 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200548 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100549
550#undef STRING_ENDED
551#undef STRING_SINGLE_QUOTED
552#undef STRING_DOUBLE_QUOTED
553#undef STRING_DOUBLE_QUOTED_ESCAPED
554#undef STRING_PAUSED_NEXTSTRING
555#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200556}
557
Michal Vaskoea5abea2018-09-18 13:10:54 +0200558/**
559 * @brief Get another YANG string from the raw data.
560 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200561 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200562 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200563 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
564 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200565 * @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 +0200566 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
567 * set to NULL. Otherwise equal to \p word_p.
568 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200569 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200570 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200571LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200572get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200573 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200574{
Michal Vaskob68ea142021-04-26 13:46:00 +0200575 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200576 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200577 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200578
Michal Vasko7fbc8162018-09-17 10:35:16 +0200579 /* word buffer - dynamically allocated */
580 *word_b = NULL;
581
582 /* word pointer - just a pointer to data */
583 *word_p = NULL;
584
585 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100586 while (ctx->in->current[0]) {
587 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200588 case '\'':
589 case '\"':
590 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200591 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100592 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200593 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200594 ret = LY_EVALID;
595 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200596 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200597 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100598 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200599 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200600 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200601 if (!*word_p) {
602 /* do not return NULL word */
603 *word_p = "";
604 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200605 goto str_end;
606 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100607 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200608 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100609 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200610 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100611 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200612 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100613 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200614 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200615 } else {
616 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200617 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 +0200618 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200619 break;
620 case ' ':
621 if (*word_len) {
622 /* word is finished */
623 goto str_end;
624 }
Radek Krejci33090f92020-12-17 20:12:46 +0100625 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200626 break;
627 case '\t':
628 if (*word_len) {
629 /* word is finished */
630 goto str_end;
631 }
632 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100633 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200634
Radek Krejci33090f92020-12-17 20:12:46 +0100635 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200636 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200637 case '\r':
638 if (ctx->in->current[1] != '\n') {
639 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200640 ret = LY_EVALID;
641 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200642 }
643 MOVE_INPUT(ctx, 1);
644 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200645 case '\n':
646 if (*word_len) {
647 /* word is finished */
648 goto str_end;
649 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100650 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100651 MOVE_INPUT(ctx, 1);
652
Michal Vasko7fbc8162018-09-17 10:35:16 +0200653 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200654 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655 break;
656 case ';':
657 case '{':
658 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
659 /* word is finished */
660 goto str_end;
661 }
662
Radek Krejci33090f92020-12-17 20:12:46 +0100663 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200664 ret = LY_EVALID;
665 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200666 case '}':
667 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100668 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200669 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200670 ret = LY_EVALID;
671 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200673 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 +0200674 break;
675 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200676 }
677
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200678 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200679 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200680 ret = LY_EVALID;
681 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200682
Michal Vasko7fbc8162018-09-17 10:35:16 +0200683str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200684 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200685 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200686 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200687 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200688 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689 *word_p = *word_b;
690 }
691
692 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200693
694error:
695 free(*word_b);
696 *word_b = NULL;
697 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200698}
699
Michal Vaskoea5abea2018-09-18 13:10:54 +0200700/**
701 * @brief Get another YANG keyword from the raw data.
702 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200703 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200704 * @param[out] kw YANG keyword read.
705 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
706 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200707 * @return LY_ERR values.
708 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200709LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200710get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200711{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200712 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200713 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200714 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715
716 if (word_p) {
717 *word_p = NULL;
718 *word_len = 0;
719 }
720
721 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100722 while (ctx->in->current[0]) {
723 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200724 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100725 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200726 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100727 MOVE_INPUT(ctx, 2);
728 LY_CHECK_RET(skip_comment(ctx, 1));
729 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100731 MOVE_INPUT(ctx, 2);
732 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200733 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200734 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200735 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200736 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737 }
Radek Krejci13028282019-06-11 14:56:48 +0200738 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200739 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200740 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100741 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200742 ctx->indent = 0;
743 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200744 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200745 /* skip whitespaces (optsep) */
746 ++ctx->indent;
747 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200749 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100750 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200751 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100752 case '\r':
753 /* possible CRLF endline */
754 if (ctx->in->current[1] == '\n') {
755 break;
756 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100757 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200758 default:
759 /* either a keyword start or an invalid character */
760 goto keyword_start;
761 }
762
Radek Krejci33090f92020-12-17 20:12:46 +0100763 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200764 }
765
766keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100767 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100768 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200769
aPiecek93582ed2021-05-25 14:49:06 +0200770 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
771 goto success;
772 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
773 ctx->depth++;
774 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100775 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200776 return LY_EINVAL;
777 }
778 goto success;
779 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
780 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200781 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200782 }
783
Radek Krejcid6b76452019-09-03 17:03:03 +0200784 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200785 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100786 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200787 case '\r':
788 if (ctx->in->current[1] != '\n') {
789 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
790 return LY_EVALID;
791 }
792 MOVE_INPUT(ctx, 1);
793 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200794 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200795 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200796 case ' ':
797 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200798 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200799 case ':':
800 /* keyword is not actually a keyword, but prefix of an extension.
801 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
802 * and we will be checking the keyword (extension instance) itself */
803 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100804 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200805 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200806 case '{':
807 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200808 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200809 break;
810 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100811 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200812 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100813 MOVE_INPUT(ctx, 1);
814 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200815 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200816 return LY_EVALID;
817 }
818 } else {
819 /* still can be an extension */
820 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200821
Radek Krejci156ccaf2018-10-15 15:49:17 +0200822extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100823 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200824 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
825 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200826 uint32_t c = 0;
827
Radek Krejci33090f92020-12-17 20:12:46 +0100828 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
829 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200830 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200831 /* check character validity */
Michal Vaskod0625d72022-10-06 15:02:50 +0200832 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100833 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200834 }
Radek Krejci33090f92020-12-17 20:12:46 +0100835 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200836 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837 return LY_EVALID;
838 }
839
840 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200841 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100842 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843 return LY_EVALID;
844 }
845
Radek Krejcid6b76452019-09-03 17:03:03 +0200846 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200847 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200848
Radek Krejci626df482018-10-11 15:06:31 +0200849success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200850 if (word_p) {
851 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100852 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200853 }
854
855 return LY_SUCCESS;
856}
857
Michal Vaskoea5abea2018-09-18 13:10:54 +0200858/**
859 * @brief Parse extension instance substatements.
860 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200861 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200862 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200863 * @param[in] word Extension instance substatement name (keyword).
864 * @param[in] word_len Extension instance substatement name length.
865 * @param[in,out] child Children of this extension instance to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200866 * @return LY_ERR values.
867 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200868static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200869parse_ext_substmt(struct lysp_yang_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200870 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200871{
872 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100873 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200874 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200875 struct lysp_stmt *stmt, *par_child;
876
877 stmt = calloc(1, sizeof *stmt);
878 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
879
Radek Krejcibb9b1982019-04-08 14:24:59 +0200880 /* insert into parent statements */
881 if (!*child) {
882 *child = stmt;
883 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200884 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200885 par_child->next = stmt;
886 }
887
Michal Vaskofc2cd072021-02-24 13:17:17 +0100888 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200889 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200890
891 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100892 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200893 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200894 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895 }
896
Radek Krejci8df109d2021-04-23 12:19:08 +0200897 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100898 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100899 stmt->kw = kw;
900
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200901 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
902 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
903 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200904 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200905
906cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200907 return ret;
908}
909
Michal Vaskoea5abea2018-09-18 13:10:54 +0200910/**
911 * @brief Parse extension instance.
912 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200913 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200914 * @param[in] ext_name Extension instance substatement name (keyword).
915 * @param[in] ext_name_len Extension instance substatement name length.
Michal Vasko193dacd2022-10-13 08:43:05 +0200916 * @param[in] parent Current statement parent.
917 * @param[in] parent_stmt Type of @p parent statement.
918 * @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 +0200919 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200920 * @return LY_ERR values.
921 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200922static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200923parse_ext(struct lysp_yang_ctx *ctx, const char *ext_name, size_t ext_name_len, const void *parent,
924 enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200925{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100926 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200928 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200929 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200930 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200932 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933
Michal Vaskofc2cd072021-02-24 13:17:17 +0100934 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
935 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
936 return LY_EVALID;
937 }
938
939 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200940 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200941
942 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100943 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200944 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200945 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200946 }
947
Michal Vaskofc2cd072021-02-24 13:17:17 +0100948 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200949 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200950 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100951 e->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200952 e->parent = (void *)parent;
953 e->parent_stmt = parent_stmt;
954 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100955
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200956 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
957 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
958 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200959 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200960
961cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200962 return ret;
963}
964
Michal Vaskoea5abea2018-09-18 13:10:54 +0200965/**
966 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
967 * description, etc...
968 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200969 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +0200970 * @param[in] parent Current statement parent.
971 * @param[in] parent_stmt Type of statement in @p value.
972 * @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 +0200973 * @param[in,out] value Place to store the parsed value.
974 * @param[in] arg Type of the YANG keyword argument (of the value).
975 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200976 * @return LY_ERR values.
977 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200978static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200979parse_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 +0200980 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100982 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200983 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200984 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200985 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200986
987 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200988 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989 return LY_EVALID;
990 }
991
992 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100993 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200994
995 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +0200996 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200997
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200998 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200999 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001000 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001001 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent, parent_stmt, parent_stmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001002 break;
1003 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001004 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001005 return LY_EVALID;
1006 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001007 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001009
1010cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011 return ret;
1012}
1013
Michal Vaskoea5abea2018-09-18 13:10:54 +02001014/**
1015 * @brief Parse the yang-version statement.
1016 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001017 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001018 * @param[in,out] mod Module to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001019 * @return LY_ERR values.
1020 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001021static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001022parse_yangversion(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001024 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001026 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001027 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001028
Michal Vasko193dacd2022-10-13 08:43:05 +02001029 if (mod->version) {
David Sedlákb3077192019-06-19 10:55:37 +02001030 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031 return LY_EVALID;
1032 }
1033
1034 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001035 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036
Radek Krejci96e48da2020-09-04 13:18:06 +02001037 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001038 mod->version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001039 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001040 mod->version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001041 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001042 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001043 free(buf);
1044 return LY_EVALID;
1045 }
1046 free(buf);
1047
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001048 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001049 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001050 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001051 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_YANG_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001052 break;
1053 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001054 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055 return LY_EVALID;
1056 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001057 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001059
1060cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 return ret;
1062}
1063
Michal Vaskoea5abea2018-09-18 13:10:54 +02001064/**
1065 * @brief Parse the belongs-to statement.
1066 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001067 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001068 * @param[in,out] submod Submodule to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001069 * @return LY_ERR values.
1070 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001072parse_belongsto(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001074 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001076 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001077 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078
Michal Vasko193dacd2022-10-13 08:43:05 +02001079 if (submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001080 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081 return LY_EVALID;
1082 }
1083
Michal Vaskoc3781c32020-10-06 14:04:08 +02001084 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001085 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001086 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001087 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 +01001088 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001089 free(buf);
1090 return LY_EVALID;
1091 }
1092 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001093
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001094 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001095 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001096 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001097 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 +02001098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001099 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001100 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_BELONGS_TO, 0, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001101 break;
1102 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001103 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104 return LY_EVALID;
1105 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001106 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001108
Michal Vasko7fbc8162018-09-17 10:35:16 +02001109 /* mandatory substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +02001110 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001111 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112 return LY_EVALID;
1113 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001114
1115cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116 return ret;
1117}
1118
Michal Vaskoea5abea2018-09-18 13:10:54 +02001119/**
1120 * @brief Parse the revision-date statement.
1121 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001122 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001123 * @param[in,out] rev Buffer to store the parsed value in.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001124 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001125 * @return LY_ERR values.
1126 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001128parse_revisiondate(struct lysp_yang_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001129{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001130 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001131 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001132 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001133 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001134
1135 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001136 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137 return LY_EVALID;
1138 }
1139
1140 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001141 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142
1143 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001144 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145 free(buf);
1146 return LY_EVALID;
1147 }
1148
1149 /* store value and spend buf if allocated */
1150 strncpy(rev, word, word_len);
1151 free(buf);
1152
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001153 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001154 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001155 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001156 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157 break;
1158 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001159 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160 return LY_EVALID;
1161 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001162 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001163 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001164
1165cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001166 return ret;
1167}
1168
Michal Vaskoea5abea2018-09-18 13:10:54 +02001169/**
1170 * @brief Parse the include statement.
1171 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001172 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001173 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001174 * @param[in,out] includes Parsed includes to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001175 * @return LY_ERR values.
1176 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001177static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001178parse_include(struct lysp_yang_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001179{
Radek Krejcid33273d2018-10-25 14:55:52 +02001180 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001182 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001183 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184 struct lysp_include *inc;
1185
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001186 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001187
1188 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001189 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190
Michal Vasko12ef5362022-09-16 15:13:58 +02001191 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001192
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001193 /* submodules share the namespace with the module names, so there must not be
1194 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001195 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001196 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001197 return LY_EVALID;
1198 }
1199
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001200 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001201 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001202 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001203 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001204 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 +02001205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001206 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001207 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001208 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 +02001209 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001210 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001211 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001213 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001214 LY_CHECK_RET(parse_ext(ctx, word, word_len, inc, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 break;
1216 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001217 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218 return LY_EVALID;
1219 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001220 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001222
1223cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001224 return ret;
1225}
1226
Michal Vaskoea5abea2018-09-18 13:10:54 +02001227/**
1228 * @brief Parse the import statement.
1229 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001230 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001231 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001232 * @param[in,out] imports Parsed imports to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001233 * @return LY_ERR values.
1234 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001235static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001236parse_import(struct lysp_yang_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001238 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001240 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001241 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242 struct lysp_import *imp;
1243
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001244 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245
1246 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001247 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001248 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001250 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001252 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001253 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 +02001254 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001256 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001257 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001258 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 +02001259 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001260 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001261 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001262 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 +02001263 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001264 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001265 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001267 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001268 LY_CHECK_RET(parse_ext(ctx, word, word_len, imp, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001269 break;
1270 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001271 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001272 return LY_EVALID;
1273 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001274 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001275 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001276
Michal Vasko7fbc8162018-09-17 10:35:16 +02001277 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001278 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001279
Michal Vasko12ef5362022-09-16 15:13:58 +02001280cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001281 return ret;
1282}
1283
Michal Vaskoea5abea2018-09-18 13:10:54 +02001284/**
1285 * @brief Parse the revision statement.
1286 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001287 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001288 * @param[in,out] revs Parsed revisions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001289 * @return LY_ERR values.
1290 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001291static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001292parse_revision(struct lysp_yang_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001293{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001294 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001295 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001296 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001297 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001298 struct lysp_revision *rev;
1299
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001300 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001301
1302 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001303 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001304
1305 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001306 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001307 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001308 return LY_EVALID;
1309 }
1310
Radek Krejcib7db73a2018-10-24 14:18:40 +02001311 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001312 free(buf);
1313
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001314 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001316 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001317 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 +02001318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001319 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001320 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 +02001321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001322 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001323 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324 break;
1325 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001326 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327 return LY_EVALID;
1328 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001329 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001331
1332cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001333 return ret;
1334}
1335
Michal Vaskoea5abea2018-09-18 13:10:54 +02001336/**
1337 * @brief Parse a generic text field that can have more instances such as base.
1338 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001339 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001340 * @param[in] parent Current statement parent.
1341 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001342 * @param[in,out] texts Parsed values to add to.
1343 * @param[in] arg Type of the expected argument.
1344 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001345 * @return LY_ERR values.
1346 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001347static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001348parse_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 +02001349 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001350{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001351 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001352 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001353 const char **item;
1354 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001355 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001356
1357 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001358 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001359
1360 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001361 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362
Michal Vasko12ef5362022-09-16 15:13:58 +02001363 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001364 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001365 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001366 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001367 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 +02001368 break;
1369 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001370 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371 return LY_EVALID;
1372 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001373 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001375
1376cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001377 return ret;
1378}
1379
Michal Vaskoea5abea2018-09-18 13:10:54 +02001380/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001381 * @brief Parse a generic text field that can have more instances such as base.
1382 *
1383 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001384 * @param[in] parent_stmt Type of statement stored in @p qnames.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001385 * @param[in,out] qnames Parsed qnames to add to.
1386 * @param[in] arg Type of the expected argument.
1387 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001388 * @return LY_ERR values.
1389 */
1390static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001391parse_qnames(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_qname **qnames, enum yang_arg arg,
1392 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +02001393{
1394 LY_ERR ret = LY_SUCCESS;
1395 char *buf, *word;
1396 struct lysp_qname *item;
1397 size_t word_len;
1398 enum ly_stmt kw;
1399
1400 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001401 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001402
1403 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001404 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001405
Michal Vasko12ef5362022-09-16 15:13:58 +02001406 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001407 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001408 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001409 switch (kw) {
1410 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001411 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 +02001412 break;
1413 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001414 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001415 return LY_EVALID;
1416 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001417 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001418 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001419
1420cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001421 return ret;
1422}
1423
1424/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001425 * @brief Parse the config statement.
1426 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001427 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001428 * @param[in,out] flags Flags to add to.
1429 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001430 * @return LY_ERR values.
1431 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001432static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001433parse_config(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001435 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001436 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001437 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001438 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001439
1440 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001441 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001442 return LY_EVALID;
1443 }
1444
1445 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001446 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001447
Radek Krejcif13b87b2020-12-01 22:02:17 +01001448 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001450 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001451 *flags |= LYS_CONFIG_R;
1452 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001453 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001454 free(buf);
1455 return LY_EVALID;
1456 }
1457 free(buf);
1458
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001459 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001460 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001461 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001462 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463 break;
1464 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001465 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466 return LY_EVALID;
1467 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001468 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001469 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001470
1471cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472 return ret;
1473}
1474
Michal Vaskoea5abea2018-09-18 13:10:54 +02001475/**
1476 * @brief Parse the mandatory statement.
1477 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001478 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001479 * @param[in,out] flags Flags to add to.
1480 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001481 * @return LY_ERR values.
1482 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001484parse_mandatory(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001485{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001486 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001488 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001489 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001490
1491 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001492 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493 return LY_EVALID;
1494 }
1495
1496 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001497 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498
Radek Krejcif13b87b2020-12-01 22:02:17 +01001499 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001501 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001502 *flags |= LYS_MAND_FALSE;
1503 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001504 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505 free(buf);
1506 return LY_EVALID;
1507 }
1508 free(buf);
1509
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001510 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001511 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001512 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001513 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 break;
1515 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001516 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 return LY_EVALID;
1518 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001519 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001521
1522cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001523 return ret;
1524}
1525
Michal Vaskoea5abea2018-09-18 13:10:54 +02001526/**
1527 * @brief Parse a restriction such as range or length.
1528 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001529 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001530 * @param[in] restr_kw Type of this particular restriction.
1531 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001532 * @return LY_ERR values.
1533 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001534static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001535parse_restr(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001537 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001539 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001540 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001541
1542 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001543 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544
Michal Vasko193dacd2022-10-13 08:43:05 +02001545 CHECK_NONEMPTY(ctx, word_len, lyplg_ext_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001546 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001547 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001548 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001549 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001550 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001551 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 +02001552 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001553 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001554 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 +02001555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001556 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02001557 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG,
1558 &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001560 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001561 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 +02001562 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001563 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001564 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 break;
1566 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001567 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 return LY_EVALID;
1569 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001570 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001572
1573cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 return ret;
1575}
1576
Michal Vaskoea5abea2018-09-18 13:10:54 +02001577/**
1578 * @brief Parse a restriction that can have more instances such as must.
1579 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001580 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001581 * @param[in] restr_kw Type of this particular restriction.
1582 * @param[in,out] restrs Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001583 * @return LY_ERR values.
1584 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001585static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001586parse_restrs(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001587{
1588 struct lysp_restr *restr;
1589
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001590 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001591 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001592}
1593
Michal Vaskoea5abea2018-09-18 13:10:54 +02001594/**
1595 * @brief Parse the status statement.
1596 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001597 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001598 * @param[in,out] flags Flags to add to.
1599 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001600 * @return LY_ERR values.
1601 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001602static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001603parse_status(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001605 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001607 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001608 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001609
1610 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001611 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612 return LY_EVALID;
1613 }
1614
1615 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001616 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001617
Radek Krejcif13b87b2020-12-01 22:02:17 +01001618 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001619 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001620 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001622 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001623 *flags |= LYS_STATUS_OBSLT;
1624 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001625 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001626 free(buf);
1627 return LY_EVALID;
1628 }
1629 free(buf);
1630
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001631 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001632 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001633 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001634 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001635 break;
1636 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001637 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 return LY_EVALID;
1639 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001640 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001642
1643cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 return ret;
1645}
1646
Michal Vaskoea5abea2018-09-18 13:10:54 +02001647/**
1648 * @brief Parse the when statement.
1649 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001650 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001651 * @param[in,out] when_p When pointer to parse to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001652 * @return LY_ERR values.
1653 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001654LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001655parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001656{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001657 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001659 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001660 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001662 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663
1664 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001665 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666 return LY_EVALID;
1667 }
1668
1669 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001670 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671
1672 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001673 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001674 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001675 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001676
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001677 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001678 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001679 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001680 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->dsc, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts),
1681 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001683 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001684 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->ref, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts),
1685 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001687 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001688 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 +02001689 break;
1690 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001691 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001692 ret = LY_EVALID;
1693 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001694 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001695 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001697
1698cleanup:
1699 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001700 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001701 free(when);
1702 } else {
1703 *when_p = when;
1704 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001705 return ret;
1706}
1707
Michal Vaskoea5abea2018-09-18 13:10:54 +02001708/**
1709 * @brief Parse the anydata or anyxml statement.
1710 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001711 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001712 * @param[in] any_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001713 * @param[in] parent Node parent.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001714 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001715 * @return LY_ERR values.
1716 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001717LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001718parse_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 +02001719{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001720 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001721 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001722 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001723 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001724 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001725
David Sedlák60adc092019-08-06 15:57:02 +02001726 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001727 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001728
Radek Krejci39b7fc22021-02-26 23:29:18 +01001729 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001730 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001731
Michal Vasko7fbc8162018-09-17 10:35:16 +02001732 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001733 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001734 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735
1736 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001737 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001739 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001740 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001741 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001742 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001743 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 +02001744 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001745 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001746 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001747 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001748 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001749 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001750 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001751 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001752 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001754 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001755 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 +02001756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001757 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001758 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001760 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001761 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001763 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001764 LY_CHECK_RET(parse_ext(ctx, word, word_len, any, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 break;
1766 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001767 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768 return LY_EVALID;
1769 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001770 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001772
1773cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 return ret;
1775}
1776
Michal Vaskoea5abea2018-09-18 13:10:54 +02001777/**
1778 * @brief Parse the value or position statement. Substatement of type enum statement.
1779 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001780 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001781 * @param[in] val_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001782 * @param[in,out] enm Structure to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001783 * @return LY_ERR values.
1784 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001785LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001786parse_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 +02001787{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001788 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001789 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001790 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001791 long long num = 0;
1792 unsigned long long unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001793 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794
Michal Vasko193dacd2022-10-13 08:43:05 +02001795 if (enm->flags & LYS_SET_VALUE) {
1796 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001797 ret = LY_EVALID;
1798 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001800 enm->flags |= LYS_SET_VALUE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001801
1802 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001803 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001804
Radek Krejcid6b76452019-09-03 17:03:03 +02001805 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 +02001806 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001807 ret = LY_EVALID;
1808 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001809 }
1810
1811 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001812 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001813 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001814 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001815 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001816 ret = LY_EVALID;
1817 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001818 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001820 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001821 if (unum > UINT64_C(4294967295)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001822 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001823 ret = LY_EVALID;
1824 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001825 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001826 }
1827 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001828 if ((size_t)(ptr - word) != word_len) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001829 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001830 ret = LY_EVALID;
1831 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001832 }
1833 if (errno == ERANGE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001834 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001835 ret = LY_EVALID;
1836 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001837 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001838 if (val_kw == LY_STMT_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001839 enm->value = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02001841 enm->value = unum;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001842 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001844 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001845 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001846 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001847 ret = parse_ext(ctx, word, word_len, enm, val_kw, 0, &enm->exts);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001848 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001849 break;
1850 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001851 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001852 ret = LY_EVALID;
1853 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001854 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001855 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001856 }
Radek Krejci8b764662018-11-14 14:15:13 +01001857
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001858cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001859 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001860 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001861}
1862
Michal Vaskoea5abea2018-09-18 13:10:54 +02001863/**
1864 * @brief Parse the enum or bit statement. Substatement of type statement.
1865 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001866 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001867 * @param[in] enum_kw Type of this particular keyword.
1868 * @param[in,out] enums Enums or bits to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001869 * @return LY_ERR values.
1870 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001872parse_type_enum(struct lysp_yang_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001873{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001874 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001875 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001876 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001877 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001878 struct lysp_type_enum *enm;
1879
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001880 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881
1882 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001883 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 +02001884 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001885 ret = lysp_check_enum_name((struct lysp_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001886 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001887 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001888
Michal Vasko12ef5362022-09-16 15:13:58 +02001889 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001890 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(enum_kw), enm->name);
Radek Krejci8b764662018-11-14 14:15:13 +01001891
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001892 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001893 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001894 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001895 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 +02001896 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001897 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001898 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001899 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001900 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001901 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001902 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 +02001903 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001904 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001905 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001907 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001908 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1909 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1910 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
David Sedlák9fb515f2019-07-11 10:33:58 +02001911 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001912 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001913 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1914 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1915 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001916 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001917 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001918 LY_CHECK_RET(parse_ext(ctx, word, word_len, enm, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919 break;
1920 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001921 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922 return LY_EVALID;
1923 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001924 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001926
1927cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928 return ret;
1929}
1930
Michal Vaskoea5abea2018-09-18 13:10:54 +02001931/**
1932 * @brief Parse the fraction-digits statement. Substatement of type statement.
1933 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001934 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001935 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001936 * @return LY_ERR values.
1937 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001938static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001939parse_type_fracdigits(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001940{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001941 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001942 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001943 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001944 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001945 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946
Michal Vasko193dacd2022-10-13 08:43:05 +02001947 if (type->fraction_digits) {
David Sedlákb3077192019-06-19 10:55:37 +02001948 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949 return LY_EVALID;
1950 }
1951
1952 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001953 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001954
1955 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001956 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001957 ret = LY_EVALID;
1958 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001959 }
1960
1961 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001962 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001963 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001964 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001965 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001966 ret = LY_EVALID;
1967 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001968 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001969 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001970 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001971 ret = LY_EVALID;
1972 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001974 type->fraction_digits = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001976 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001978 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001979 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 +02001980 break;
1981 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001982 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001983 ret = LY_EVALID;
1984 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001985 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001986 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001988
1989cleanup:
1990 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991 return ret;
1992}
1993
Michal Vaskoea5abea2018-09-18 13:10:54 +02001994/**
1995 * @brief Parse the require-instance statement. Substatement of type statement.
1996 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001997 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001998 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001999 * @return LY_ERR values.
2000 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002002parse_type_reqinstance(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002004 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002005 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002006 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002007 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008
Michal Vasko193dacd2022-10-13 08:43:05 +02002009 if (type->flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002010 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 return LY_EVALID;
2012 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002013 type->flags |= LYS_SET_REQINST;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014
2015 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002016 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017
Radek Krejcif13b87b2020-12-01 22:02:17 +01002018 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002019 type->require_instance = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002020 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002021 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002022 ret = LY_EVALID;
2023 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002024 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002026 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002028 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002029 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 +02002030 break;
2031 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002032 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002033 ret = LY_EVALID;
2034 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002035 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002036 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002038
2039cleanup:
2040 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041 return ret;
2042}
2043
Michal Vaskoea5abea2018-09-18 13:10:54 +02002044/**
2045 * @brief Parse the modifier statement. Substatement of type pattern statement.
2046 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002047 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002048 * @param[in,out] restr Restriction to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002049 * @return LY_ERR values.
2050 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002051static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002052parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002054 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002055 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002056 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002057 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058
Michal Vasko193dacd2022-10-13 08:43:05 +02002059 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002060 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002061 return LY_EVALID;
2062 }
2063
2064 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002065 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066
Radek Krejcif13b87b2020-12-01 22:02:17 +01002067 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002068 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002069 ret = LY_EVALID;
2070 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002072
2073 /* replace the value in the dictionary */
Michal Vasko193dacd2022-10-13 08:43:05 +02002074 buf = malloc(strlen(restr->arg.str) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002075 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02002076 strcpy(buf, restr->arg.str);
2077 lydict_remove(PARSER_CTX(ctx), restr->arg.str);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078
Radek Krejcif13b87b2020-12-01 22:02:17 +01002079 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2080 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko183ea332022-11-08 10:50:03 +01002081 ret = lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002082 buf = NULL;
Michal Vasko183ea332022-11-08 10:50:03 +01002083 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002084
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002085 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002087 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002088 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 +02002089 break;
2090 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002091 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002092 ret = LY_EVALID;
2093 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002095 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002097
2098cleanup:
2099 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100 return ret;
2101}
2102
Michal Vaskoea5abea2018-09-18 13:10:54 +02002103/**
2104 * @brief Parse the pattern statement. Substatement of type statement.
2105 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002106 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002107 * @param[in,out] patterns Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002108 * @return LY_ERR values.
2109 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002110static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002111parse_type_pattern(struct lysp_yang_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002113 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002115 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002116 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 struct lysp_restr *restr;
2118
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002119 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120
2121 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002122 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123
2124 /* add special meaning first byte */
2125 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002126 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 word = buf;
2128 } else {
2129 buf = malloc(word_len + 2);
2130 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002131 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002132 if (word_len) {
2133 memmove(buf + 1, word, word_len);
2134 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002135 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002136 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002137 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002138 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002140 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002142 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002143 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 +02002144 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002145 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002146 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 +02002147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002148 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02002149 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 +02002150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002151 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002152 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 +02002153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002154 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002155 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko193dacd2022-10-13 08:43:05 +02002156 LY_CHECK_RET(parse_type_pattern_modifier(ctx, restr));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002158 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002159 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002160 break;
2161 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002162 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163 return LY_EVALID;
2164 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002165 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002167
2168cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169 return ret;
2170}
2171
Michal Vaskoea5abea2018-09-18 13:10:54 +02002172/**
2173 * @brief Parse the type statement.
2174 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002175 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002176 * @param[in,out] type Type to wrote to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002177 * @return LY_ERR values.
2178 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002180parse_type(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002182 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002184 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002185 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002186 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187 struct lysp_type *nest_type;
2188
2189 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002190 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191 return LY_EVALID;
2192 }
2193
2194 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002195 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002196 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002198 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002199 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002200
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002201 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002203 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002204 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 +01002205 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002206 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002207 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002208 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002209 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002211 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002212 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002213 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002215 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002216 LY_CHECK_RET(parse_type_fracdigits(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002217 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002219 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002221 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222 return LY_EVALID;
2223 }
2224 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002225 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002226
Radek Krejci33090f92020-12-17 20:12:46 +01002227 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002228 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002230 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002231 if (type->path) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002232 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002233 return LY_EVALID;
2234 }
2235
aPiecek4bb1e372021-05-07 11:01:00 +02002236 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2237 * corresponding structure, so in case of failure, the lysp_module_free function will take
2238 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2239 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2240 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002241 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 +02002242 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002243 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002244 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002245 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002246 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002247 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002248 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002249 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002250 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002251 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002252 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002254 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002256 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002257 return LY_EVALID;
2258 }
2259 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002260 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002261
Radek Krejci33090f92020-12-17 20:12:46 +01002262 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002263 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002265 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002266 LY_CHECK_RET(parse_type_reqinstance(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002267 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002269 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002270 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002271 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002272 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002273 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002274 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002275 LY_CHECK_RET(parse_ext(ctx, word, word_len, type, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276 break;
2277 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002278 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 return LY_EVALID;
2280 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002281 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002282 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002283
2284cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002285 return ret;
2286}
2287
Michal Vaskoea5abea2018-09-18 13:10:54 +02002288/**
2289 * @brief Parse the leaf statement.
2290 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002291 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002292 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002293 * @return LY_ERR values.
2294 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002295LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002296parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002297{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002298 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002300 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002301 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302 struct lysp_node_leaf *leaf;
2303
David Sedlák60adc092019-08-06 15:57:02 +02002304 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002305 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002306 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002307 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002310 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002311 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312
2313 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002314 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002315 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002316 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002317 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002319 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002320 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 +01002321 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002322 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002323 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002324 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 +02002325 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002326 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002327 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002329 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002330 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002332 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002333 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002335 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002336 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 +02002337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002338 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002339 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002341 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002342 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002344 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002345 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 +02002346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002347 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002348 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002350 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002351 LY_CHECK_RET(parse_ext(ctx, word, word_len, leaf, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002352 break;
2353 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002354 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002355 return LY_EVALID;
2356 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002357 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002358 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002359
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 /* mandatory substatements */
2361 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002362 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363 return LY_EVALID;
2364 }
2365
Michal Vasko12ef5362022-09-16 15:13:58 +02002366cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367 return ret;
2368}
2369
Michal Vaskoea5abea2018-09-18 13:10:54 +02002370/**
2371 * @brief Parse the max-elements statement.
2372 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002373 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002374 * @param[in,out] max Value to write to.
2375 * @param[in,out] flags Flags to write to.
2376 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002377 * @return LY_ERR values.
2378 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002379LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002380parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002381{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002382 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002383 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002384 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002385 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002386 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002387
2388 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002389 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 return LY_EVALID;
2391 }
2392 *flags |= LYS_SET_MAX;
2393
2394 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002395 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396
2397 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002398 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002399 ret = LY_EVALID;
2400 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002401 }
2402
Radek Krejci7f9b6512019-09-18 13:11:09 +02002403 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002404 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002405 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002407 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002408 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002409 ret = LY_EVALID;
2410 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411 }
2412 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002413 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002414 ret = LY_EVALID;
2415 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416 }
2417
2418 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002419 } else {
2420 /* unbounded */
2421 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002424 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002425 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002426 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002427 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 +02002428 break;
2429 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002430 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002431 ret = LY_EVALID;
2432 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002433 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002434 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002436
2437cleanup:
2438 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002439 return ret;
2440}
2441
Michal Vaskoea5abea2018-09-18 13:10:54 +02002442/**
2443 * @brief Parse the min-elements statement.
2444 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002445 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002446 * @param[in,out] min Value to write to.
2447 * @param[in,out] flags Flags to write to.
2448 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002449 * @return LY_ERR values.
2450 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002451LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002452parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002453{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002454 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002455 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002456 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002457 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002458 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002459
2460 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002461 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002462 return LY_EVALID;
2463 }
2464 *flags |= LYS_SET_MIN;
2465
2466 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002467 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002468
2469 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002470 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002471 ret = LY_EVALID;
2472 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473 }
2474
2475 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002476 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002478 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002479 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002480 ret = LY_EVALID;
2481 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002482 }
2483 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002484 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002485 ret = LY_EVALID;
2486 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 }
2488 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002489
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002490 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002491 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002492 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002493 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 +02002494 break;
2495 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002496 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002497 ret = LY_EVALID;
2498 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002500 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002502
2503cleanup:
2504 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 return ret;
2506}
2507
Michal Vaskoea5abea2018-09-18 13:10:54 +02002508/**
2509 * @brief Parse the ordered-by statement.
2510 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002511 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002512 * @param[in,out] llist List or leaf-list to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002513 * @return LY_ERR values.
2514 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002515static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002516parse_orderedby(struct lysp_yang_ctx *ctx, struct lysp_node *llist)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002518 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002519 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002520 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002521 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002522
Michal Vasko193dacd2022-10-13 08:43:05 +02002523 if (llist->flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002524 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 return LY_EVALID;
2526 }
2527
2528 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002529 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002530
Radek Krejcif13b87b2020-12-01 22:02:17 +01002531 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002532 llist->flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002533 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002534 llist->flags |= LYS_ORDBY_USER;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002535 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002536 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002537 ret = LY_EVALID;
2538 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002541 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002543 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002544 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 +02002545 break;
2546 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002547 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002548 ret = LY_EVALID;
2549 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002550 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002551 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002553
2554cleanup:
2555 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556 return ret;
2557}
2558
Michal Vaskoea5abea2018-09-18 13:10:54 +02002559/**
2560 * @brief Parse the leaf-list statement.
2561 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002562 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002563 * @param[in,out] siblings Siblings to add to.
2564 *
2565 * @return LY_ERR values.
2566 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002567LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002568parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002569{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002570 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002572 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002573 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574 struct lysp_node_leaflist *llist;
2575
David Sedlák60adc092019-08-06 15:57:02 +02002576 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002577 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002579 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002580
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002582 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002583 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584
2585 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002586 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002588 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002589 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002591 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002592 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002593 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002595 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002596 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 +02002597 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002598 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002599 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002601 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002602 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002604 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002605 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002607 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002608 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002610 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02002611 LY_CHECK_RET(parse_orderedby(ctx, &llist->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002613 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002614 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 +02002615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002616 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002617 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002619 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002620 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002622 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002623 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 +02002624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002626 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002629 LY_CHECK_RET(parse_ext(ctx, word, word_len, llist, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 break;
2631 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002632 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 return LY_EVALID;
2634 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002635 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002637
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 /* mandatory substatements */
2639 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002640 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641 return LY_EVALID;
2642 }
2643
Michal Vasko12ef5362022-09-16 15:13:58 +02002644cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 return ret;
2646}
2647
Michal Vaskoea5abea2018-09-18 13:10:54 +02002648/**
2649 * @brief Parse the refine statement.
2650 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002651 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002652 * @param[in,out] refines Refines to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002653 * @return LY_ERR values.
2654 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002656parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002658 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002659 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002660 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002661 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662 struct lysp_refine *rf;
2663
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002664 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002665
2666 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002667 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002668 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002669 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002670
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002671 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002673 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002674 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002676 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002677 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002679 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002680 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 +02002681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002682 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002683 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002684 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002686 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002687 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002689 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002690 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002692 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002693 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002695 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002696 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002698 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002699 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 +02002700 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002701 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002702 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 +02002703 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002704 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002705 LY_CHECK_RET(parse_ext(ctx, word, word_len, rf, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706 break;
2707 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002708 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002709 return LY_EVALID;
2710 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002711 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002713
2714cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002715 return ret;
2716}
2717
Michal Vaskoea5abea2018-09-18 13:10:54 +02002718/**
2719 * @brief Parse the typedef statement.
2720 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002721 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002722 * @param[in,out] typedefs Typedefs to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002723 * @return LY_ERR values.
2724 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002725static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002726parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002728 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002730 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002731 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 struct lysp_tpdf *tpdf;
2733
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002734 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735
2736 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002737 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002738 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739
2740 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002741 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002742 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002743 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002744 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 +01002745 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002746 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002747 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002748 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 +02002749 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002750 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002751 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 +02002752 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002753 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002754 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002756 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002757 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002759 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002760 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 +02002761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002762 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002763 LY_CHECK_RET(parse_ext(ctx, word, word_len, tpdf, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764 break;
2765 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002766 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767 return LY_EVALID;
2768 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002769 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002771
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 /* mandatory substatements */
2773 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002774 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 return LY_EVALID;
2776 }
2777
Radek Krejcibbe09a92018-11-08 09:36:54 +01002778 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002779 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002780 assert(ctx->main_ctx);
2781 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002782 }
2783
Michal Vasko12ef5362022-09-16 15:13:58 +02002784cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002785 return ret;
2786}
2787
Michal Vaskoea5abea2018-09-18 13:10:54 +02002788/**
2789 * @brief Parse the input or output statement.
2790 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002791 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002792 * @param[in] kw Type of this particular keyword
2793 * @param[in,out] inout_p Input/output pointer to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002794 * @return LY_ERR values.
2795 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002797parse_inout(struct lysp_yang_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002798 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002800 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002801 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002802 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002803 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002804 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002805
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002806 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002807 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808 return LY_EVALID;
2809 }
2810
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002811 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002812 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2813 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002814 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002815
2816 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002817 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002819 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002820 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002821 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002822 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002823 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002824 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002825 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002826 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002828 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002829 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002831 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002832 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002833 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002834 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002835 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002837 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002838 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002840 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002841 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002843 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002844 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002846 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002847 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002848 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002849 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002850 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002851 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002852 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002853 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002854 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_p, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855 break;
2856 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002857 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 return LY_EVALID;
2859 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002860 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002862
Radek Krejci01180ac2021-01-27 08:48:22 +01002863 if (!inout_p->child) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002864 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", lyplg_ext_stmt2str(inout_kw));
Michal Vaskob83af8a2020-01-06 09:49:22 +01002865 return LY_EVALID;
2866 }
2867
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002868cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869 return ret;
2870}
2871
Michal Vaskoea5abea2018-09-18 13:10:54 +02002872/**
2873 * @brief Parse the action statement.
2874 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002875 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002876 * @param[in,out] actions Actions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002877 * @return LY_ERR values.
2878 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002879LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002880parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002882 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002884 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002885 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002886 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002887
Radek Krejci2a9fc652021-01-22 17:44:34 +01002888 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889
2890 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002891 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002892 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002893 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002894 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002896 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002897 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002898 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002899 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 +02002900 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002901 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002902 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002905 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 +02002906 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002907 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002908 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 break;
2910
Radek Krejcid6b76452019-09-03 17:03:03 +02002911 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002912 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002914 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002915 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
2917
Radek Krejcid6b76452019-09-03 17:03:03 +02002918 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002919 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002920 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002921 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002922 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002924 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002925 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 +02002926 break;
2927 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002928 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929 return LY_EVALID;
2930 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002931 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002933
2934 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2935 if (!act->input.nodetype) {
2936 act->input.nodetype = LYS_INPUT;
2937 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002938 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002939 }
2940 if (!act->output.nodetype) {
2941 act->output.nodetype = LYS_OUTPUT;
2942 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002943 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002944 }
2945
Michal Vasko12ef5362022-09-16 15:13:58 +02002946cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002947 return ret;
2948}
2949
Michal Vaskoea5abea2018-09-18 13:10:54 +02002950/**
2951 * @brief Parse the notification statement.
2952 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002953 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002954 * @param[in,out] notifs Notifications to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002955 * @return LY_ERR values.
2956 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002957LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002958parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002959{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002960 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002962 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002963 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002964 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002965
Radek Krejci2a9fc652021-01-22 17:44:34 +01002966 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967
2968 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002969 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002970 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002971 notif->nodetype = LYS_NOTIF;
2972 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002974 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002976 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002977 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 +02002978 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002979 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002980 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002983 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 +02002984 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002985 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002986 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 break;
2988
Radek Krejcid6b76452019-09-03 17:03:03 +02002989 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002990 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002991 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002992 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002993 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002996 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002999 LY_CHECK_RET(parse_container(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:
Radek Krejci01180ac2021-01-27 08:48:22 +01003002 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003005 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003008 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003010 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003011 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 break;
3013
Radek Krejcid6b76452019-09-03 17:03:03 +02003014 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003015 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003016 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003018 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003019 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003021 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003022 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003024 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003025 LY_CHECK_RET(parse_ext(ctx, word, word_len, notif, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026 break;
3027 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003028 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003029 return LY_EVALID;
3030 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003031 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003032 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003033
Michal Vasko12ef5362022-09-16 15:13:58 +02003034cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 return ret;
3036}
3037
Michal Vaskoea5abea2018-09-18 13:10:54 +02003038/**
3039 * @brief Parse the grouping statement.
3040 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003041 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003042 * @param[in,out] groupings Groupings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003043 * @return LY_ERR values.
3044 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003045LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003046parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003048 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003050 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003051 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003052 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053
Radek Krejci2a9fc652021-01-22 17:44:34 +01003054 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003055
3056 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003057 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003058 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003059 grp->nodetype = LYS_GROUPING;
3060 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003062 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003064 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003065 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 +02003066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003067 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003068 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 +02003069 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003070 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003071 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 break;
3073
Radek Krejcid6b76452019-09-03 17:03:03 +02003074 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003075 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003076 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003077 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003078 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003080 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003081 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003084 LY_CHECK_RET(parse_container(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:
Radek Krejci01180ac2021-01-27 08:48:22 +01003087 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003090 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003093 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003094 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003095 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003096 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097 break;
3098
Radek Krejcid6b76452019-09-03 17:03:03 +02003099 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003100 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003102 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003103 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003104 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003106 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003107 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003109 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003110 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003111 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003113 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003114 LY_CHECK_RET(parse_ext(ctx, word, word_len, grp, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 break;
3116 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003117 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003118 return LY_EVALID;
3119 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003120 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003122
aPiecek63e080d2021-06-29 13:53:28 +02003123 /* store data for collision check */
3124 if (parent) {
3125 assert(ctx->main_ctx);
3126 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3127 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003128
Michal Vasko12ef5362022-09-16 15:13:58 +02003129cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130 return ret;
3131}
3132
Michal Vaskoea5abea2018-09-18 13:10:54 +02003133/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003134 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003135 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003136 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003137 * @param[in,out] augments Augments to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003138 * @return LY_ERR values.
3139 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003140LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003141parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003143 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003145 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003146 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003147 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148
Radek Krejci2a9fc652021-01-22 17:44:34 +01003149 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003150
3151 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003152 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003153 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003154 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003155 aug->nodetype = LYS_AUGMENT;
3156 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003158 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003159 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003160 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003161 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 +02003162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003163 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003164 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003167 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 +02003168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003169 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003170 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003172 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003173 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 break;
3175
Radek Krejcid6b76452019-09-03 17:03:03 +02003176 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003177 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003178 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003179 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003180 LY_CHECK_RET(parse_any(ctx, kw, (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_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003183 LY_CHECK_RET(parse_case(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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003186 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003189 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01003192 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003195 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003198 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003200 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003201 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202 break;
3203
Radek Krejcid6b76452019-09-03 17:03:03 +02003204 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003205 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003206 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003208 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003209 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003210 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003212 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003213 LY_CHECK_RET(parse_ext(ctx, word, word_len, aug, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
3215 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003216 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 return LY_EVALID;
3218 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003219 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003221
Michal Vasko12ef5362022-09-16 15:13:58 +02003222cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223 return ret;
3224}
3225
Michal Vaskoea5abea2018-09-18 13:10:54 +02003226/**
3227 * @brief Parse the uses statement.
3228 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003229 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003230 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003231 * @return LY_ERR values.
3232 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003233LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003234parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003235{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003236 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003238 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003239 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 struct lysp_node_uses *uses;
3241
David Sedlák60adc092019-08-06 15:57:02 +02003242 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003243 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003245 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003248 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003249 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003250
3251 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003252 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003254 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003255 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 +02003256 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003257 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003258 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003261 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 +02003262 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003263 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003264 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003266 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003267 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268 break;
3269
Radek Krejcid6b76452019-09-03 17:03:03 +02003270 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003271 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003273 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003274 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003276 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003277 LY_CHECK_RET(parse_ext(ctx, word, word_len, uses, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278 break;
3279 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003280 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003281 return LY_EVALID;
3282 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003283 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003284 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003285
Michal Vasko12ef5362022-09-16 15:13:58 +02003286cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003287 return ret;
3288}
3289
Michal Vaskoea5abea2018-09-18 13:10:54 +02003290/**
3291 * @brief Parse the case statement.
3292 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003293 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003294 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003295 * @return LY_ERR values.
3296 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003297LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003298parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003299{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003300 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003302 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003303 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304 struct lysp_node_case *cas;
3305
David Sedlák60adc092019-08-06 15:57:02 +02003306 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003307 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003309 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003312 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003313 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314
3315 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003316 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003318 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003319 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 +02003320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003321 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003322 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003325 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 +02003326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003328 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003331 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332 break;
3333
Radek Krejcid6b76452019-09-03 17:03:03 +02003334 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003335 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003336 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003337 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003338 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003341 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003344 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01003347 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003350 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003353 LY_CHECK_RET(parse_list(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_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003356 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003358 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003359 LY_CHECK_RET(parse_ext(ctx, word, word_len, cas, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003360 break;
3361 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003362 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 return LY_EVALID;
3364 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003365 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003367
3368cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 return ret;
3370}
3371
Michal Vaskoea5abea2018-09-18 13:10:54 +02003372/**
3373 * @brief Parse the choice statement.
3374 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003375 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003376 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003377 * @return LY_ERR values.
3378 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003379LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003380parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003382 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003384 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003385 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003386 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003387
David Sedlák60adc092019-08-06 15:57:02 +02003388 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003389 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003390 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003391 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003394 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003395 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396
3397 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003398 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003400 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003401 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003403 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003404 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 +02003405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003406 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003407 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003409 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003410 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003412 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003413 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 +02003414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003415 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003416 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003418 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003419 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003421 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003422 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 +02003423 &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003424 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003425 break;
3426
Radek Krejcid6b76452019-09-03 17:03:03 +02003427 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003428 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003429 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003430 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003431 LY_CHECK_RET(parse_any(ctx, kw, (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_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003434 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003436 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003437 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003438 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003441 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01003444 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003447 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003450 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003452 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003453 LY_CHECK_RET(parse_ext(ctx, word, word_len, choice, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003454 break;
3455 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003456 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003457 return LY_EVALID;
3458 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003459 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003460 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003461
3462cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003463 return ret;
3464}
3465
Michal Vaskoea5abea2018-09-18 13:10:54 +02003466/**
3467 * @brief Parse the container statement.
3468 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003469 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003470 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003471 * @return LY_ERR values.
3472 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003473LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003474parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003475{
3476 LY_ERR ret = 0;
3477 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003478 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003479 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 struct lysp_node_container *cont;
3481
David Sedlák60adc092019-08-06 15:57:02 +02003482 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003483 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003485 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003488 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003489 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490
3491 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003492 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003494 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003495 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003497 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003498 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 +02003499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003500 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003501 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003504 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 +02003505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003506 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003507 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003509 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003510 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003512 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003513 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 +02003514 break;
3515
Radek Krejcid6b76452019-09-03 17:03:03 +02003516 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003517 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003518 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003519 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003520 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003523 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003526 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01003529 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003532 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003535 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003536 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003537 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003538 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539 break;
3540
Radek Krejcid6b76452019-09-03 17:03:03 +02003541 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003542 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003544 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003545 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003547 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003548 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003549 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003551 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003552 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003554 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003555 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003556 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003558 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003559 LY_CHECK_RET(parse_ext(ctx, word, word_len, cont, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 break;
3561 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003562 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563 return LY_EVALID;
3564 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003565 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003567
Michal Vasko12ef5362022-09-16 15:13:58 +02003568cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569 return ret;
3570}
3571
Michal Vaskoea5abea2018-09-18 13:10:54 +02003572/**
3573 * @brief Parse the list statement.
3574 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003575 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003576 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003577 * @return LY_ERR values.
3578 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003579LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003580parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003582 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003583 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003584 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003585 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 struct lysp_node_list *list;
3587
David Sedlák60adc092019-08-06 15:57:02 +02003588 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003589 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003591 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003594 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003595 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596
3597 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003598 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003600 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003601 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003603 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003604 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 +02003605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003606 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003607 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003610 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 +02003611 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003612 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003613 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003615 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003616 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003618 case LY_STMT_KEY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003619 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 +02003620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003621 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003622 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003624 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003625 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003627 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003628 LY_CHECK_RET(parse_orderedby(ctx, &list->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003630 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003631 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 break;
3633
Radek Krejcid6b76452019-09-03 17:03:03 +02003634 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003635 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003636 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003637 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003638 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003641 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003644 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01003647 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003650 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003653 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003654 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003655 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003656 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 break;
3658
Radek Krejcid6b76452019-09-03 17:03:03 +02003659 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003660 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003661 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003662 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003663 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003665 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003666 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003667 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003669 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003670 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003672 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003673 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003674 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003676 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003677 LY_CHECK_RET(parse_ext(ctx, word, word_len, list, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 break;
3679 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003680 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003681 return LY_EVALID;
3682 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003683 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003684 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003685
Michal Vasko12ef5362022-09-16 15:13:58 +02003686cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687 return ret;
3688}
3689
Michal Vaskoea5abea2018-09-18 13:10:54 +02003690/**
3691 * @brief Parse the yin-element statement.
3692 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003693 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003694 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003695 * @return LY_ERR values.
3696 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003697static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003698parse_yinelement(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003700 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003702 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003703 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704
Michal Vasko193dacd2022-10-13 08:43:05 +02003705 if (ext->flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003706 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003707 return LY_EVALID;
3708 }
3709
3710 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003711 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003712
Radek Krejcif13b87b2020-12-01 22:02:17 +01003713 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003714 ext->flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003715 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003716 ext->flags |= LYS_YINELEM_FALSE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003718 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719 free(buf);
3720 return LY_EVALID;
3721 }
3722 free(buf);
3723
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003724 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003725 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003726 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003727 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_YIN_ELEMENT, 0, &ext->exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003728 LY_CHECK_RET(ret);
3729 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003730 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003731 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732 return LY_EVALID;
3733 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003734 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003736
3737cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003738 return ret;
3739}
3740
Michal Vaskoea5abea2018-09-18 13:10:54 +02003741/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003742 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003743 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003744 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003745 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003746 * @return LY_ERR values.
3747 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003749parse_argument(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003751 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003752 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003753 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003754 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755
Michal Vasko193dacd2022-10-13 08:43:05 +02003756 if (ext->argname) {
David Sedlákb3077192019-06-19 10:55:37 +02003757 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003758 return LY_EVALID;
3759 }
3760
3761 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003762 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko193dacd2022-10-13 08:43:05 +02003763 INSERT_WORD_GOTO(ctx, buf, ext->argname, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003765 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003767 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003768 LY_CHECK_RET(parse_yinelement(ctx, ext));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003770 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003771 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_ARGUMENT, 0, &ext->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 break;
3773 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003774 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 return LY_EVALID;
3776 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003777 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003779
3780cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 return ret;
3782}
3783
Michal Vaskoea5abea2018-09-18 13:10:54 +02003784/**
3785 * @brief Parse the extension statement.
3786 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003787 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003788 * @param[in,out] extensions Extensions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003789 * @return LY_ERR values.
3790 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003792parse_extension(struct lysp_yang_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003793{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003794 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003796 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003797 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798 struct lysp_ext *ex;
3799
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003800 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801
3802 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003803 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003804 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003806 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003807 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003808 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003809 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 +02003810 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003811 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003812 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 +02003813 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003814 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003815 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003816 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003817 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003818 LY_CHECK_RET(parse_argument(ctx, ex));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003820 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003821 LY_CHECK_RET(parse_ext(ctx, word, word_len, ex, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003822 break;
3823 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003824 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003825 return LY_EVALID;
3826 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003827 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003829
3830cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003831 return ret;
3832}
3833
Michal Vaskoea5abea2018-09-18 13:10:54 +02003834/**
3835 * @brief Parse the deviate statement.
3836 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003837 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003838 * @param[in,out] deviates Deviates to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003839 * @return LY_ERR values.
3840 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003841LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003842parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003843{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003844 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003845 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003846 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003847 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003848 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003849 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850 struct lysp_deviate_add *d_add = NULL;
3851 struct lysp_deviate_rpl *d_rpl = NULL;
3852 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003853 const char **d_units = NULL;
3854 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003855 struct lysp_restr **d_musts = NULL;
3856 uint16_t *d_flags = 0;
3857 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858
3859 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003860 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861
Radek Krejcif13b87b2020-12-01 22:02:17 +01003862 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003864 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003866 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003867 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003868 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003869 dev_mod = LYS_DEV_DELETE;
3870 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003871 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003872 ret = LY_EVALID;
3873 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003875
3876 /* create structure */
3877 switch (dev_mod) {
3878 case LYS_DEV_NOT_SUPPORTED:
3879 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003880 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 break;
3882 case LYS_DEV_ADD:
3883 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003884 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885 d = (struct lysp_deviate *)d_add;
3886 d_units = &d_add->units;
3887 d_uniques = &d_add->uniques;
3888 d_dflts = &d_add->dflts;
3889 d_musts = &d_add->musts;
3890 d_flags = &d_add->flags;
3891 d_min = &d_add->min;
3892 d_max = &d_add->max;
3893 break;
3894 case LYS_DEV_REPLACE:
3895 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003896 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003897 d = (struct lysp_deviate *)d_rpl;
3898 d_units = &d_rpl->units;
3899 d_flags = &d_rpl->flags;
3900 d_min = &d_rpl->min;
3901 d_max = &d_rpl->max;
3902 break;
3903 case LYS_DEV_DELETE:
3904 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003905 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003906 d = (struct lysp_deviate *)d_del;
3907 d_units = &d_del->units;
3908 d_uniques = &d_del->uniques;
3909 d_dflts = &d_del->dflts;
3910 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003911 break;
3912 default:
3913 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003914 LOGINT(PARSER_CTX(ctx));
3915 ret = LY_EINT;
3916 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917 }
3918 d->mod = dev_mod;
3919
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003920 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003922 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003923 switch (dev_mod) {
3924 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003925 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003926 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003927 ret = LY_EVALID;
3928 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003930 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931 break;
3932 }
3933 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003934 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003935 switch (dev_mod) {
3936 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02003937 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003938 ret = LY_EVALID;
3939 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003941 ret = parse_text_field(ctx, &d_rpl->dflt, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts);
3942 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003943 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 break;
3945 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003946 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 +02003947 break;
3948 }
3949 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003950 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003951 switch (dev_mod) {
3952 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003953 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003954 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003955 ret = LY_EVALID;
3956 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003957 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003958 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959 break;
3960 }
3961 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003962 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003963 switch (dev_mod) {
3964 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003965 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003966 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003967 ret = LY_EVALID;
3968 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003970 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003971 break;
3972 }
3973 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003974 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003975 switch (dev_mod) {
3976 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003977 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003978 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003979 ret = LY_EVALID;
3980 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003982 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983 break;
3984 }
3985 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003986 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 switch (dev_mod) {
3988 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003989 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003990 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003991 ret = LY_EVALID;
3992 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003994 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003995 break;
3996 }
3997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003998 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003999 switch (dev_mod) {
4000 case LYS_DEV_NOT_SUPPORTED:
4001 case LYS_DEV_ADD:
4002 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004003 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004004 ret = LY_EVALID;
4005 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004006 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004007 if (d_rpl->type) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004008 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004009 ret = LY_EVALID;
4010 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004011 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004012 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004013 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4014 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004015 break;
4016 }
4017 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004018 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019 switch (dev_mod) {
4020 case LYS_DEV_NOT_SUPPORTED:
4021 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004022 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004023 ret = LY_EVALID;
4024 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004025 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004026 LY_CHECK_GOTO(ret = parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004027 break;
4028 }
4029 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004030 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004031 switch (dev_mod) {
4032 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02004033 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004034 ret = LY_EVALID;
4035 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004036 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004037 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 +02004038 break;
4039 }
4040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004041 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004042 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 +02004043 break;
4044 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004045 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004046 ret = LY_EVALID;
4047 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004048 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004049 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004050 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004051
4052cleanup:
4053 free(buf);
4054 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004055 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004056 free(d);
4057 } else {
4058 /* insert into siblings */
4059 LY_LIST_INSERT(deviates, d, next);
4060 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004061 return ret;
4062}
4063
Michal Vaskoea5abea2018-09-18 13:10:54 +02004064/**
4065 * @brief Parse the deviation statement.
4066 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004067 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004068 * @param[in,out] deviations Deviations to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004069 * @return LY_ERR values.
4070 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004071LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004072parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004074 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004075 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004076 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004077 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004079 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004080
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004081 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004082
4083 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004084 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004085 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004086 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004087
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004088 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004089 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004090 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004091 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 +02004092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004093 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004094 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004096 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004097 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 +02004098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004099 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004100 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 +02004101 break;
4102 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004103 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004104 ret = LY_EVALID;
4105 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004107 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004108 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004109
Michal Vasko7fbc8162018-09-17 10:35:16 +02004110 /* mandatory substatements */
4111 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004112 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004113 ret = LY_EVALID;
4114 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 }
4116
Michal Vasko12ef5362022-09-16 15:13:58 +02004117cleanup:
4118 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004119 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004120 LY_ARRAY_DECREMENT_FREE(*deviations);
4121 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004122 return ret;
4123}
4124
Michal Vaskoea5abea2018-09-18 13:10:54 +02004125/**
4126 * @brief Parse the feature statement.
4127 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004128 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004129 * @param[in,out] features Features to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004130 * @return LY_ERR values.
4131 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004132LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004133parse_feature(struct lysp_yang_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004134{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004135 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004136 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004137 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004138 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139 struct lysp_feature *feat;
4140
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004141 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004142
4143 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004144 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004145 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004146
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004147 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004148 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004149 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004150 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 +02004151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004152 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004153 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004156 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 +02004157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004158 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004159 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004161 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004162 LY_CHECK_RET(parse_ext(ctx, word, word_len, feat, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 break;
4164 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004165 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004166 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004167 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004168 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004170
4171cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004172 return ret;
4173}
4174
Michal Vaskoea5abea2018-09-18 13:10:54 +02004175/**
4176 * @brief Parse the identity statement.
4177 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004178 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004179 * @param[in,out] identities Identities to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004180 * @return LY_ERR values.
4181 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004182LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004183parse_identity(struct lysp_yang_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004185 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004186 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004187 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004189 struct lysp_ident *ident;
4190
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004191 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004192
4193 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004194 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004195 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004196
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004197 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004198 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004199 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004200 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 +02004201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004202 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004203 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004204 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004207 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 +02004208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004209 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004210 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004212 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004213 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004214 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 +01004215 return LY_EVALID;
4216 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004217 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 +02004218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004219 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004220 LY_CHECK_RET(parse_ext(ctx, word, word_len, ident, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 break;
4222 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004223 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 return LY_EVALID;
4225 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004226 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004228
4229cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004230 return ret;
4231}
4232
Michal Vaskoea5abea2018-09-18 13:10:54 +02004233/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004234 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004235 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004236 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004237 * @param[in,out] mod Module to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004238 * @return LY_ERR values.
4239 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004240LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004241parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004242{
4243 LY_ERR ret = 0;
4244 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004245 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004246 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004247 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004248 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004249
Michal Vaskoc3781c32020-10-06 14:04:08 +02004250 mod->is_submod = 0;
4251
4252 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004253 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004254 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004255
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004256 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004257
Radek Krejcie3846472018-10-15 15:24:51 +02004258#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004259 if (mod_stmt > SECTION) {\
4260 LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;\
4261 } mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004262
Michal Vasko7fbc8162018-09-17 10:35:16 +02004263 switch (kw) {
4264 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004265 case LY_STMT_NAMESPACE:
4266 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004267 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004269 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004270 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004271 break;
4272 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004273 case LY_STMT_INCLUDE:
4274 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004275 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004276 break;
4277 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004278 case LY_STMT_ORGANIZATION:
4279 case LY_STMT_CONTACT:
4280 case LY_STMT_DESCRIPTION:
4281 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004282 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004283 break;
4284
4285 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004286 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004287 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004288 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004289 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004290 case LY_STMT_ANYDATA:
4291 case LY_STMT_ANYXML:
4292 case LY_STMT_AUGMENT:
4293 case LY_STMT_CHOICE:
4294 case LY_STMT_CONTAINER:
4295 case LY_STMT_DEVIATION:
4296 case LY_STMT_EXTENSION:
4297 case LY_STMT_FEATURE:
4298 case LY_STMT_GROUPING:
4299 case LY_STMT_IDENTITY:
4300 case LY_STMT_LEAF:
4301 case LY_STMT_LEAF_LIST:
4302 case LY_STMT_LIST:
4303 case LY_STMT_NOTIFICATION:
4304 case LY_STMT_RPC:
4305 case LY_STMT_TYPEDEF:
4306 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004307 mod_stmt = Y_MOD_BODY;
4308 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004309 case LY_STMT_EXTENSION_INSTANCE:
4310 /* no place in the statement order defined */
4311 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004312 default:
4313 /* error handled in the next switch */
4314 break;
4315 }
Radek Krejcie3846472018-10-15 15:24:51 +02004316#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004317
Radek Krejcie3846472018-10-15 15:24:51 +02004318 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004319 switch (kw) {
4320 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004321 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004322 LY_CHECK_RET(parse_yangversion(ctx, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004324 case LY_STMT_NAMESPACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004325 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 +02004326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004327 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02004328 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 +02004329 break;
4330
4331 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004332 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004333 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004336 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004337 break;
4338
4339 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004340 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004341 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 +02004342 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004343 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004344 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 +02004345 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004346 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004347 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 +02004348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004349 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004350 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 +02004351 break;
4352
4353 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004354 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004355 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004356 break;
4357
4358 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004359 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004360 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004361 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004362 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004363 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004365 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004366 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004367 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004368 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004369 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01004372 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004374 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004375 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004377 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004378 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004380 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004381 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004382 break;
4383
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004385 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004388 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004390 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004391 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004393 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004394 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004396 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004397 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004399 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004400 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004402 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004403 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004405 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004406 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004407 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004408 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004409 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004411 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004412 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004413 break;
4414
4415 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004416 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004417 return LY_EVALID;
4418 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004419 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004420 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004421
Michal Vasko7fbc8162018-09-17 10:35:16 +02004422 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004423 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004424 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004425 return LY_EVALID;
4426 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004427 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004428 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004429 }
4430
Radek Krejcie9e987e2018-10-31 12:50:27 +01004431 /* submodules share the namespace with the module names, so there must not be
4432 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004433 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004434 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004435 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004436 return LY_EVALID;
4437 }
4438
Michal Vasko12ef5362022-09-16 15:13:58 +02004439cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004440 return ret;
4441}
4442
4443/**
4444 * @brief Parse submodule substatements.
4445 *
4446 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004447 * @param[out] submod Parsed submodule structure.
4448 *
4449 * @return LY_ERR values.
4450 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004451LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004452parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004453{
4454 LY_ERR ret = 0;
4455 char *buf, *word;
4456 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004457 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004458 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004459 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004460
Michal Vaskoc3781c32020-10-06 14:04:08 +02004461 submod->is_submod = 1;
4462
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004463 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004464 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004465 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004466
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004467 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004468
4469#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004470 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 +01004471
4472 switch (kw) {
4473 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004474 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004475 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4476 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004477 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004478 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4479 break;
4480 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004481 case LY_STMT_INCLUDE:
4482 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004483 CHECK_ORDER(Y_MOD_LINKAGE);
4484 break;
4485 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004486 case LY_STMT_ORGANIZATION:
4487 case LY_STMT_CONTACT:
4488 case LY_STMT_DESCRIPTION:
4489 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004490 CHECK_ORDER(Y_MOD_META);
4491 break;
4492
4493 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004494 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004495 CHECK_ORDER(Y_MOD_REVISION);
4496 break;
4497 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004498 case LY_STMT_ANYDATA:
4499 case LY_STMT_ANYXML:
4500 case LY_STMT_AUGMENT:
4501 case LY_STMT_CHOICE:
4502 case LY_STMT_CONTAINER:
4503 case LY_STMT_DEVIATION:
4504 case LY_STMT_EXTENSION:
4505 case LY_STMT_FEATURE:
4506 case LY_STMT_GROUPING:
4507 case LY_STMT_IDENTITY:
4508 case LY_STMT_LEAF:
4509 case LY_STMT_LEAF_LIST:
4510 case LY_STMT_LIST:
4511 case LY_STMT_NOTIFICATION:
4512 case LY_STMT_RPC:
4513 case LY_STMT_TYPEDEF:
4514 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004515 mod_stmt = Y_MOD_BODY;
4516 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004517 case LY_STMT_EXTENSION_INSTANCE:
4518 /* no place in the statement order defined */
4519 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004520 default:
4521 /* error handled in the next switch */
4522 break;
4523 }
4524#undef CHECK_ORDER
4525
4526 prev_kw = kw;
4527 switch (kw) {
4528 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004529 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004530 LY_CHECK_RET(parse_yangversion(ctx, (struct lysp_module *)submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004532 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02004533 LY_CHECK_RET(parse_belongsto(ctx, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004534 break;
4535
4536 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004537 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004538 if (submod->version == LYS_VERSION_1_1) {
4539 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4540 submod->name);
4541 }
Radek Krejci33090f92020-12-17 20:12:46 +01004542 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004544 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004545 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004546 break;
4547
4548 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004549 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004550 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 +01004551 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004552 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004553 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 +01004554 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004555 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004556 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 +01004557 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004558 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004559 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 +01004560 break;
4561
4562 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004563 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004564 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004565 break;
4566
4567 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004568 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004569 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004570 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004571 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004572 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004573 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004574 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004575 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004577 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004578 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01004581 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004582 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004583 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004584 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004585 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004586 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004587 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004588 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004589 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004590 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004591 break;
4592
Radek Krejcid6b76452019-09-03 17:03:03 +02004593 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004594 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004595 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004596 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004597 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004598 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004599 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004600 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004601 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004602 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004603 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004604 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004605 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004606 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004607 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004608 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004609 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004610 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004611 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004612 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004614 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004615 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004617 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004618 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004619 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004620 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004621 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004622 break;
4623
4624 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004625 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004626 return LY_EVALID;
4627 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004628 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004629 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004630
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004631 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004632 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004633 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004634 return LY_EVALID;
4635 }
4636
4637 /* submodules share the namespace with the module names, so there must not be
4638 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004639 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004640 /* main modules may have different revisions */
4641 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004642 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004643 return LY_EVALID;
4644 }
4645
Michal Vasko12ef5362022-09-16 15:13:58 +02004646cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004647 return ret;
4648}
4649
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004650/**
4651 * @brief Skip any redundant characters, namely whitespaces and comments.
4652 *
4653 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004654 * @return LY_SUCCESS on success.
4655 * @return LY_EVALID on invalid comment.
4656 */
4657static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004658skip_redundant_chars(struct lysp_yang_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004659{
4660 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004661 while (ctx->in->current[0]) {
4662 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004663 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004664 ly_in_skip(ctx->in, 2);
4665 LY_CHECK_RET(skip_comment(ctx, 1));
4666 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004667 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004668 ly_in_skip(ctx->in, 2);
4669 LY_CHECK_RET(skip_comment(ctx, 2));
4670 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004671 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004672 if (ctx->in->current[0] == '\n') {
4673 LY_IN_NEW_LINE(ctx->in);
4674 }
Radek Krejci33090f92020-12-17 20:12:46 +01004675 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004676 } else {
4677 break;
4678 }
4679 }
4680
4681 return LY_SUCCESS;
4682}
4683
Radek Krejcid4557c62018-09-17 11:42:09 +02004684LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004685yang_parse_submodule(struct lysp_yang_ctx **context, struct ly_ctx *ly_ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004686 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004687{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004688 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004689 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004690 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004691 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004692 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004693 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004694
aPiecek56be92a2021-07-01 07:18:10 +02004695 assert(context && ly_ctx && main_ctx && in && submod);
4696
David Sedlák1b623122019-08-05 15:27:49 +02004697 /* create context */
4698 *context = calloc(1, sizeof **context);
4699 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004700 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004701 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004702 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004703
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004704 mod_p = calloc(1, sizeof *mod_p);
4705 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004706 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004707 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004708
4709 /* use main context parsed mods adding the current one */
4710 (*context)->parsed_mods = main_ctx->parsed_mods;
4711 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004712
Michal Vaskof8ebf132022-11-21 14:06:48 +01004713 LOG_LOCSET(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004714
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004715 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004716 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004717 LY_CHECK_GOTO(ret, cleanup);
4718
Michal Vasko7fbc8162018-09-17 10:35:16 +02004719 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004720 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004721 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004722
Radek Krejcid6b76452019-09-03 17:03:03 +02004723 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004724 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004725 ret = LY_EINVAL;
4726 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004727 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004728 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004729 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004730 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004731 }
4732
Michal Vasko7fbc8162018-09-17 10:35:16 +02004733 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004734 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004735 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004736
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004737 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004738 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004739 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004740 if (in->current[0]) {
4741 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004742 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004743 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004744 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004745
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004746 mod_p->parsing = 0;
4747 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004748
Radek Krejcibbe09a92018-11-08 09:36:54 +01004749cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004750 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004751 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004752 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004753 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004754 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004755 }
4756
4757 return ret;
4758}
4759
4760LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004761yang_parse_module(struct lysp_yang_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004762{
4763 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004764 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004765 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004766 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004767 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004768 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004769
David Sedlák1b623122019-08-05 15:27:49 +02004770 /* create context */
4771 *context = calloc(1, sizeof **context);
4772 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004773 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004774 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004775 (*context)->in = in;
Michal Vaskod0625d72022-10-06 15:02:50 +02004776 (*context)->main_ctx = (struct lysp_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004777
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004778 mod_p = calloc(1, sizeof *mod_p);
4779 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4780 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004781 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004782
Michal Vaskof8ebf132022-11-21 14:06:48 +01004783 LOG_LOCSET(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004784
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004785 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004786 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004787 LY_CHECK_GOTO(ret, cleanup);
4788
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004789 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004790 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004791 LY_CHECK_GOTO(ret, cleanup);
4792
Radek Krejcid6b76452019-09-03 17:03:03 +02004793 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004794 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 +01004795 ret = LY_EINVAL;
4796 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004797 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004798 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004799 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004800 goto cleanup;
4801 }
4802
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004803 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004804 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004805 LY_CHECK_GOTO(ret, cleanup);
4806
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004807 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004808 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004809 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004810 if (in->current[0]) {
4811 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004812 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004813 goto cleanup;
4814 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004815
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004816 mod->parsed = mod_p;
4817
4818cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004819 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004820 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004821 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004822 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004823 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004824 }
4825
Michal Vasko7fbc8162018-09-17 10:35:16 +02004826 return ret;
4827}