blob: cde230ac8dc03471908e4f4659e43606ef6d1807 [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
Michal Vaskoc636ea42022-09-16 10:20:31 +02006 * Copyright (c) 2018 - 2022 CESNET, z.s.p.o.
Michal Vasko7fbc8162018-09-17 10:35:16 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko63f3d842020-07-08 10:10:14 +020014#include "parser_internal.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
17#include <ctype.h>
18#include <errno.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "log.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020030#include "path.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "set.h"
32#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010033#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "tree_schema.h"
Michal Vaskoc636ea42022-09-16 10:20:31 +020035#include "tree_schema_free.h"
Radek Krejci70853c52018-10-15 14:46:16 +020036#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020037
Radek Krejci77114102021-03-10 15:21:57 +010038struct lys_glob_unres;
39
Radek Krejciceaf2122019-01-02 15:03:26 +010040/**
41 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020042 *
Radek Krejciceaf2122019-01-02 15:03:26 +010043 * @param[in] CTX yang parser context to access libyang context.
44 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
45 * @param[out] TARGET variable where to store the pointer to the inserted value.
46 * @param[in] WORD string to store.
47 * @param[in] LEN length of the string in WORD to store.
48 */
Michal Vasko12ef5362022-09-16 15:13:58 +020049#define INSERT_WORD_GOTO(CTX, BUF, TARGET, WORD, LEN, RET, LABEL) \
50 if (BUF) {LY_CHECK_GOTO(RET = lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)), LABEL);}\
51 else {LY_CHECK_GOTO(RET = lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)), LABEL);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020052
Radek Krejciceaf2122019-01-02 15:03:26 +010053/**
Michal Vasko63f3d842020-07-08 10:10:14 +020054 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020055 *
Radek Krejciceaf2122019-01-02 15:03:26 +010056 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010057 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
58 */
Radek Krejcid54412f2020-12-17 20:25:35 +010059#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020060
Michal Vaskoea5abea2018-09-18 13:10:54 +020061/**
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020062 * @brief Loop through all substatements. Starts a for loop and ::YANG_READ_SUBSTMT_NEXT_ITER must be used at its end.
Michal Vaskoea5abea2018-09-18 13:10:54 +020063 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020064 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020065 * @param[out] KW YANG keyword read.
66 * @param[out] WORD Pointer to the keyword itself.
67 * @param[out] WORD_LEN Length of the keyword.
Michal Vasko12ef5362022-09-16 15:13:58 +020068 * @param[out] RET Variable for error storing.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020069 * @param[in] ERR_LABEL Label to go to on error.
Michal Vaskoea5abea2018-09-18 13:10:54 +020070 */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020071#define YANG_READ_SUBSTMT_FOR_GOTO(CTX, KW, WORD, WORD_LEN, RET, ERR_LABEL) \
72 ly_bool __loop_end = 0; \
Michal Vasko12ef5362022-09-16 15:13:58 +020073 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020074 goto ERR_LABEL; \
aPieceka24a2252021-05-07 10:52:31 +020075 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020076 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020077 __loop_end = 1; \
78 } else if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
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 *
90 * @param[in] CTX yang parser context for logging.
91 * @param[out] KW YANG keyword read.
92 * @param[out] WORD Pointer to the keyword itself.
93 * @param[out] WORD_LEN Length of the keyword.
94 * @param[in] EXTS Final extension instance array to store.
95 * @param[out] RET Variable for error storing.
96 * @param[in] ERR_LABEL Label to go to on error.
97 */
98#define YANG_READ_SUBSTMT_NEXT_ITER(CTX, KW, WORD, WORD_LEN, EXTS, RET, ERR_LABEL) \
99 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
100 goto ERR_LABEL; \
101 } \
102 if (KW == LY_STMT_SYNTAX_RIGHT_BRACE) { \
103 if (EXTS && (RET = ly_set_add(&(CTX)->ext_inst, (EXTS), 1, NULL))) { \
104 goto ERR_LABEL; \
105 } \
106 __loop_end = 1; \
107 }
108
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:
Radek Krejci33090f92020-12-17 20:12:46 +0100352 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200353 case '\'':
354 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100355 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100356 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200357 break;
358 default:
359 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100360 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200361 break;
362 }
363 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100364 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100365 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200366 case '\"':
367 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100368 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100369 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200370 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200371 break;
372 case '\\':
373 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100374 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200375
376 /* the backslash sequence is substituted, so we will need a buffer to store the result */
377 need_buf = 1;
378
379 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100380 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200381
382 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
383 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
384 trailing_ws = 0;
385
386 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
387 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200388 break;
389 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200390 if (current_indent < block_indent) {
391 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100392 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200393 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100394 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100395 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100396 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200397 }
398 break;
399 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200400 if (current_indent < block_indent) {
401 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100402 current_indent += Y_TAB_SPACES;
403 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200404 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200405 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100406 c = ctx->in->current;
407 ctx->in->current = " ";
408 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
409 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100410 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200411 }
Radek Krejci33090f92020-12-17 20:12:46 +0100412 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200413 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100414 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100415 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100416 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200417 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100418 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200419 }
420 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200421 case '\r':
422 if (ctx->in->current[1] != '\n') {
423 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
424 return LY_EVALID;
425 }
426 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200427 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200428 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 /* we will be removing the indents so we need our own buffer */
430 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200431
432 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200433 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200434
Radek Krejciff13cd12019-10-25 15:34:24 +0200435 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200436 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200437 }
438
439 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100440 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200441
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200442 /* reset context indentation counter for possible string after this one */
443 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200444 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200445 break;
446 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200447 /* first non-whitespace character, stop eating indentation */
448 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200449
450 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100451 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200452 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200453 break;
454 }
455 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100456 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200457 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100458 c = ctx->in->current;
459 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200460 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100461 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100462 /* fix false newline count in buf_store_char() */
463 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200464 break;
465 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100466 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 break;
468 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200469 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200471 break;
472 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200473 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100474 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200475 return LY_EVALID;
476 }
477
478 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100479 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480
Radek Krejcif13b87b2020-12-01 22:02:17 +0100481 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100482 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200483 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100484 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100485 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200486 case '+':
487 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100488 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200489 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200490 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200491 case '\r':
492 if (ctx->in->current[1] != '\n') {
493 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
494 return LY_EVALID;
495 }
496 MOVE_INPUT(ctx, 1);
497 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200498 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100499 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100500 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200501 case ' ':
502 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200503 /* just skip */
504 break;
505 default:
506 /* string is finished */
507 goto string_end;
508 }
Radek Krejci33090f92020-12-17 20:12:46 +0100509 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200510 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100511 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100512 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200513 case '\r':
514 if (ctx->in->current[1] != '\n') {
515 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
516 return LY_EVALID;
517 }
518 MOVE_INPUT(ctx, 1);
519 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200520 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100521 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100522 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200523 case ' ':
524 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200525 /* skip */
526 break;
527 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100528 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200529 break;
530 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100531 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200532 break;
533 default:
534 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200535 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200536 return LY_EVALID;
537 }
Radek Krejci33090f92020-12-17 20:12:46 +0100538 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200539 break;
540 default:
541 return LY_EINT;
542 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200543 }
544
545string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200546 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200547 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200548 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200549 return LY_EVALID;
550 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200551 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100552
553#undef STRING_ENDED
554#undef STRING_SINGLE_QUOTED
555#undef STRING_DOUBLE_QUOTED
556#undef STRING_DOUBLE_QUOTED_ESCAPED
557#undef STRING_PAUSED_NEXTSTRING
558#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200559}
560
Michal Vaskoea5abea2018-09-18 13:10:54 +0200561/**
562 * @brief Get another YANG string from the raw data.
563 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200564 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200565 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200566 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
567 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200568 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200569 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
570 * set to NULL. Otherwise equal to \p word_p.
571 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200572 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200573 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200574LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200575get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200576 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200577{
Michal Vaskob68ea142021-04-26 13:46:00 +0200578 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200579 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200580 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200581
Michal Vasko7fbc8162018-09-17 10:35:16 +0200582 /* word buffer - dynamically allocated */
583 *word_b = NULL;
584
585 /* word pointer - just a pointer to data */
586 *word_p = NULL;
587
588 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100589 while (ctx->in->current[0]) {
590 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200591 case '\'':
592 case '\"':
593 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200594 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100595 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200596 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200597 ret = LY_EVALID;
598 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200599 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200600 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100601 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200602 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200603 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200604 if (!*word_p) {
605 /* do not return NULL word */
606 *word_p = "";
607 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200608 goto str_end;
609 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100610 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200611 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100612 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200613 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100614 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200615 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100616 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200617 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200618 } else {
619 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200620 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200621 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200622 break;
623 case ' ':
624 if (*word_len) {
625 /* word is finished */
626 goto str_end;
627 }
Radek Krejci33090f92020-12-17 20:12:46 +0100628 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200629 break;
630 case '\t':
631 if (*word_len) {
632 /* word is finished */
633 goto str_end;
634 }
635 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100636 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200637
Radek Krejci33090f92020-12-17 20:12:46 +0100638 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200639 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200640 case '\r':
641 if (ctx->in->current[1] != '\n') {
642 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200643 ret = LY_EVALID;
644 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200645 }
646 MOVE_INPUT(ctx, 1);
647 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200648 case '\n':
649 if (*word_len) {
650 /* word is finished */
651 goto str_end;
652 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100653 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100654 MOVE_INPUT(ctx, 1);
655
Michal Vasko7fbc8162018-09-17 10:35:16 +0200656 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200657 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200658 break;
659 case ';':
660 case '{':
661 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
662 /* word is finished */
663 goto str_end;
664 }
665
Radek Krejci33090f92020-12-17 20:12:46 +0100666 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200667 ret = LY_EVALID;
668 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200669 case '}':
670 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100671 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200672 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200673 ret = LY_EVALID;
674 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200675 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200676 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200677 break;
678 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200679 }
680
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200681 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200682 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200683 ret = LY_EVALID;
684 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200685
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200687 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200689 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200690 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200691 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200692 *word_p = *word_b;
693 }
694
695 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200696
697error:
698 free(*word_b);
699 *word_b = NULL;
700 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200701}
702
Michal Vaskoea5abea2018-09-18 13:10:54 +0200703/**
704 * @brief Get another YANG keyword from the raw data.
705 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200706 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200707 * @param[out] kw YANG keyword read.
708 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
709 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200710 * @return LY_ERR values.
711 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200712LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200713get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200714{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200715 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200716 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200717 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200718
719 if (word_p) {
720 *word_p = NULL;
721 *word_len = 0;
722 }
723
724 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100725 while (ctx->in->current[0]) {
726 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200727 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100728 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200729 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100730 MOVE_INPUT(ctx, 2);
731 LY_CHECK_RET(skip_comment(ctx, 1));
732 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200733 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100734 MOVE_INPUT(ctx, 2);
735 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200737 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200738 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200739 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200740 }
Radek Krejci13028282019-06-11 14:56:48 +0200741 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200742 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200743 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100744 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200745 ctx->indent = 0;
746 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200747 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200748 /* skip whitespaces (optsep) */
749 ++ctx->indent;
750 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200751 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100753 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200754 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100755 case '\r':
756 /* possible CRLF endline */
757 if (ctx->in->current[1] == '\n') {
758 break;
759 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100760 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200761 default:
762 /* either a keyword start or an invalid character */
763 goto keyword_start;
764 }
765
Radek Krejci33090f92020-12-17 20:12:46 +0100766 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200767 }
768
769keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100770 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100771 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200772
aPiecek93582ed2021-05-25 14:49:06 +0200773 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
774 goto success;
775 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
776 ctx->depth++;
777 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100778 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200779 return LY_EINVAL;
780 }
781 goto success;
782 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
783 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200784 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200785 }
786
Radek Krejcid6b76452019-09-03 17:03:03 +0200787 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200788 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100789 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200790 case '\r':
791 if (ctx->in->current[1] != '\n') {
792 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
793 return LY_EVALID;
794 }
795 MOVE_INPUT(ctx, 1);
796 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200797 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200798 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200799 case ' ':
800 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200801 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200802 case ':':
803 /* keyword is not actually a keyword, but prefix of an extension.
804 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
805 * and we will be checking the keyword (extension instance) itself */
806 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100807 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200808 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200809 case '{':
810 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200811 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200812 break;
813 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100814 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200815 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100816 MOVE_INPUT(ctx, 1);
817 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200818 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200819 return LY_EVALID;
820 }
821 } else {
822 /* still can be an extension */
823 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200824
Radek Krejci156ccaf2018-10-15 15:49:17 +0200825extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100826 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200827 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
828 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200829 uint32_t c = 0;
830
Radek Krejci33090f92020-12-17 20:12:46 +0100831 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
832 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200833 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200834 /* check character validity */
Michal Vaskod0625d72022-10-06 15:02:50 +0200835 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100836 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837 }
Radek Krejci33090f92020-12-17 20:12:46 +0100838 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200839 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200840 return LY_EVALID;
841 }
842
843 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200844 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100845 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200846 return LY_EVALID;
847 }
848
Radek Krejcid6b76452019-09-03 17:03:03 +0200849 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200850 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200851
Radek Krejci626df482018-10-11 15:06:31 +0200852success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200853 if (word_p) {
854 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100855 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200856 }
857
858 return LY_SUCCESS;
859}
860
Michal Vaskoea5abea2018-09-18 13:10:54 +0200861/**
862 * @brief Parse extension instance substatements.
863 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200864 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200865 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200866 * @param[in] word Extension instance substatement name (keyword).
867 * @param[in] word_len Extension instance substatement name length.
868 * @param[in,out] child Children of this extension instance to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200869 * @return LY_ERR values.
870 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200871static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200872parse_ext_substmt(struct lysp_yang_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200873 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200874{
875 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100876 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200877 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878 struct lysp_stmt *stmt, *par_child;
879
880 stmt = calloc(1, sizeof *stmt);
881 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
882
Radek Krejcibb9b1982019-04-08 14:24:59 +0200883 /* insert into parent statements */
884 if (!*child) {
885 *child = stmt;
886 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200887 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200888 par_child->next = stmt;
889 }
890
Michal Vaskofc2cd072021-02-24 13:17:17 +0100891 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200892 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200893
894 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100895 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200896 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200897 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200898 }
899
Radek Krejci8df109d2021-04-23 12:19:08 +0200900 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100901 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100902 stmt->kw = kw;
903
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200904 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
905 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
906 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200907 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200908
909cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910 return ret;
911}
912
Michal Vaskoea5abea2018-09-18 13:10:54 +0200913/**
914 * @brief Parse extension instance.
915 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200916 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200917 * @param[in] ext_name Extension instance substatement name (keyword).
918 * @param[in] ext_name_len Extension instance substatement name length.
Michal Vasko193dacd2022-10-13 08:43:05 +0200919 * @param[in] parent Current statement parent.
920 * @param[in] parent_stmt Type of @p parent statement.
921 * @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 +0200922 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200923 * @return LY_ERR values.
924 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200925static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200926parse_ext(struct lysp_yang_ctx *ctx, const char *ext_name, size_t ext_name_len, const void *parent,
927 enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100929 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200930 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200931 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200933 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200934
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200935 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936
Michal Vaskofc2cd072021-02-24 13:17:17 +0100937 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
938 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
939 return LY_EVALID;
940 }
941
942 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200943 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200944
945 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100946 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200947 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200948 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200949 }
950
Michal Vaskofc2cd072021-02-24 13:17:17 +0100951 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200952 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200953 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100954 e->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200955 e->parent = (void *)parent;
956 e->parent_stmt = parent_stmt;
957 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100958
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200959 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
960 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
961 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200962 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200963
964cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200965 return ret;
966}
967
Michal Vaskoea5abea2018-09-18 13:10:54 +0200968/**
969 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
970 * description, etc...
971 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200972 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +0200973 * @param[in] parent Current statement parent.
974 * @param[in] parent_stmt Type of statement in @p value.
975 * @param[in] parent_stmt_index In case of several @p parent_stmt, index of the relevant @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200976 * @param[in,out] value Place to store the parsed value.
977 * @param[in] arg Type of the YANG keyword argument (of the value).
978 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200979 * @return LY_ERR values.
980 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200982parse_text_field(struct lysp_yang_ctx *ctx, const void *parent, enum ly_stmt parent_stmt, uint32_t parent_stmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200983 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100985 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200986 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200987 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200988 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989
990 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200991 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992 return LY_EVALID;
993 }
994
995 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100996 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200997
998 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +0200999 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001000
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001001 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001002 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001003 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001004 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent, parent_stmt, parent_stmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001005 break;
1006 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001007 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008 return LY_EVALID;
1009 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001010 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001012
1013cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014 return ret;
1015}
1016
Michal Vaskoea5abea2018-09-18 13:10:54 +02001017/**
1018 * @brief Parse the yang-version statement.
1019 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001020 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001021 * @param[in,out] mod Module to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001022 * @return LY_ERR values.
1023 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001024static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001025parse_yangversion(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001026{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001027 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001028 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001029 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001030 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031
Michal Vasko193dacd2022-10-13 08:43:05 +02001032 if (mod->version) {
David Sedlákb3077192019-06-19 10:55:37 +02001033 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001034 return LY_EVALID;
1035 }
1036
1037 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001038 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039
Radek Krejci96e48da2020-09-04 13:18:06 +02001040 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001041 mod->version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001042 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001043 mod->version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001044 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001045 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001046 free(buf);
1047 return LY_EVALID;
1048 }
1049 free(buf);
1050
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001051 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001052 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001053 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001054 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_YANG_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055 break;
1056 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001057 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 return LY_EVALID;
1059 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001060 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001062
1063cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064 return ret;
1065}
1066
Michal Vaskoea5abea2018-09-18 13:10:54 +02001067/**
1068 * @brief Parse the belongs-to statement.
1069 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001070 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001071 * @param[in,out] submod Submodule to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001072 * @return LY_ERR values.
1073 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001074static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001075parse_belongsto(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001076{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001077 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001079 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001080 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081
Michal Vasko193dacd2022-10-13 08:43:05 +02001082 if (submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001083 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001084 return LY_EVALID;
1085 }
1086
Michal Vaskoc3781c32020-10-06 14:04:08 +02001087 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001088 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001089 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001090 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Submodule \"belongs-to\" value \"%.*s\" does not match its module name \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001091 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001092 free(buf);
1093 return LY_EVALID;
1094 }
1095 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001096
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001097 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001098 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001099 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001100 LY_CHECK_RET(parse_text_field(ctx, submod->prefix, LY_STMT_PREFIX, 0, &submod->prefix, Y_IDENTIF_ARG, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001102 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001103 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_BELONGS_TO, 0, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104 break;
1105 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001106 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 return LY_EVALID;
1108 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001109 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001111
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112 /* mandatory substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +02001113 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001114 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001115 return LY_EVALID;
1116 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001117
1118cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001119 return ret;
1120}
1121
Michal Vaskoea5abea2018-09-18 13:10:54 +02001122/**
1123 * @brief Parse the revision-date statement.
1124 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001125 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001126 * @param[in,out] rev Buffer to store the parsed value in.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001127 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001128 * @return LY_ERR values.
1129 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001131parse_revisiondate(struct lysp_yang_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001133 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001134 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001135 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001136 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137
1138 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001139 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001140 return LY_EVALID;
1141 }
1142
1143 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001144 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145
1146 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001147 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001148 free(buf);
1149 return LY_EVALID;
1150 }
1151
1152 /* store value and spend buf if allocated */
1153 strncpy(rev, word, word_len);
1154 free(buf);
1155
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001156 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001158 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001159 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160 break;
1161 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001162 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001163 return LY_EVALID;
1164 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001165 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001166 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001167
1168cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169 return ret;
1170}
1171
Michal Vaskoea5abea2018-09-18 13:10:54 +02001172/**
1173 * @brief Parse the include statement.
1174 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001175 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001176 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001177 * @param[in,out] includes Parsed includes to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001178 * @return LY_ERR values.
1179 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001181parse_include(struct lysp_yang_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182{
Radek Krejcid33273d2018-10-25 14:55:52 +02001183 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001185 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001186 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001187 struct lysp_include *inc;
1188
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001189 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190
1191 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001192 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001193
Michal Vasko12ef5362022-09-16 15:13:58 +02001194 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001195
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001196 /* submodules share the namespace with the module names, so there must not be
1197 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001198 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001199 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001200 return LY_EVALID;
1201 }
1202
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001203 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001204 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001205 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001206 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001207 LY_CHECK_RET(parse_text_field(ctx, inc->dsc, LY_STMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001209 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001210 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001211 LY_CHECK_RET(parse_text_field(ctx, inc->ref, LY_STMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001213 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001214 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001216 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001217 LY_CHECK_RET(parse_ext(ctx, word, word_len, inc, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218 break;
1219 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001220 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221 return LY_EVALID;
1222 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001223 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001224 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001225
1226cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227 return ret;
1228}
1229
Michal Vaskoea5abea2018-09-18 13:10:54 +02001230/**
1231 * @brief Parse the import statement.
1232 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001233 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001234 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001235 * @param[in,out] imports Parsed imports to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001236 * @return LY_ERR values.
1237 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001238static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001239parse_import(struct lysp_yang_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001241 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001243 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001244 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 struct lysp_import *imp;
1246
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001247 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248
1249 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001250 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001251 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001253 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001255 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001256 LY_CHECK_RET(parse_text_field(ctx, imp->prefix, LY_STMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskod0625d72022-10-06 15:02:50 +02001257 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001258 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001259 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001260 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001261 LY_CHECK_RET(parse_text_field(ctx, imp->dsc, LY_STMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001262 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001263 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001264 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001265 LY_CHECK_RET(parse_text_field(ctx, imp->ref, LY_STMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001267 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001268 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001269 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001270 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001271 LY_CHECK_RET(parse_ext(ctx, word, word_len, imp, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001272 break;
1273 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001274 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001275 return LY_EVALID;
1276 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001277 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001278 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001279
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001281 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001282
Michal Vasko12ef5362022-09-16 15:13:58 +02001283cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001284 return ret;
1285}
1286
Michal Vaskoea5abea2018-09-18 13:10:54 +02001287/**
1288 * @brief Parse the revision statement.
1289 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001290 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001291 * @param[in,out] revs Parsed revisions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001292 * @return LY_ERR values.
1293 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001294static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001295parse_revision(struct lysp_yang_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001297 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001298 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001299 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001300 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001301 struct lysp_revision *rev;
1302
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001303 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001304
1305 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001306 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001307
1308 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001309 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001310 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001311 return LY_EVALID;
1312 }
1313
Radek Krejcib7db73a2018-10-24 14:18:40 +02001314 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315 free(buf);
1316
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001317 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001319 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001320 LY_CHECK_RET(parse_text_field(ctx, rev->dsc, LY_STMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001322 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001323 LY_CHECK_RET(parse_text_field(ctx, rev->ref, LY_STMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001325 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001326 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327 break;
1328 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001329 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330 return LY_EVALID;
1331 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001332 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001333 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001334
1335cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336 return ret;
1337}
1338
Michal Vaskoea5abea2018-09-18 13:10:54 +02001339/**
1340 * @brief Parse a generic text field that can have more instances such as base.
1341 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001342 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001343 * @param[in] parent Current statement parent.
1344 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001345 * @param[in,out] texts Parsed values to add to.
1346 * @param[in] arg Type of the expected argument.
1347 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001348 * @return LY_ERR values.
1349 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001350static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001351parse_text_fields(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, const char ***texts, enum yang_arg arg,
Radek Krejci0f969882020-08-21 16:56:47 +02001352 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001354 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001355 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001356 const char **item;
1357 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001358 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001359
1360 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001361 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362
1363 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001364 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001365
Michal Vasko12ef5362022-09-16 15:13:58 +02001366 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001367 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001369 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001370 LY_CHECK_RET(parse_ext(ctx, word, word_len, *texts, parent_stmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371 break;
1372 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001373 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374 return LY_EVALID;
1375 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001376 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001377 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001378
1379cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001380 return ret;
1381}
1382
Michal Vaskoea5abea2018-09-18 13:10:54 +02001383/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001384 * @brief Parse a generic text field that can have more instances such as base.
1385 *
1386 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001387 * @param[in] parent_stmt Type of statement stored in @p qnames.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001388 * @param[in,out] qnames Parsed qnames to add to.
1389 * @param[in] arg Type of the expected argument.
1390 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001391 * @return LY_ERR values.
1392 */
1393static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001394parse_qnames(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_qname **qnames, enum yang_arg arg,
1395 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +02001396{
1397 LY_ERR ret = LY_SUCCESS;
1398 char *buf, *word;
1399 struct lysp_qname *item;
1400 size_t word_len;
1401 enum ly_stmt kw;
1402
1403 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001404 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001405
1406 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001407 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001408
Michal Vasko12ef5362022-09-16 15:13:58 +02001409 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001410 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001411 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001412 switch (kw) {
1413 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001414 LY_CHECK_RET(parse_ext(ctx, word, word_len, *qnames, parent_stmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001415 break;
1416 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001417 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001418 return LY_EVALID;
1419 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001420 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001421 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001422
1423cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001424 return ret;
1425}
1426
1427/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001428 * @brief Parse the config statement.
1429 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001430 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001431 * @param[in,out] flags Flags to add to.
1432 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001433 * @return LY_ERR values.
1434 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001436parse_config(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001438 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001439 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001440 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001441 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001442
1443 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001444 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001445 return LY_EVALID;
1446 }
1447
1448 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001449 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450
Radek Krejcif13b87b2020-12-01 22:02:17 +01001451 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001452 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001453 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001454 *flags |= LYS_CONFIG_R;
1455 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001456 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001457 free(buf);
1458 return LY_EVALID;
1459 }
1460 free(buf);
1461
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001462 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001464 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001465 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466 break;
1467 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001468 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001469 return LY_EVALID;
1470 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001471 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001473
1474cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475 return ret;
1476}
1477
Michal Vaskoea5abea2018-09-18 13:10:54 +02001478/**
1479 * @brief Parse the mandatory statement.
1480 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001481 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001482 * @param[in,out] flags Flags to add to.
1483 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001484 * @return LY_ERR values.
1485 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001487parse_mandatory(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001489 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001490 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001491 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001492 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493
1494 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001495 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 return LY_EVALID;
1497 }
1498
1499 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001500 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501
Radek Krejcif13b87b2020-12-01 22:02:17 +01001502 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001504 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505 *flags |= LYS_MAND_FALSE;
1506 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001507 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 free(buf);
1509 return LY_EVALID;
1510 }
1511 free(buf);
1512
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001513 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001515 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001516 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 break;
1518 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001519 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 return LY_EVALID;
1521 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001522 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001523 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001524
1525cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526 return ret;
1527}
1528
Michal Vaskoea5abea2018-09-18 13:10:54 +02001529/**
1530 * @brief Parse a restriction such as range or length.
1531 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001532 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001533 * @param[in] restr_kw Type of this particular restriction.
1534 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001535 * @return LY_ERR values.
1536 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001538parse_restr(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001539{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001540 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001541 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001542 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001543 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544
1545 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001546 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547
Michal Vasko193dacd2022-10-13 08:43:05 +02001548 CHECK_NONEMPTY(ctx, word_len, lyplg_ext_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001549 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001550 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001551 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001553 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001554 LY_CHECK_RET(parse_text_field(ctx, restr->dsc, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001556 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001557 LY_CHECK_RET(parse_text_field(ctx, restr->ref, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001559 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02001560 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG,
1561 &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001562 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001563 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001564 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001566 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001567 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 break;
1569 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001570 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 return LY_EVALID;
1572 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001573 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001575
1576cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 return ret;
1578}
1579
Michal Vaskoea5abea2018-09-18 13:10:54 +02001580/**
1581 * @brief Parse a restriction that can have more instances such as must.
1582 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001583 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001584 * @param[in] restr_kw Type of this particular restriction.
1585 * @param[in,out] restrs Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001586 * @return LY_ERR values.
1587 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001589parse_restrs(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001590{
1591 struct lysp_restr *restr;
1592
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001593 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001594 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001595}
1596
Michal Vaskoea5abea2018-09-18 13:10:54 +02001597/**
1598 * @brief Parse the status statement.
1599 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001600 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001601 * @param[in,out] flags Flags to add to.
1602 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001603 * @return LY_ERR values.
1604 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001605static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001606parse_status(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001608 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001609 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001610 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001611 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612
1613 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001614 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 return LY_EVALID;
1616 }
1617
1618 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001619 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001620
Radek Krejcif13b87b2020-12-01 22:02:17 +01001621 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001623 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001625 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001626 *flags |= LYS_STATUS_OBSLT;
1627 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001628 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001629 free(buf);
1630 return LY_EVALID;
1631 }
1632 free(buf);
1633
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001634 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001635 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001636 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001637 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 break;
1639 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001640 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641 return LY_EVALID;
1642 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001643 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001645
1646cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647 return ret;
1648}
1649
Michal Vaskoea5abea2018-09-18 13:10:54 +02001650/**
1651 * @brief Parse the when statement.
1652 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001653 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001654 * @param[in,out] when_p When pointer to parse to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001655 * @return LY_ERR values.
1656 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001657LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001658parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001659{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001660 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001662 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001663 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001665 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666
1667 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001668 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001669 return LY_EVALID;
1670 }
1671
1672 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001673 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001674
1675 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001676 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001677 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001678 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001680 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001682 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001683 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->dsc, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts),
1684 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001685 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001686 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001687 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->ref, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts),
1688 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001689 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001690 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001691 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, *when_p, LY_STMT_WHEN, 0, &when->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001692 break;
1693 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001694 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001695 ret = LY_EVALID;
1696 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001698 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001700
1701cleanup:
1702 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001703 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001704 free(when);
1705 } else {
1706 *when_p = when;
1707 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001708 return ret;
1709}
1710
Michal Vaskoea5abea2018-09-18 13:10:54 +02001711/**
1712 * @brief Parse the anydata or anyxml statement.
1713 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001714 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001715 * @param[in] any_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001716 * @param[in] parent Node parent.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001717 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001718 * @return LY_ERR values.
1719 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001720LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001721parse_any(struct lysp_yang_ctx *ctx, enum ly_stmt any_kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001722{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001723 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001725 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001726 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001727 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001728
David Sedlák60adc092019-08-06 15:57:02 +02001729 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001730 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001731
Radek Krejci39b7fc22021-02-26 23:29:18 +01001732 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001733 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001736 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001737 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738
1739 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001740 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001741 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001742 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001743 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001744 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001745 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001746 LY_CHECK_RET(parse_text_field(ctx, any->dsc, LY_STMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001747 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001748 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001749 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001750 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001751 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001752 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001754 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001755 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001757 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001758 LY_CHECK_RET(parse_text_field(ctx, any->ref, LY_STMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001760 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001761 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001763 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001764 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001766 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001767 LY_CHECK_RET(parse_ext(ctx, word, word_len, any, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768 break;
1769 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001770 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771 return LY_EVALID;
1772 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001773 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001775
1776cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 return ret;
1778}
1779
Michal Vaskoea5abea2018-09-18 13:10:54 +02001780/**
1781 * @brief Parse the value or position statement. Substatement of type enum statement.
1782 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001783 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001784 * @param[in] val_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001785 * @param[in,out] enm Structure to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001786 * @return LY_ERR values.
1787 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001788LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001789parse_type_enum_value_pos(struct lysp_yang_ctx *ctx, enum ly_stmt val_kw, struct lysp_type_enum *enm)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001790{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001791 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001792 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001793 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001794 long long int num = 0;
1795 unsigned long long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001796 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797
Michal Vasko193dacd2022-10-13 08:43:05 +02001798 if (enm->flags & LYS_SET_VALUE) {
1799 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001800 ret = LY_EVALID;
1801 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001802 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001803 enm->flags |= LYS_SET_VALUE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001804
1805 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001806 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001807
Radek Krejcid6b76452019-09-03 17:03:03 +02001808 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001809 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001810 ret = LY_EVALID;
1811 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001812 }
1813
1814 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001815 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001816 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001817 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001818 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001819 ret = LY_EVALID;
1820 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001821 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001822 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001823 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001824 if (unum > UINT64_C(4294967295)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001825 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001826 ret = LY_EVALID;
1827 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001828 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001829 }
1830 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001831 if ((size_t)(ptr - word) != word_len) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001832 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001833 ret = LY_EVALID;
1834 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835 }
1836 if (errno == ERANGE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001837 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001838 ret = LY_EVALID;
1839 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001841 if (val_kw == LY_STMT_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001842 enm->value = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02001844 enm->value = unum;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001845 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001847 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001849 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001850 ret = parse_ext(ctx, word, word_len, enm, val_kw, 0, &enm->exts);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001851 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001852 break;
1853 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001854 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001855 ret = LY_EVALID;
1856 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001857 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001858 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001859 }
Radek Krejci8b764662018-11-14 14:15:13 +01001860
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001861cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001862 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001863 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001864}
1865
Michal Vaskoea5abea2018-09-18 13:10:54 +02001866/**
1867 * @brief Parse the enum or bit statement. Substatement of type statement.
1868 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001869 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001870 * @param[in] enum_kw Type of this particular keyword.
1871 * @param[in,out] enums Enums or bits to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001872 * @return LY_ERR values.
1873 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001875parse_type_enum(struct lysp_yang_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001877 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001878 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001879 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001880 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881 struct lysp_type_enum *enm;
1882
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001883 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001884
1885 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001886 LY_CHECK_RET(get_argument(ctx, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejcid6b76452019-09-03 17:03:03 +02001887 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001888 ret = lysp_check_enum_name((struct lysp_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001889 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001890 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001891
Michal Vasko12ef5362022-09-16 15:13:58 +02001892 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001893 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(enum_kw), enm->name);
Radek Krejci8b764662018-11-14 14:15:13 +01001894
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001895 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001897 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001898 LY_CHECK_RET(parse_text_field(ctx, enm->dsc, LY_STMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001900 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001901 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001902 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001903 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001904 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001905 LY_CHECK_RET(parse_text_field(ctx, enm->ref, LY_STMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001907 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001908 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001909 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001910 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001911 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1912 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1913 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
David Sedlák9fb515f2019-07-11 10:33:58 +02001914 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001915 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001916 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1917 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1918 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001920 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001921 LY_CHECK_RET(parse_ext(ctx, word, word_len, enm, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922 break;
1923 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001924 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925 return LY_EVALID;
1926 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001927 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001929
1930cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931 return ret;
1932}
1933
Michal Vaskoea5abea2018-09-18 13:10:54 +02001934/**
1935 * @brief Parse the fraction-digits statement. Substatement of type statement.
1936 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001937 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001938 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001939 * @return LY_ERR values.
1940 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001941static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001942parse_type_fracdigits(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001943{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001944 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001945 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001946 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001947 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001948 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949
Michal Vasko193dacd2022-10-13 08:43:05 +02001950 if (type->fraction_digits) {
David Sedlákb3077192019-06-19 10:55:37 +02001951 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001952 return LY_EVALID;
1953 }
1954
1955 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001956 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001957
1958 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001959 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001960 ret = LY_EVALID;
1961 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962 }
1963
1964 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001965 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001966 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001967 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001968 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001969 ret = LY_EVALID;
1970 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001971 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001972 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001973 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001974 ret = LY_EVALID;
1975 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001977 type->fraction_digits = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001979 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001980 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001981 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001982 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, type, LY_STMT_FRACTION_DIGITS, 0, &type->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001983 break;
1984 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001985 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001986 ret = LY_EVALID;
1987 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001988 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001989 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001991
1992cleanup:
1993 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001994 return ret;
1995}
1996
Michal Vaskoea5abea2018-09-18 13:10:54 +02001997/**
1998 * @brief Parse the require-instance statement. Substatement of type statement.
1999 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002000 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002001 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002002 * @return LY_ERR values.
2003 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002004static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002005parse_type_reqinstance(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002007 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002008 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002009 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002010 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011
Michal Vasko193dacd2022-10-13 08:43:05 +02002012 if (type->flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002013 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 return LY_EVALID;
2015 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002016 type->flags |= LYS_SET_REQINST;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017
2018 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002019 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020
Radek Krejcif13b87b2020-12-01 22:02:17 +01002021 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002022 type->require_instance = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002023 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002024 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002025 ret = LY_EVALID;
2026 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002029 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002031 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002032 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, type, LY_STMT_REQUIRE_INSTANCE, 0, &type->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 break;
2034 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002035 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002036 ret = LY_EVALID;
2037 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002039 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002040 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002041
2042cleanup:
2043 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044 return ret;
2045}
2046
Michal Vaskoea5abea2018-09-18 13:10:54 +02002047/**
2048 * @brief Parse the modifier statement. Substatement of type pattern statement.
2049 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002050 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002051 * @param[in,out] restr Restriction to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002052 * @return LY_ERR values.
2053 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002054static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002055parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002057 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002058 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002059 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002060 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002061
Michal Vasko193dacd2022-10-13 08:43:05 +02002062 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002063 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064 return LY_EVALID;
2065 }
2066
2067 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002068 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002069
Radek Krejcif13b87b2020-12-01 22:02:17 +01002070 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002071 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002072 ret = LY_EVALID;
2073 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002075
2076 /* replace the value in the dictionary */
Michal Vasko193dacd2022-10-13 08:43:05 +02002077 buf = malloc(strlen(restr->arg.str) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002078 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02002079 strcpy(buf, restr->arg.str);
2080 lydict_remove(PARSER_CTX(ctx), restr->arg.str);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081
Radek Krejcif13b87b2020-12-01 22:02:17 +01002082 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2083 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko193dacd2022-10-13 08:43:05 +02002084 LY_CHECK_GOTO(ret = lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str), cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002085 buf = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002087 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002089 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002090 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, restr, LY_STMT_MODIFIER, 0, &restr->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002091 break;
2092 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002093 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002094 ret = LY_EVALID;
2095 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002097 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002099
2100cleanup:
2101 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 return ret;
2103}
2104
Michal Vaskoea5abea2018-09-18 13:10:54 +02002105/**
2106 * @brief Parse the pattern statement. Substatement of type statement.
2107 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002108 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002109 * @param[in,out] patterns Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002110 * @return LY_ERR values.
2111 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002113parse_type_pattern(struct lysp_yang_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002115 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002117 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002118 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002119 struct lysp_restr *restr;
2120
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002121 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122
2123 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002124 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125
2126 /* add special meaning first byte */
2127 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002128 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 word = buf;
2130 } else {
2131 buf = malloc(word_len + 2);
2132 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002133 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002134 if (word_len) {
2135 memmove(buf + 1, word, word_len);
2136 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002137 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002138 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002139 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002140 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002142 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002144 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002145 LY_CHECK_RET(parse_text_field(ctx, restr->dsc, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002147 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002148 LY_CHECK_RET(parse_text_field(ctx, restr->ref, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002150 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02002151 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002153 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002154 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002156 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002157 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko193dacd2022-10-13 08:43:05 +02002158 LY_CHECK_RET(parse_type_pattern_modifier(ctx, restr));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002160 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002161 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 break;
2163 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002164 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165 return LY_EVALID;
2166 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002167 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002169
2170cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 return ret;
2172}
2173
Michal Vaskoea5abea2018-09-18 13:10:54 +02002174/**
2175 * @brief Parse the type statement.
2176 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002177 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002178 * @param[in,out] type Type to wrote to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002179 * @return LY_ERR values.
2180 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002182parse_type(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002184 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002186 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002187 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002188 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 struct lysp_type *nest_type;
2190
2191 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002192 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002193 return LY_EVALID;
2194 }
2195
2196 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002197 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002198 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002199
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002200 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002201 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002202
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002203 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002204 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002205 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002206 LY_CHECK_RET(parse_text_fields(ctx, LY_STMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002207 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002209 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002210 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002211 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002213 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002214 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002215 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002217 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002218 LY_CHECK_RET(parse_type_fracdigits(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002219 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002221 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002223 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224 return LY_EVALID;
2225 }
2226 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002227 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228
Radek Krejci33090f92020-12-17 20:12:46 +01002229 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002230 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002232 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002233 if (type->path) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002234 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002235 return LY_EVALID;
2236 }
2237
aPiecek4bb1e372021-05-07 11:01:00 +02002238 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2239 * corresponding structure, so in case of failure, the lysp_module_free function will take
2240 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2241 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2242 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002243 LY_CHECK_ERR_RET(ret = parse_text_field(ctx, type, LY_STMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts),
aPiecek4bb1e372021-05-07 11:01:00 +02002244 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002245 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002246 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002247 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002248 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002249 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002250 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002252 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002253 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002254 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002256 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002257 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002258 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002259 return LY_EVALID;
2260 }
2261 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002262 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263
Radek Krejci33090f92020-12-17 20:12:46 +01002264 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002265 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002267 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002268 LY_CHECK_RET(parse_type_reqinstance(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002269 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002271 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002272 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002273 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002274 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002275 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002276 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002277 LY_CHECK_RET(parse_ext(ctx, word, word_len, type, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 break;
2279 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002280 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281 return LY_EVALID;
2282 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002283 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002285
2286cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002287 return ret;
2288}
2289
Michal Vaskoea5abea2018-09-18 13:10:54 +02002290/**
2291 * @brief Parse the leaf statement.
2292 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002293 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002294 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002295 * @return LY_ERR values.
2296 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002297LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002298parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002300 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002302 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002303 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304 struct lysp_node_leaf *leaf;
2305
David Sedlák60adc092019-08-06 15:57:02 +02002306 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002307 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002309 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002312 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002313 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314
2315 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002316 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002318 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002319 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002321 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002322 LY_CHECK_RET(parse_text_field(ctx, &leaf->dflt, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002323 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002325 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002326 LY_CHECK_RET(parse_text_field(ctx, leaf->dsc, LY_STMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002328 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002329 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002331 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002332 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002334 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002335 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002336 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002337 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002338 LY_CHECK_RET(parse_text_field(ctx, leaf->ref, LY_STMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002339 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002340 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002341 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002342 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002343 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002344 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002345 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002346 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002347 LY_CHECK_RET(parse_text_field(ctx, leaf->units, LY_STMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002349 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002350 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002351 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002352 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002353 LY_CHECK_RET(parse_ext(ctx, word, word_len, leaf, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002354 break;
2355 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002356 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357 return LY_EVALID;
2358 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002359 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002361
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362 /* mandatory substatements */
2363 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002364 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365 return LY_EVALID;
2366 }
2367
Michal Vasko12ef5362022-09-16 15:13:58 +02002368cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 return ret;
2370}
2371
Michal Vaskoea5abea2018-09-18 13:10:54 +02002372/**
2373 * @brief Parse the max-elements statement.
2374 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002375 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002376 * @param[in,out] max Value to write to.
2377 * @param[in,out] flags Flags to write to.
2378 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002379 * @return LY_ERR values.
2380 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002381LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002382parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002384 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002385 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002386 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002387 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002388 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002389
2390 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002391 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002392 return LY_EVALID;
2393 }
2394 *flags |= LYS_SET_MAX;
2395
2396 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002397 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002398
2399 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002400 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002401 ret = LY_EVALID;
2402 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 }
2404
Radek Krejci7f9b6512019-09-18 13:11:09 +02002405 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002407 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002409 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002410 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002411 ret = LY_EVALID;
2412 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413 }
2414 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002415 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002416 ret = LY_EVALID;
2417 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 }
2419
2420 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002421 } else {
2422 /* unbounded */
2423 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002424 }
2425 free(buf);
2426
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002427 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002429 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002430 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, max, LY_STMT_MAX_ELEMENTS, 0, exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002431 break;
2432 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002433 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002434 ret = LY_EVALID;
2435 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002437 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002439
2440cleanup:
2441 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002442 return ret;
2443}
2444
Michal Vaskoea5abea2018-09-18 13:10:54 +02002445/**
2446 * @brief Parse the min-elements statement.
2447 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002448 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002449 * @param[in,out] min Value to write to.
2450 * @param[in,out] flags Flags to write to.
2451 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002452 * @return LY_ERR values.
2453 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002454LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002455parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002456{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002457 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002458 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002459 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002460 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002461 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002462
2463 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002464 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465 return LY_EVALID;
2466 }
2467 *flags |= LYS_SET_MIN;
2468
2469 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002470 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002471
2472 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002473 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002474 ret = LY_EVALID;
2475 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002476 }
2477
2478 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002479 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002481 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002482 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002483 ret = LY_EVALID;
2484 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002485 }
2486 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002487 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002488 ret = LY_EVALID;
2489 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490 }
2491 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002492
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002493 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002494 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002495 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002496 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, min, LY_STMT_MIN_ELEMENTS, 0, exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497 break;
2498 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002499 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002500 ret = LY_EVALID;
2501 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002503 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002505
2506cleanup:
2507 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508 return ret;
2509}
2510
Michal Vaskoea5abea2018-09-18 13:10:54 +02002511/**
2512 * @brief Parse the ordered-by statement.
2513 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002514 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002515 * @param[in,out] llist List or leaf-list to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002516 * @return LY_ERR values.
2517 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002519parse_orderedby(struct lysp_yang_ctx *ctx, struct lysp_node *llist)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002520{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002521 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002522 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002523 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002524 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525
Michal Vasko193dacd2022-10-13 08:43:05 +02002526 if (llist->flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002527 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 return LY_EVALID;
2529 }
2530
2531 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002532 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533
Radek Krejcif13b87b2020-12-01 22:02:17 +01002534 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002535 llist->flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002536 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002537 llist->flags |= LYS_ORDBY_USER;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002539 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002540 ret = LY_EVALID;
2541 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002544 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002545 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002546 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002547 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, llist, LY_STMT_ORDERED_BY, 0, &llist->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002548 break;
2549 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002550 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002551 ret = LY_EVALID;
2552 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002553 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002554 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002556
2557cleanup:
2558 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002559 return ret;
2560}
2561
Michal Vaskoea5abea2018-09-18 13:10:54 +02002562/**
2563 * @brief Parse the leaf-list statement.
2564 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002565 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002566 * @param[in,out] siblings Siblings to add to.
2567 *
2568 * @return LY_ERR values.
2569 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002570LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002571parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002573 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002575 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002576 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577 struct lysp_node_leaflist *llist;
2578
David Sedlák60adc092019-08-06 15:57:02 +02002579 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002580 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002582 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002583
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002585 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002586 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587
2588 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002589 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002591 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002592 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002594 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002595 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002596 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002598 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002599 LY_CHECK_RET(parse_text_field(ctx, llist->dsc, LY_STMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002601 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002602 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002604 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002605 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002607 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002608 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002610 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002611 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002613 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02002614 LY_CHECK_RET(parse_orderedby(ctx, &llist->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002616 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002617 LY_CHECK_RET(parse_text_field(ctx, llist->ref, LY_STMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002619 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002620 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002622 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002623 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002626 LY_CHECK_RET(parse_text_field(ctx, llist->units, LY_STMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002629 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002631 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002632 LY_CHECK_RET(parse_ext(ctx, word, word_len, llist, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 break;
2634 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002635 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 return LY_EVALID;
2637 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002638 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002640
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641 /* mandatory substatements */
2642 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002643 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644 return LY_EVALID;
2645 }
2646
Michal Vasko12ef5362022-09-16 15:13:58 +02002647cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002648 return ret;
2649}
2650
Michal Vaskoea5abea2018-09-18 13:10:54 +02002651/**
2652 * @brief Parse the refine statement.
2653 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002654 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002655 * @param[in,out] refines Refines to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002656 * @return LY_ERR values.
2657 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002659parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002661 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002663 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002664 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002665 struct lysp_refine *rf;
2666
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002667 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002668
2669 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002670 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002671 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002672 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002674 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002676 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002677 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002679 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002680 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002682 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002683 LY_CHECK_RET(parse_text_field(ctx, rf->dsc, LY_STMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002685 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002686 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002687 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002689 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002690 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002692 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002693 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002695 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002696 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002698 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002699 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002701 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002702 LY_CHECK_RET(parse_text_field(ctx, rf->ref, LY_STMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002704 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002705 LY_CHECK_RET(parse_text_field(ctx, rf->presence, LY_STMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002707 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002708 LY_CHECK_RET(parse_ext(ctx, word, word_len, rf, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002709 break;
2710 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002711 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712 return LY_EVALID;
2713 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002714 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002715 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002716
2717cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718 return ret;
2719}
2720
Michal Vaskoea5abea2018-09-18 13:10:54 +02002721/**
2722 * @brief Parse the typedef statement.
2723 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002724 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002725 * @param[in,out] typedefs Typedefs to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002726 * @return LY_ERR values.
2727 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002729parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002730{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002731 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002733 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002734 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 struct lysp_tpdf *tpdf;
2736
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002737 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738
2739 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002740 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002741 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002742
2743 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002744 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002746 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002747 LY_CHECK_RET(parse_text_field(ctx, &tpdf->dflt, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002748 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002750 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002751 LY_CHECK_RET(parse_text_field(ctx, tpdf->dsc, LY_STMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002753 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002754 LY_CHECK_RET(parse_text_field(ctx, tpdf->ref, LY_STMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002756 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002757 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002759 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002760 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002762 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002763 LY_CHECK_RET(parse_text_field(ctx, tpdf->units, LY_STMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002765 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002766 LY_CHECK_RET(parse_ext(ctx, word, word_len, tpdf, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767 break;
2768 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002769 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 return LY_EVALID;
2771 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002772 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002774
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 /* mandatory substatements */
2776 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002777 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 return LY_EVALID;
2779 }
2780
Radek Krejcibbe09a92018-11-08 09:36:54 +01002781 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002782 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002783 assert(ctx->main_ctx);
2784 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002785 }
2786
Michal Vasko12ef5362022-09-16 15:13:58 +02002787cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 return ret;
2789}
2790
Michal Vaskoea5abea2018-09-18 13:10:54 +02002791/**
2792 * @brief Parse the input or output statement.
2793 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002794 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002795 * @param[in] kw Type of this particular keyword
2796 * @param[in,out] inout_p Input/output pointer to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002797 * @return LY_ERR values.
2798 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002800parse_inout(struct lysp_yang_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002801 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002803 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002804 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002805 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002806 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002807 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002809 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002810 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811 return LY_EVALID;
2812 }
2813
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002814 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002815 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2816 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002817 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818
2819 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002820 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002821 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002822 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002823 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002824 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002825 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002826 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002828 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002829 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002831 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002832 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002833 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002834 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002835 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002837 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002838 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002840 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002841 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002843 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002844 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002846 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002847 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002849 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002850 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002851 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002852 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002853 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002854 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002856 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002857 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_p, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 break;
2859 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002860 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 return LY_EVALID;
2862 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002863 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002865
Radek Krejci01180ac2021-01-27 08:48:22 +01002866 if (!inout_p->child) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002867 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", lyplg_ext_stmt2str(inout_kw));
Michal Vaskob83af8a2020-01-06 09:49:22 +01002868 return LY_EVALID;
2869 }
2870
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002871cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002872 return ret;
2873}
2874
Michal Vaskoea5abea2018-09-18 13:10:54 +02002875/**
2876 * @brief Parse the action statement.
2877 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002878 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002879 * @param[in,out] actions Actions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002880 * @return LY_ERR values.
2881 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002882LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002883parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002884{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002885 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002886 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002887 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002888 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002889 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890
Radek Krejci2a9fc652021-01-22 17:44:34 +01002891 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892
2893 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002894 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002895 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002896 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002897 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002899 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002900 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002901 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002902 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 +02002903 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002904 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002905 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002908 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 +02002909 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002910 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002911 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002912 break;
2913
Radek Krejcid6b76452019-09-03 17:03:03 +02002914 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002915 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002917 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002918 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 break;
2920
Radek Krejcid6b76452019-09-03 17:03:03 +02002921 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002922 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002924 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002925 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002926 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002927 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002928 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 +02002929 break;
2930 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002931 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932 return LY_EVALID;
2933 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002934 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002936
2937 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2938 if (!act->input.nodetype) {
2939 act->input.nodetype = LYS_INPUT;
2940 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002941 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002942 }
2943 if (!act->output.nodetype) {
2944 act->output.nodetype = LYS_OUTPUT;
2945 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002946 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002947 }
2948
Michal Vasko12ef5362022-09-16 15:13:58 +02002949cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002950 return ret;
2951}
2952
Michal Vaskoea5abea2018-09-18 13:10:54 +02002953/**
2954 * @brief Parse the notification statement.
2955 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002956 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002957 * @param[in,out] notifs Notifications to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002958 * @return LY_ERR values.
2959 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002960LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002961parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002962{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002963 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002965 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002966 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002967 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968
Radek Krejci2a9fc652021-01-22 17:44:34 +01002969 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970
2971 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002972 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002973 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002974 notif->nodetype = LYS_NOTIF;
2975 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002977 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002978 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002979 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002980 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 +02002981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002982 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002983 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002986 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 +02002987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002988 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002989 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 break;
2991
Radek Krejcid6b76452019-09-03 17:03:03 +02002992 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002993 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002994 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002995 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002996 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002999 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003002 LY_CHECK_RET(parse_container(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:
Radek Krejci01180ac2021-01-27 08:48:22 +01003005 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003008 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003011 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003013 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003014 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 break;
3016
Radek Krejcid6b76452019-09-03 17:03:03 +02003017 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003018 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003019 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003021 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003022 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003024 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003025 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003027 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003028 LY_CHECK_RET(parse_ext(ctx, word, word_len, notif, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003029 break;
3030 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003031 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003032 return LY_EVALID;
3033 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003034 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003036
Michal Vasko12ef5362022-09-16 15:13:58 +02003037cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003038 return ret;
3039}
3040
Michal Vaskoea5abea2018-09-18 13:10:54 +02003041/**
3042 * @brief Parse the grouping statement.
3043 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003044 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003045 * @param[in,out] groupings Groupings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003046 * @return LY_ERR values.
3047 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003048LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003049parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003051 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003053 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003054 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003055 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056
Radek Krejci2a9fc652021-01-22 17:44:34 +01003057 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058
3059 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003060 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003061 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003062 grp->nodetype = LYS_GROUPING;
3063 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003065 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003067 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003068 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 +02003069 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003070 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003071 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 +02003072 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003073 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003074 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 break;
3076
Radek Krejcid6b76452019-09-03 17:03:03 +02003077 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003078 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003079 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003080 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003081 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003082 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003083 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003084 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003087 LY_CHECK_RET(parse_container(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:
Radek Krejci01180ac2021-01-27 08:48:22 +01003090 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003093 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003096 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003098 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003099 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003100 break;
3101
Radek Krejcid6b76452019-09-03 17:03:03 +02003102 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003103 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003105 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003106 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003107 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003109 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003110 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003112 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003113 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003114 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003116 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003117 LY_CHECK_RET(parse_ext(ctx, word, word_len, grp, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003118 break;
3119 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003120 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121 return LY_EVALID;
3122 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003123 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003124 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003125
aPiecek63e080d2021-06-29 13:53:28 +02003126 /* store data for collision check */
3127 if (parent) {
3128 assert(ctx->main_ctx);
3129 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3130 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003131
Michal Vasko12ef5362022-09-16 15:13:58 +02003132cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133 return ret;
3134}
3135
Michal Vaskoea5abea2018-09-18 13:10:54 +02003136/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003137 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003138 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003139 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003140 * @param[in,out] augments Augments to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003141 * @return LY_ERR values.
3142 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003143LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003144parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003146 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003148 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003149 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003150 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151
Radek Krejci2a9fc652021-01-22 17:44:34 +01003152 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003153
3154 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003155 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003156 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003157 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003158 aug->nodetype = LYS_AUGMENT;
3159 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003161 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003163 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003164 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 +02003165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003166 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003167 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003170 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 +02003171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003172 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003173 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003175 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003176 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177 break;
3178
Radek Krejcid6b76452019-09-03 17:03:03 +02003179 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003180 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003181 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003182 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003183 LY_CHECK_RET(parse_any(ctx, kw, (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_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003186 LY_CHECK_RET(parse_case(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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003189 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003192 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01003195 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003198 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003201 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003203 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003204 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 break;
3206
Radek Krejcid6b76452019-09-03 17:03:03 +02003207 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003208 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003209 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003211 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003212 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003213 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003216 LY_CHECK_RET(parse_ext(ctx, word, word_len, aug, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 break;
3218 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003219 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 return LY_EVALID;
3221 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003222 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003224
Michal Vasko12ef5362022-09-16 15:13:58 +02003225cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003226 return ret;
3227}
3228
Michal Vaskoea5abea2018-09-18 13:10:54 +02003229/**
3230 * @brief Parse the uses statement.
3231 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003232 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003233 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003234 * @return LY_ERR values.
3235 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003236LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003237parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003238{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003239 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003241 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003242 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243 struct lysp_node_uses *uses;
3244
David Sedlák60adc092019-08-06 15:57:02 +02003245 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003246 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003248 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249
Michal Vasko7fbc8162018-09-17 10:35:16 +02003250 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003251 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003252 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253
3254 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003255 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003256 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003257 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003258 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 +02003259 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003260 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003261 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003264 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 +02003265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003266 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003267 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003269 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003270 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 break;
3272
Radek Krejcid6b76452019-09-03 17:03:03 +02003273 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003274 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003276 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003277 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003279 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003280 LY_CHECK_RET(parse_ext(ctx, word, word_len, uses, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003281 break;
3282 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003283 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003284 return LY_EVALID;
3285 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003286 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003287 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003288
Michal Vasko12ef5362022-09-16 15:13:58 +02003289cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003290 return ret;
3291}
3292
Michal Vaskoea5abea2018-09-18 13:10:54 +02003293/**
3294 * @brief Parse the case statement.
3295 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003296 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003297 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003298 * @return LY_ERR values.
3299 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003300LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003301parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003302{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003303 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003305 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003306 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 struct lysp_node_case *cas;
3308
David Sedlák60adc092019-08-06 15:57:02 +02003309 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003310 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003312 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003315 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003316 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317
3318 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003319 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003321 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003322 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 +02003323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003324 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003325 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003328 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 +02003329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003331 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003333 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003334 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 break;
3336
Radek Krejcid6b76452019-09-03 17:03:03 +02003337 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003338 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003339 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003340 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003341 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003344 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003347 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01003350 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003353 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003356 LY_CHECK_RET(parse_list(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_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003359 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003360 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003361 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003362 LY_CHECK_RET(parse_ext(ctx, word, word_len, cas, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 break;
3364 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003365 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 return LY_EVALID;
3367 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003368 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003370
3371cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 return ret;
3373}
3374
Michal Vaskoea5abea2018-09-18 13:10:54 +02003375/**
3376 * @brief Parse the choice statement.
3377 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003378 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003379 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003380 * @return LY_ERR values.
3381 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003382LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003383parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003385 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003386 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003387 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003388 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003389 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390
David Sedlák60adc092019-08-06 15:57:02 +02003391 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003392 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003393 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003394 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003397 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003398 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399
3400 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003401 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003403 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003404 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003406 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003407 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 +02003408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003409 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003410 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003412 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003413 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003415 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003416 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 +02003417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003418 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003419 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003421 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003422 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003424 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003425 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 +02003426 &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003427 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003428 break;
3429
Radek Krejcid6b76452019-09-03 17:03:03 +02003430 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003431 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003432 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003433 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003434 LY_CHECK_RET(parse_any(ctx, kw, (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_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003437 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003439 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003440 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003441 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003444 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01003447 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003450 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003453 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003455 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003456 LY_CHECK_RET(parse_ext(ctx, word, word_len, choice, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003457 break;
3458 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003459 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003460 return LY_EVALID;
3461 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003462 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003463 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003464
3465cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003466 return ret;
3467}
3468
Michal Vaskoea5abea2018-09-18 13:10:54 +02003469/**
3470 * @brief Parse the container statement.
3471 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003472 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003473 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003474 * @return LY_ERR values.
3475 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003476LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003477parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478{
3479 LY_ERR ret = 0;
3480 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003481 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003482 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 struct lysp_node_container *cont;
3484
David Sedlák60adc092019-08-06 15:57:02 +02003485 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003486 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003488 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003491 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003492 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493
3494 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003495 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003497 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003498 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003500 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003501 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 +02003502 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003503 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003504 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003507 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 +02003508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003509 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003510 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003512 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003513 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003514 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003515 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003516 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 +02003517 break;
3518
Radek Krejcid6b76452019-09-03 17:03:03 +02003519 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003520 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003521 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003522 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003523 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003526 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003529 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01003532 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003535 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003538 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003540 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003541 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 break;
3543
Radek Krejcid6b76452019-09-03 17:03:03 +02003544 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003545 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003547 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003548 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003549 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003550 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003551 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003552 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003554 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003555 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003557 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003558 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003559 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003561 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003562 LY_CHECK_RET(parse_ext(ctx, word, word_len, cont, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563 break;
3564 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003565 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 return LY_EVALID;
3567 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003568 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003570
Michal Vasko12ef5362022-09-16 15:13:58 +02003571cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003572 return ret;
3573}
3574
Michal Vaskoea5abea2018-09-18 13:10:54 +02003575/**
3576 * @brief Parse the list statement.
3577 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003578 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003579 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003580 * @return LY_ERR values.
3581 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003582LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003583parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003585 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003587 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003588 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 struct lysp_node_list *list;
3590
David Sedlák60adc092019-08-06 15:57:02 +02003591 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003592 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003594 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003597 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003598 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599
3600 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003601 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003603 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003604 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003606 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003607 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 +02003608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003609 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003610 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003613 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 +02003614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003615 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003616 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003618 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003619 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003621 case LY_STMT_KEY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003622 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 +02003623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003624 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003625 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003627 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003628 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003630 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003631 LY_CHECK_RET(parse_orderedby(ctx, &list->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003633 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003634 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 break;
3636
Radek Krejcid6b76452019-09-03 17:03:03 +02003637 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003638 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003639 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003640 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003641 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003644 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003647 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01003650 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003653 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003656 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003658 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003659 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660 break;
3661
Radek Krejcid6b76452019-09-03 17:03:03 +02003662 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003663 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003665 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003666 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003668 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003669 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003670 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003672 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003673 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003675 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003676 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003677 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003679 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003680 LY_CHECK_RET(parse_ext(ctx, word, word_len, list, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003681 break;
3682 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003683 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003684 return LY_EVALID;
3685 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003686 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003688
Michal Vasko12ef5362022-09-16 15:13:58 +02003689cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 return ret;
3691}
3692
Michal Vaskoea5abea2018-09-18 13:10:54 +02003693/**
3694 * @brief Parse the yin-element statement.
3695 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003696 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003697 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003698 * @return LY_ERR values.
3699 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003700static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003701parse_yinelement(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003702{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003703 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003705 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003706 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003707
Michal Vasko193dacd2022-10-13 08:43:05 +02003708 if (ext->flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003709 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003710 return LY_EVALID;
3711 }
3712
3713 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003714 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715
Radek Krejcif13b87b2020-12-01 22:02:17 +01003716 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003717 ext->flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003718 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003719 ext->flags |= LYS_YINELEM_FALSE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003721 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003722 free(buf);
3723 return LY_EVALID;
3724 }
3725 free(buf);
3726
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003727 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003729 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003730 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_YIN_ELEMENT, 0, &ext->exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003731 LY_CHECK_RET(ret);
3732 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003734 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 return LY_EVALID;
3736 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003737 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003738 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003739
3740cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003741 return ret;
3742}
3743
Michal Vaskoea5abea2018-09-18 13:10:54 +02003744/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003745 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003746 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003747 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003748 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003749 * @return LY_ERR values.
3750 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003751static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003752parse_argument(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003753{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003754 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003756 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003757 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003758
Michal Vasko193dacd2022-10-13 08:43:05 +02003759 if (ext->argname) {
David Sedlákb3077192019-06-19 10:55:37 +02003760 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 return LY_EVALID;
3762 }
3763
3764 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003765 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko193dacd2022-10-13 08:43:05 +02003766 INSERT_WORD_GOTO(ctx, buf, ext->argname, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003767
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003768 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003770 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003771 LY_CHECK_RET(parse_yinelement(ctx, ext));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003773 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003774 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_ARGUMENT, 0, &ext->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 break;
3776 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003777 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 return LY_EVALID;
3779 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003780 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003782
3783cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003784 return ret;
3785}
3786
Michal Vaskoea5abea2018-09-18 13:10:54 +02003787/**
3788 * @brief Parse the extension statement.
3789 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003790 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003791 * @param[in,out] extensions Extensions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003792 * @return LY_ERR values.
3793 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003795parse_extension(struct lysp_yang_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003796{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003797 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003799 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003800 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 struct lysp_ext *ex;
3802
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003803 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003804
3805 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003806 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003807 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003809 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003810 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003811 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003812 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 +02003813 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003814 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003815 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 +02003816 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003817 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003818 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003820 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003821 LY_CHECK_RET(parse_argument(ctx, ex));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003822 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003823 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003824 LY_CHECK_RET(parse_ext(ctx, word, word_len, ex, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003825 break;
3826 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003827 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 return LY_EVALID;
3829 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003830 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003831 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003832
3833cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 return ret;
3835}
3836
Michal Vaskoea5abea2018-09-18 13:10:54 +02003837/**
3838 * @brief Parse the deviate statement.
3839 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003840 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003841 * @param[in,out] deviates Deviates to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003842 * @return LY_ERR values.
3843 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003844LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003845parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003847 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003848 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003849 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003850 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003851 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003852 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003853 struct lysp_deviate_add *d_add = NULL;
3854 struct lysp_deviate_rpl *d_rpl = NULL;
3855 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003856 const char **d_units = NULL;
3857 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003858 struct lysp_restr **d_musts = NULL;
3859 uint16_t *d_flags = 0;
3860 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861
3862 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003863 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864
Radek Krejcif13b87b2020-12-01 22:02:17 +01003865 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003867 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003868 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003869 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003871 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872 dev_mod = LYS_DEV_DELETE;
3873 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003874 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003875 ret = LY_EVALID;
3876 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003878
3879 /* create structure */
3880 switch (dev_mod) {
3881 case LYS_DEV_NOT_SUPPORTED:
3882 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003883 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 break;
3885 case LYS_DEV_ADD:
3886 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003887 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888 d = (struct lysp_deviate *)d_add;
3889 d_units = &d_add->units;
3890 d_uniques = &d_add->uniques;
3891 d_dflts = &d_add->dflts;
3892 d_musts = &d_add->musts;
3893 d_flags = &d_add->flags;
3894 d_min = &d_add->min;
3895 d_max = &d_add->max;
3896 break;
3897 case LYS_DEV_REPLACE:
3898 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003899 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003900 d = (struct lysp_deviate *)d_rpl;
3901 d_units = &d_rpl->units;
3902 d_flags = &d_rpl->flags;
3903 d_min = &d_rpl->min;
3904 d_max = &d_rpl->max;
3905 break;
3906 case LYS_DEV_DELETE:
3907 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003908 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003909 d = (struct lysp_deviate *)d_del;
3910 d_units = &d_del->units;
3911 d_uniques = &d_del->uniques;
3912 d_dflts = &d_del->dflts;
3913 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914 break;
3915 default:
3916 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003917 LOGINT(PARSER_CTX(ctx));
3918 ret = LY_EINT;
3919 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003920 }
3921 d->mod = dev_mod;
3922
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003923 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003924 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003925 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003926 switch (dev_mod) {
3927 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003928 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003929 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003930 ret = LY_EVALID;
3931 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003932 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003933 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934 break;
3935 }
3936 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003937 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938 switch (dev_mod) {
3939 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02003940 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003941 ret = LY_EVALID;
3942 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003943 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003944 ret = parse_text_field(ctx, &d_rpl->dflt, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts);
3945 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003946 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 break;
3948 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003949 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 +02003950 break;
3951 }
3952 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003953 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 switch (dev_mod) {
3955 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003956 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003957 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003958 ret = LY_EVALID;
3959 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003961 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962 break;
3963 }
3964 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003965 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003966 switch (dev_mod) {
3967 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003968 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003969 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003970 ret = LY_EVALID;
3971 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003972 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003973 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003974 break;
3975 }
3976 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003977 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978 switch (dev_mod) {
3979 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003980 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003981 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003982 ret = LY_EVALID;
3983 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003985 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003986 break;
3987 }
3988 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003989 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990 switch (dev_mod) {
3991 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003992 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003993 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003994 ret = LY_EVALID;
3995 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003997 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003998 break;
3999 }
4000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004001 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004002 switch (dev_mod) {
4003 case LYS_DEV_NOT_SUPPORTED:
4004 case LYS_DEV_ADD:
4005 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004006 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004007 ret = LY_EVALID;
4008 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004009 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004010 if (d_rpl->type) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004011 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004012 ret = LY_EVALID;
4013 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004014 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004015 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004016 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4017 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004018 break;
4019 }
4020 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004021 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004022 switch (dev_mod) {
4023 case LYS_DEV_NOT_SUPPORTED:
4024 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004025 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004026 ret = LY_EVALID;
4027 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004028 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004029 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 +02004030 break;
4031 }
4032 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004033 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004034 switch (dev_mod) {
4035 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02004036 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004037 ret = LY_EVALID;
4038 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004039 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004040 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 +02004041 break;
4042 }
4043 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004044 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004045 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 +02004046 break;
4047 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004048 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004049 ret = LY_EVALID;
4050 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004051 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004052 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004054
4055cleanup:
4056 free(buf);
4057 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004058 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004059 free(d);
4060 } else {
4061 /* insert into siblings */
4062 LY_LIST_INSERT(deviates, d, next);
4063 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004064 return ret;
4065}
4066
Michal Vaskoea5abea2018-09-18 13:10:54 +02004067/**
4068 * @brief Parse the deviation statement.
4069 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004070 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004071 * @param[in,out] deviations Deviations to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004072 * @return LY_ERR values.
4073 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004074LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004075parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004077 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004079 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004080 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004081 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004082 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004083
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004084 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004085
4086 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004087 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004088 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004089 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004090
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004091 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004092 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004093 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004094 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 +02004095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004096 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004097 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004099 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004100 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 +02004101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004102 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004103 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 +02004104 break;
4105 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004106 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004107 ret = LY_EVALID;
4108 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004109 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004110 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004112
Michal Vasko7fbc8162018-09-17 10:35:16 +02004113 /* mandatory substatements */
4114 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004115 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004116 ret = LY_EVALID;
4117 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004118 }
4119
Michal Vasko12ef5362022-09-16 15:13:58 +02004120cleanup:
4121 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004122 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004123 LY_ARRAY_DECREMENT_FREE(*deviations);
4124 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004125 return ret;
4126}
4127
Michal Vaskoea5abea2018-09-18 13:10:54 +02004128/**
4129 * @brief Parse the feature statement.
4130 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004131 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004132 * @param[in,out] features Features to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004133 * @return LY_ERR values.
4134 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004135LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004136parse_feature(struct lysp_yang_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004137{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004138 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004140 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004141 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004142 struct lysp_feature *feat;
4143
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004144 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004145
4146 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004147 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004148 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004149
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004150 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004152 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004153 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 +02004154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004155 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004156 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004159 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 +02004160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004161 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004162 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004164 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004165 LY_CHECK_RET(parse_ext(ctx, word, word_len, feat, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 break;
4167 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004168 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004169 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004171 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004172 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004173
4174cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175 return ret;
4176}
4177
Michal Vaskoea5abea2018-09-18 13:10:54 +02004178/**
4179 * @brief Parse the identity statement.
4180 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004181 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004182 * @param[in,out] identities Identities to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004183 * @return LY_ERR values.
4184 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004185LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004186parse_identity(struct lysp_yang_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004188 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004189 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004190 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004191 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004192 struct lysp_ident *ident;
4193
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004194 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004195
4196 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004197 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004198 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004199
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004200 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004201 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004202 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004203 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 +02004204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004205 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004206 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004207 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004210 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 +02004211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004212 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004213 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004215 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004216 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004217 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 +01004218 return LY_EVALID;
4219 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004220 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 +02004221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004222 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004223 LY_CHECK_RET(parse_ext(ctx, word, word_len, ident, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 break;
4225 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004226 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 return LY_EVALID;
4228 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004229 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004230 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004231
4232cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004233 return ret;
4234}
4235
Michal Vaskoea5abea2018-09-18 13:10:54 +02004236/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004237 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004238 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004239 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004240 * @param[in,out] mod Module to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004241 * @return LY_ERR values.
4242 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004243LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004244parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004245{
4246 LY_ERR ret = 0;
4247 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004248 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004249 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004250 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004251 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004252
Michal Vaskoc3781c32020-10-06 14:04:08 +02004253 mod->is_submod = 0;
4254
4255 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004256 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004257 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004258
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004259 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004260
Radek Krejcie3846472018-10-15 15:24:51 +02004261#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004262 if (mod_stmt > SECTION) {\
4263 LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;\
4264 } mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004265
Michal Vasko7fbc8162018-09-17 10:35:16 +02004266 switch (kw) {
4267 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004268 case LY_STMT_NAMESPACE:
4269 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004270 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004272 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004273 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004274 break;
4275 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004276 case LY_STMT_INCLUDE:
4277 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004278 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004279 break;
4280 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004281 case LY_STMT_ORGANIZATION:
4282 case LY_STMT_CONTACT:
4283 case LY_STMT_DESCRIPTION:
4284 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004285 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004286 break;
4287
4288 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004289 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004290 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004291 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004292 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004293 case LY_STMT_ANYDATA:
4294 case LY_STMT_ANYXML:
4295 case LY_STMT_AUGMENT:
4296 case LY_STMT_CHOICE:
4297 case LY_STMT_CONTAINER:
4298 case LY_STMT_DEVIATION:
4299 case LY_STMT_EXTENSION:
4300 case LY_STMT_FEATURE:
4301 case LY_STMT_GROUPING:
4302 case LY_STMT_IDENTITY:
4303 case LY_STMT_LEAF:
4304 case LY_STMT_LEAF_LIST:
4305 case LY_STMT_LIST:
4306 case LY_STMT_NOTIFICATION:
4307 case LY_STMT_RPC:
4308 case LY_STMT_TYPEDEF:
4309 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004310 mod_stmt = Y_MOD_BODY;
4311 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004312 case LY_STMT_EXTENSION_INSTANCE:
4313 /* no place in the statement order defined */
4314 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004315 default:
4316 /* error handled in the next switch */
4317 break;
4318 }
Radek Krejcie3846472018-10-15 15:24:51 +02004319#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004320
Radek Krejcie3846472018-10-15 15:24:51 +02004321 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004322 switch (kw) {
4323 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004324 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004325 LY_CHECK_RET(parse_yangversion(ctx, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004327 case LY_STMT_NAMESPACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004328 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 +02004329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004330 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02004331 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 +02004332 break;
4333
4334 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004336 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004339 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004340 break;
4341
4342 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004343 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004344 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 +02004345 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004346 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004347 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 +02004348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004349 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004350 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 +02004351 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004352 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004353 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 +02004354 break;
4355
4356 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004357 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004358 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004359 break;
4360
4361 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004362 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004363 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004364 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004365 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004366 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004367 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004368 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004369 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004371 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004372 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01004375 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004377 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004378 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004380 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004381 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004383 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004384 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004385 break;
4386
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004388 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004390 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004391 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004393 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004394 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004396 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004397 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004399 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004400 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004402 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004403 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004405 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004406 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004407 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004408 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004409 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004411 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004412 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004413 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004414 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004415 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004416 break;
4417
4418 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004419 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004420 return LY_EVALID;
4421 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004422 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004423 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004424
Michal Vasko7fbc8162018-09-17 10:35:16 +02004425 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004426 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004427 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004428 return LY_EVALID;
4429 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004430 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004431 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004432 }
4433
Radek Krejcie9e987e2018-10-31 12:50:27 +01004434 /* submodules share the namespace with the module names, so there must not be
4435 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004436 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004437 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004438 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004439 return LY_EVALID;
4440 }
4441
Michal Vasko12ef5362022-09-16 15:13:58 +02004442cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004443 return ret;
4444}
4445
4446/**
4447 * @brief Parse submodule substatements.
4448 *
4449 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004450 * @param[out] submod Parsed submodule structure.
4451 *
4452 * @return LY_ERR values.
4453 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004454LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004455parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004456{
4457 LY_ERR ret = 0;
4458 char *buf, *word;
4459 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004460 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004461 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004462 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004463
Michal Vaskoc3781c32020-10-06 14:04:08 +02004464 submod->is_submod = 1;
4465
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004466 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004467 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004468 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004469
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004470 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004471
4472#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004473 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 +01004474
4475 switch (kw) {
4476 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004477 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004478 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4479 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004480 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004481 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4482 break;
4483 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004484 case LY_STMT_INCLUDE:
4485 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004486 CHECK_ORDER(Y_MOD_LINKAGE);
4487 break;
4488 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004489 case LY_STMT_ORGANIZATION:
4490 case LY_STMT_CONTACT:
4491 case LY_STMT_DESCRIPTION:
4492 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004493 CHECK_ORDER(Y_MOD_META);
4494 break;
4495
4496 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004497 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004498 CHECK_ORDER(Y_MOD_REVISION);
4499 break;
4500 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004501 case LY_STMT_ANYDATA:
4502 case LY_STMT_ANYXML:
4503 case LY_STMT_AUGMENT:
4504 case LY_STMT_CHOICE:
4505 case LY_STMT_CONTAINER:
4506 case LY_STMT_DEVIATION:
4507 case LY_STMT_EXTENSION:
4508 case LY_STMT_FEATURE:
4509 case LY_STMT_GROUPING:
4510 case LY_STMT_IDENTITY:
4511 case LY_STMT_LEAF:
4512 case LY_STMT_LEAF_LIST:
4513 case LY_STMT_LIST:
4514 case LY_STMT_NOTIFICATION:
4515 case LY_STMT_RPC:
4516 case LY_STMT_TYPEDEF:
4517 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004518 mod_stmt = Y_MOD_BODY;
4519 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004520 case LY_STMT_EXTENSION_INSTANCE:
4521 /* no place in the statement order defined */
4522 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004523 default:
4524 /* error handled in the next switch */
4525 break;
4526 }
4527#undef CHECK_ORDER
4528
4529 prev_kw = kw;
4530 switch (kw) {
4531 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004532 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004533 LY_CHECK_RET(parse_yangversion(ctx, (struct lysp_module *)submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004535 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02004536 LY_CHECK_RET(parse_belongsto(ctx, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004537 break;
4538
4539 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004540 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004541 if (submod->version == LYS_VERSION_1_1) {
4542 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4543 submod->name);
4544 }
Radek Krejci33090f92020-12-17 20:12:46 +01004545 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004546 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004547 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004548 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004549 break;
4550
4551 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004552 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004553 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 +01004554 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004555 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004556 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 +01004557 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004558 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004559 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 +01004560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004561 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004562 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 +01004563 break;
4564
4565 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004566 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004567 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004568 break;
4569
4570 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004571 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004572 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004573 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004574 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004575 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004577 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004578 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004579 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004580 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004581 LY_CHECK_RET(parse_container(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:
Radek Krejci33090f92020-12-17 20:12:46 +01004584 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004585 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004586 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004587 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004588 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004589 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004590 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004591 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004592 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004593 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004594 break;
4595
Radek Krejcid6b76452019-09-03 17:03:03 +02004596 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004597 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004598 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004599 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004600 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004601 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004602 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004603 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004604 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004605 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004606 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004607 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004608 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004609 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004610 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004611 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004612 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004614 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004615 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004617 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004618 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004619 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004620 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004621 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004622 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004623 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004624 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004625 break;
4626
4627 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004628 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004629 return LY_EVALID;
4630 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004631 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004632 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004633
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004634 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004635 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004636 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004637 return LY_EVALID;
4638 }
4639
4640 /* submodules share the namespace with the module names, so there must not be
4641 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004642 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004643 /* main modules may have different revisions */
4644 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004645 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004646 return LY_EVALID;
4647 }
4648
Michal Vasko12ef5362022-09-16 15:13:58 +02004649cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004650 return ret;
4651}
4652
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004653/**
4654 * @brief Skip any redundant characters, namely whitespaces and comments.
4655 *
4656 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004657 * @return LY_SUCCESS on success.
4658 * @return LY_EVALID on invalid comment.
4659 */
4660static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004661skip_redundant_chars(struct lysp_yang_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004662{
4663 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004664 while (ctx->in->current[0]) {
4665 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004666 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004667 ly_in_skip(ctx->in, 2);
4668 LY_CHECK_RET(skip_comment(ctx, 1));
4669 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004670 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004671 ly_in_skip(ctx->in, 2);
4672 LY_CHECK_RET(skip_comment(ctx, 2));
4673 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004674 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004675 if (ctx->in->current[0] == '\n') {
4676 LY_IN_NEW_LINE(ctx->in);
4677 }
Radek Krejci33090f92020-12-17 20:12:46 +01004678 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004679 } else {
4680 break;
4681 }
4682 }
4683
4684 return LY_SUCCESS;
4685}
4686
Radek Krejcid4557c62018-09-17 11:42:09 +02004687LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004688yang_parse_submodule(struct lysp_yang_ctx **context, struct ly_ctx *ly_ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004689 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004690{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004691 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004692 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004693 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004694 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004695 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004696 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004697
aPiecek56be92a2021-07-01 07:18:10 +02004698 assert(context && ly_ctx && main_ctx && in && submod);
4699
David Sedlák1b623122019-08-05 15:27:49 +02004700 /* create context */
4701 *context = calloc(1, sizeof **context);
4702 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004703 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004704 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004705 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004706
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004707 mod_p = calloc(1, sizeof *mod_p);
4708 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004709 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004710 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004711
4712 /* use main context parsed mods adding the current one */
4713 (*context)->parsed_mods = main_ctx->parsed_mods;
4714 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004715
Radek Krejciddace2c2021-01-08 11:30:56 +01004716 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004717
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004718 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004719 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004720 LY_CHECK_GOTO(ret, cleanup);
4721
Michal Vasko7fbc8162018-09-17 10:35:16 +02004722 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004723 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004724 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004725
Radek Krejcid6b76452019-09-03 17:03:03 +02004726 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004727 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004728 ret = LY_EINVAL;
4729 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004730 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004731 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004732 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004733 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004734 }
4735
Michal Vasko7fbc8162018-09-17 10:35:16 +02004736 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004737 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004738 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004739
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004740 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004741 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004742 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004743 if (in->current[0]) {
4744 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004745 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004746 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004747 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004748
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004749 mod_p->parsing = 0;
4750 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004751
Radek Krejcibbe09a92018-11-08 09:36:54 +01004752cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004753 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004754 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004755 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004756 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004757 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004758 }
4759
4760 return ret;
4761}
4762
4763LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004764yang_parse_module(struct lysp_yang_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004765{
4766 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004767 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004768 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004769 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004770 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004771 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004772
David Sedlák1b623122019-08-05 15:27:49 +02004773 /* create context */
4774 *context = calloc(1, sizeof **context);
4775 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004776 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004777 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004778 (*context)->in = in;
Michal Vaskod0625d72022-10-06 15:02:50 +02004779 (*context)->main_ctx = (struct lysp_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004780
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004781 mod_p = calloc(1, sizeof *mod_p);
4782 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4783 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004784 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004785
Radek Krejciddace2c2021-01-08 11:30:56 +01004786 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004787
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004788 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004789 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004790 LY_CHECK_GOTO(ret, cleanup);
4791
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004792 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004793 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004794 LY_CHECK_GOTO(ret, cleanup);
4795
Radek Krejcid6b76452019-09-03 17:03:03 +02004796 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004797 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 +01004798 ret = LY_EINVAL;
4799 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004800 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004801 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004802 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004803 goto cleanup;
4804 }
4805
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004806 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004807 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004808 LY_CHECK_GOTO(ret, cleanup);
4809
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004810 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004811 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004812 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004813 if (in->current[0]) {
4814 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004815 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004816 goto cleanup;
4817 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004818
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004819 mod->parsed = mod_p;
4820
4821cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004822 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004823 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004824 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004825 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004826 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004827 }
4828
Michal Vasko7fbc8162018-09-17 10:35:16 +02004829 return ret;
4830}