blob: e18ed1735ca03388ffe391c55e1b2ac00860b033 [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
Michal Vaskoc636ea42022-09-16 10:20:31 +02006 * Copyright (c) 2018 - 2022 CESNET, z.s.p.o.
Michal Vasko7fbc8162018-09-17 10:35:16 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko63f3d842020-07-08 10:10:14 +020014#include "parser_internal.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
17#include <ctype.h>
18#include <errno.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "log.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020030#include "path.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "set.h"
32#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010033#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "tree_schema.h"
Michal Vaskoc636ea42022-09-16 10:20:31 +020035#include "tree_schema_free.h"
Radek Krejci70853c52018-10-15 14:46:16 +020036#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020037
Radek Krejci77114102021-03-10 15:21:57 +010038struct lys_glob_unres;
39
Radek Krejciceaf2122019-01-02 15:03:26 +010040/**
41 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020042 *
Radek Krejciceaf2122019-01-02 15:03:26 +010043 * @param[in] CTX yang parser context to access libyang context.
44 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
45 * @param[out] TARGET variable where to store the pointer to the inserted value.
46 * @param[in] WORD string to store.
47 * @param[in] LEN length of the string in WORD to store.
48 */
Michal Vasko12ef5362022-09-16 15:13:58 +020049#define INSERT_WORD_GOTO(CTX, BUF, TARGET, WORD, LEN, RET, LABEL) \
50 if (BUF) {LY_CHECK_GOTO(RET = lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)), LABEL);}\
51 else {LY_CHECK_GOTO(RET = lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)), LABEL);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020052
Radek Krejciceaf2122019-01-02 15:03:26 +010053/**
Michal Vasko63f3d842020-07-08 10:10:14 +020054 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020055 *
Radek Krejciceaf2122019-01-02 15:03:26 +010056 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010057 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
58 */
Radek Krejcid54412f2020-12-17 20:25:35 +010059#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020060
Michal Vaskoea5abea2018-09-18 13:10:54 +020061/**
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020062 * @brief Loop through all substatements. Starts a for loop and ::YANG_READ_SUBSTMT_NEXT_ITER must be used at its end.
Michal Vaskoea5abea2018-09-18 13:10:54 +020063 *
Michal Vaskoc8806c82022-12-06 10:31:24 +010064 * @param[in] CTX yang parser context.
Michal Vaskoea5abea2018-09-18 13:10:54 +020065 * @param[out] KW YANG keyword read.
66 * @param[out] WORD Pointer to the keyword itself.
67 * @param[out] WORD_LEN Length of the keyword.
Michal Vasko12ef5362022-09-16 15:13:58 +020068 * @param[out] RET Variable for error storing.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020069 * @param[in] ERR_LABEL Label to go to on error.
Michal Vaskoea5abea2018-09-18 13:10:54 +020070 */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020071#define YANG_READ_SUBSTMT_FOR_GOTO(CTX, KW, WORD, WORD_LEN, RET, ERR_LABEL) \
72 ly_bool __loop_end = 0; \
Michal Vasko12ef5362022-09-16 15:13:58 +020073 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020074 goto ERR_LABEL; \
aPieceka24a2252021-05-07 10:52:31 +020075 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020076 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020077 __loop_end = 1; \
78 } else if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
Michal Vasko193dacd2022-10-13 08:43:05 +020079 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", lyplg_ext_stmt2str(KW)); \
Michal Vasko12ef5362022-09-16 15:13:58 +020080 RET = LY_EVALID; \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020081 goto ERR_LABEL; \
82 } else { \
83 YANG_READ_SUBSTMT_NEXT_ITER(CTX, KW, WORD, WORD_LEN, NULL, RET, ERR_LABEL); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020084 } \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020085 while (!__loop_end)
Michal Vasko7fbc8162018-09-17 10:35:16 +020086
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020087/**
88 * @brief Next iteration of ::YANG_READ_SUBSTMT_FOR_GOTO loop.
89 *
Michal Vaskoc8806c82022-12-06 10:31:24 +010090 * @param[in] CTX yang parser context.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020091 * @param[out] KW YANG keyword read.
92 * @param[out] WORD Pointer to the keyword itself.
93 * @param[out] WORD_LEN Length of the keyword.
94 * @param[in] EXTS Final extension instance array to store.
95 * @param[out] RET Variable for error storing.
96 * @param[in] ERR_LABEL Label to go to on error.
97 */
98#define YANG_READ_SUBSTMT_NEXT_ITER(CTX, KW, WORD, WORD_LEN, EXTS, RET, ERR_LABEL) \
99 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
100 goto ERR_LABEL; \
101 } \
102 if (KW == LY_STMT_SYNTAX_RIGHT_BRACE) { \
Michal Vaskoc8806c82022-12-06 10:31:24 +0100103 if (EXTS && (RET = ly_set_add(&(CTX)->main_ctx->ext_inst, (EXTS), 1, NULL))) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200104 goto ERR_LABEL; \
105 } \
106 __loop_end = 1; \
107 }
108
Michal Vaskod0625d72022-10-06 15:02:50 +0200109LY_ERR parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
110LY_ERR parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
111LY_ERR parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
112LY_ERR parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
113LY_ERR parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
114LY_ERR parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200115
Michal Vaskoea5abea2018-09-18 13:10:54 +0200116/**
117 * @brief Add another character to dynamic buffer, a low-level function.
118 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200119 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200120 *
Radek Krejci404251e2018-10-09 12:06:44 +0200121 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200122 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200123 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200124 * @param[in,out] buf Buffer to use, can be moved by realloc().
125 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200127 * @return LY_ERR values.
128 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200129LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200130buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200131{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100132#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100134 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200135 *buf = ly_realloc(*buf, *buf_len);
136 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
137 }
Radek Krejcic0917392019-04-10 13:04:04 +0200138 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200139 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200140 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200141 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200142 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143
Radek Krejci44ceedc2018-10-02 15:54:31 +0200144 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200145 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100146#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200147}
148
Michal Vaskoea5abea2018-09-18 13:10:54 +0200149/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200150 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
151 *
152 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200153 * @param[in] arg Type of the input string to select method of checking character validity.
154 * @param[in,out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first
Michal Vaskoea5abea2018-09-18 13:10:54 +0200155 * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200156 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
157 * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not requested (word is a substring of the data).
Michal Vaskoea5abea2018-09-18 13:10:54 +0200158 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200159 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200160 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
161 * 0 - colon not yet found (no prefix)
162 * 1 - \p c is the colon character
163 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200164 * @return LY_ERR values.
165 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200166LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200167buf_store_char(struct lysp_yang_ctx *ctx, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200168 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200169{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200170 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 size_t len;
172
Radek Krejcif29b7c32019-04-09 16:17:49 +0200173 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
174 assert(!need_buf || (need_buf && word_b));
175
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100177 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
178 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
179 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200180 if (c == '\n') {
181 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100182 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200183 } else {
184 /* note - even the multibyte character is count as 1 */
185 ++ctx->indent;
186 }
187
Radek Krejci44ceedc2018-10-02 15:54:31 +0200188 /* check character validity */
189 switch (arg) {
190 case Y_IDENTIF_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200191 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200192 break;
193 case Y_PREF_IDENTIF_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200194 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200195 break;
196 case Y_STR_ARG:
197 case Y_MAYBE_STR_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200198 LY_CHECK_RET(lysp_check_stringchar((struct lysp_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200199 break;
200 }
201
Michal Vasko7fbc8162018-09-17 10:35:16 +0200202 if (word_b && *word_b) {
203 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100204 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200205 return LY_EMEM;
206 }
207
208 /* in case of realloc */
209 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200210 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200211 /* first time we need a buffer, copy everything read up to now */
212 if (*word_len) {
213 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200214 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200215 *buf_len = *word_len;
216 memcpy(*word_b, *word_p, *word_len);
217 }
218
219 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100220 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200221 return LY_EMEM;
222 }
223
224 /* in case of realloc */
225 *word_p = *word_b;
226 } else {
227 /* just remember the first character pointer */
228 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100229 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200230 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200231 /* ... and update the word's length */
232 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100233 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200234 }
235
236 return LY_SUCCESS;
237}
238
Michal Vaskoea5abea2018-09-18 13:10:54 +0200239/**
240 * @brief Skip YANG comment in data.
241 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200242 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200243 * @param[in] comment Type of the comment to process:
244 * 1 for a one-line comment,
245 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200246 * @return LY_ERR values.
247 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200248LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200249skip_comment(struct lysp_yang_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200250{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100251 /* internal statuses: */
252#define COMMENT_NO 0 /* comment ended */
253#define COMMENT_LINE 1 /* in line comment */
254#define COMMENT_BLOCK 2 /* in block comment */
255#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
256
Radek Krejci33090f92020-12-17 20:12:46 +0100257 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200258 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100259 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100260 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100261 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100262 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200263 }
264 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100265 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100266 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100267 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100268 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100269 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200270 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200271 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100272 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100273 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100274 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100275 } else if (ctx->in->current[0] != '*') {
276 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100277 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200278 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100279 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200280 }
281 break;
282 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200283 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200284 }
285
Radek Krejci33090f92020-12-17 20:12:46 +0100286 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200287 ctx->indent = 0;
288 } else {
289 ++ctx->indent;
290 }
Radek Krejci33090f92020-12-17 20:12:46 +0100291 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200292 }
293
Radek Krejci33090f92020-12-17 20:12:46 +0100294 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200295 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200296 return LY_EVALID;
297 }
298
299 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100300
301#undef COMMENT_NO
302#undef COMMENT_LINE
303#undef COMMENT_BLOCK
304#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200305}
306
Michal Vaskoea5abea2018-09-18 13:10:54 +0200307/**
308 * @brief Read a quoted string from data.
309 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200310 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200311 * @param[in] arg Type of YANG keyword argument expected.
312 * @param[out] word_p Pointer to the read quoted string.
313 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
314 * set to NULL. Otherwise equal to \p word_p.
315 * @param[out] word_len Length of the read quoted string.
316 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
317 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
318 * indenation in the final quoted string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200319 * @return LY_ERR values.
320 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200322read_qstring(struct lysp_yang_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200323 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200324{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100325 /* string parsing status: */
326#define STRING_ENDED 0 /* string ended */
327#define STRING_SINGLE_QUOTED 1 /* string with ' */
328#define STRING_DOUBLE_QUOTED 2 /* string with " */
329#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
330#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
331#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
332
Radek Krejci1deb5be2020-08-26 16:43:36 +0200333 uint8_t string;
334 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200335 ly_bool need_buf = 0;
336 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200337 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200338 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200339
Radek Krejci33090f92020-12-17 20:12:46 +0100340 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100341 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200342 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200343 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100344 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100345 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200346 }
Radek Krejci33090f92020-12-17 20:12:46 +0100347 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200348
Radek Krejci33090f92020-12-17 20:12:46 +0100349 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200350 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100351 case STRING_SINGLE_QUOTED:
Michal Vasko4e55f5a2022-12-14 12:15:00 +0100352 if (ctx->in->current[0] == '\'') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200353 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100354 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100355 MOVE_INPUT(ctx, 1);
Michal Vasko4e55f5a2022-12-14 12:15:00 +0100356 } else {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200357 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100358 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200359 }
360 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100361 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100362 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200363 case '\"':
364 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100365 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100366 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200367 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200368 break;
369 case '\\':
370 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100371 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200372
373 /* the backslash sequence is substituted, so we will need a buffer to store the result */
374 need_buf = 1;
375
376 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100377 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200378
379 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
380 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
381 trailing_ws = 0;
382
383 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
384 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200385 break;
386 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200387 if (current_indent < block_indent) {
388 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100389 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200390 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100391 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100392 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100393 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200394 }
395 break;
396 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200397 if (current_indent < block_indent) {
398 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100399 current_indent += Y_TAB_SPACES;
400 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200401 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200402 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100403 c = ctx->in->current;
404 ctx->in->current = " ";
405 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
406 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100407 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200408 }
Radek Krejci33090f92020-12-17 20:12:46 +0100409 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200410 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100411 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100412 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100413 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200414 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100415 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200416 }
417 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200418 case '\r':
Michal Vasko438f3782023-08-09 10:01:15 +0200419 /* newline may be escaped */
420 if ((ctx->in->current[1] != '\n') && strncmp(&ctx->in->current[1], "\\n", 2)) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200421 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
422 return LY_EVALID;
423 }
Michal Vasko38266a42023-08-11 11:28:56 +0200424
425 /* skip this character, do not store it */
426 ++ctx->in->current;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200427 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200428 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200429 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200430 /* we will be removing the indents so we need our own buffer */
431 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200432
433 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200434 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200435
Radek Krejciff13cd12019-10-25 15:34:24 +0200436 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200437 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438 }
439
440 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100441 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 +0200442
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200443 /* reset context indentation counter for possible string after this one */
444 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200445 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200446 break;
447 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200448 /* first non-whitespace character, stop eating indentation */
449 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200450
451 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100452 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 +0200453 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 break;
455 }
456 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100457 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200458 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100459 c = ctx->in->current;
460 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200461 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100462 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100463 /* fix false newline count in buf_store_char() */
464 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200465 break;
466 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100467 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200468 break;
469 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200470 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200471 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200472 break;
473 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200474 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100475 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200476 return LY_EVALID;
477 }
478
479 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100480 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 +0200481
Radek Krejcif13b87b2020-12-01 22:02:17 +0100482 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100483 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200484 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100485 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100486 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 case '+':
488 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100489 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200490 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200491 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200492 case '\r':
493 if (ctx->in->current[1] != '\n') {
494 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
495 return LY_EVALID;
496 }
497 MOVE_INPUT(ctx, 1);
498 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200499 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100500 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100501 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200502 case ' ':
503 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200504 /* just skip */
505 break;
506 default:
507 /* string is finished */
508 goto string_end;
509 }
Radek Krejci33090f92020-12-17 20:12:46 +0100510 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200511 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100512 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100513 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200514 case '\r':
515 if (ctx->in->current[1] != '\n') {
516 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
517 return LY_EVALID;
518 }
519 MOVE_INPUT(ctx, 1);
520 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200521 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100522 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100523 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200524 case ' ':
525 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200526 /* skip */
527 break;
528 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100529 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200530 break;
531 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100532 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200533 break;
534 default:
535 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200536 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537 return LY_EVALID;
538 }
Radek Krejci33090f92020-12-17 20:12:46 +0100539 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200540 break;
541 default:
542 return LY_EINT;
543 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200544 }
545
546string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200547 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200548 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200549 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200550 return LY_EVALID;
551 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200552 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100553
554#undef STRING_ENDED
555#undef STRING_SINGLE_QUOTED
556#undef STRING_DOUBLE_QUOTED
557#undef STRING_DOUBLE_QUOTED_ESCAPED
558#undef STRING_PAUSED_NEXTSTRING
559#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560}
561
Michal Vaskoea5abea2018-09-18 13:10:54 +0200562/**
563 * @brief Get another YANG string from the raw data.
564 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200565 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200566 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200567 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
568 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200569 * @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 +0200570 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
571 * set to NULL. Otherwise equal to \p word_p.
572 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200573 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200574 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200575LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200576get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200577 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200578{
Michal Vaskob68ea142021-04-26 13:46:00 +0200579 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200580 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200581 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200582
Michal Vasko7fbc8162018-09-17 10:35:16 +0200583 /* word buffer - dynamically allocated */
584 *word_b = NULL;
585
586 /* word pointer - just a pointer to data */
587 *word_p = NULL;
588
589 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100590 while (ctx->in->current[0]) {
591 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200592 case '\'':
593 case '\"':
594 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200595 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100596 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200597 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200598 ret = LY_EVALID;
599 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200601 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100602 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200603 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200604 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200605 if (!*word_p) {
606 /* do not return NULL word */
607 *word_p = "";
608 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200609 goto str_end;
610 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100611 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200612 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100613 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200614 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100615 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200616 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100617 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200618 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200619 } else {
620 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200621 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 +0200622 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200623 break;
624 case ' ':
625 if (*word_len) {
626 /* word is finished */
627 goto str_end;
628 }
Radek Krejci33090f92020-12-17 20:12:46 +0100629 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200630 break;
631 case '\t':
632 if (*word_len) {
633 /* word is finished */
634 goto str_end;
635 }
636 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100637 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200638
Radek Krejci33090f92020-12-17 20:12:46 +0100639 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200640 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200641 case '\r':
642 if (ctx->in->current[1] != '\n') {
643 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200644 ret = LY_EVALID;
645 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200646 }
647 MOVE_INPUT(ctx, 1);
648 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200649 case '\n':
650 if (*word_len) {
651 /* word is finished */
652 goto str_end;
653 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100654 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100655 MOVE_INPUT(ctx, 1);
656
Michal Vasko7fbc8162018-09-17 10:35:16 +0200657 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200658 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 break;
660 case ';':
661 case '{':
662 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
663 /* word is finished */
664 goto str_end;
665 }
666
Radek Krejci33090f92020-12-17 20:12:46 +0100667 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200668 ret = LY_EVALID;
669 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200670 case '}':
671 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100672 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200673 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200674 ret = LY_EVALID;
675 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200676 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200677 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 +0200678 break;
679 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200680 }
681
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200682 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200683 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200684 ret = LY_EVALID;
685 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200686
Michal Vasko7fbc8162018-09-17 10:35:16 +0200687str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200688 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200690 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200691 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200692 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200693 *word_p = *word_b;
694 }
695
696 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200697
698error:
699 free(*word_b);
700 *word_b = NULL;
701 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200702}
703
Michal Vaskoea5abea2018-09-18 13:10:54 +0200704/**
705 * @brief Get another YANG keyword from the raw data.
706 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200707 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200708 * @param[out] kw YANG keyword read.
709 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
710 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200711 * @return LY_ERR values.
712 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200713LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200714get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200716 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200717 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200718 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200719
720 if (word_p) {
721 *word_p = NULL;
722 *word_len = 0;
723 }
724
725 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100726 while (ctx->in->current[0]) {
727 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200728 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100729 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100731 MOVE_INPUT(ctx, 2);
732 LY_CHECK_RET(skip_comment(ctx, 1));
733 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200734 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100735 MOVE_INPUT(ctx, 2);
736 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200738 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200739 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200740 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200741 }
Radek Krejci13028282019-06-11 14:56:48 +0200742 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200743 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200744 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100745 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200746 ctx->indent = 0;
747 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200749 /* skip whitespaces (optsep) */
750 ++ctx->indent;
751 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200753 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100754 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200755 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100756 case '\r':
757 /* possible CRLF endline */
758 if (ctx->in->current[1] == '\n') {
759 break;
760 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100761 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200762 default:
763 /* either a keyword start or an invalid character */
764 goto keyword_start;
765 }
766
Radek Krejci33090f92020-12-17 20:12:46 +0100767 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200768 }
769
770keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100771 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100772 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200773
aPiecek93582ed2021-05-25 14:49:06 +0200774 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
775 goto success;
776 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
777 ctx->depth++;
778 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100779 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200780 return LY_EINVAL;
781 }
782 goto success;
783 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
784 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200785 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200786 }
787
Radek Krejcid6b76452019-09-03 17:03:03 +0200788 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200789 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100790 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200791 case '\r':
792 if (ctx->in->current[1] != '\n') {
793 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
794 return LY_EVALID;
795 }
796 MOVE_INPUT(ctx, 1);
797 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200798 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200799 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200800 case ' ':
801 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200802 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200803 case ':':
804 /* keyword is not actually a keyword, but prefix of an extension.
805 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
806 * and we will be checking the keyword (extension instance) itself */
807 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100808 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200809 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200810 case '{':
Michal Vasko946b70c2023-07-19 08:57:31 +0200811 case ';':
812 /* allowed only for input and output statements which are without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200813 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200814 break;
815 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100816 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200817 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100818 MOVE_INPUT(ctx, 1);
819 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200820 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821 return LY_EVALID;
822 }
823 } else {
824 /* still can be an extension */
825 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200826
Radek Krejci156ccaf2018-10-15 15:49:17 +0200827extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100828 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200829 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
830 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200831 uint32_t c = 0;
832
Radek Krejci33090f92020-12-17 20:12:46 +0100833 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
834 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200835 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200836 /* check character validity */
Michal Vaskod0625d72022-10-06 15:02:50 +0200837 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100838 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200839 }
Radek Krejci33090f92020-12-17 20:12:46 +0100840 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200841 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200842 return LY_EVALID;
843 }
844
845 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200846 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100847 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200848 return LY_EVALID;
849 }
850
Radek Krejcid6b76452019-09-03 17:03:03 +0200851 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200852 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200853
Radek Krejci626df482018-10-11 15:06:31 +0200854success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200855 if (word_p) {
856 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100857 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200858 }
859
860 return LY_SUCCESS;
861}
862
Michal Vaskoea5abea2018-09-18 13:10:54 +0200863/**
864 * @brief Parse extension instance substatements.
865 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200866 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200867 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200868 * @param[in] word Extension instance substatement name (keyword).
869 * @param[in] word_len Extension instance substatement name length.
870 * @param[in,out] child Children of this extension instance to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200871 * @return LY_ERR values.
872 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200873static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200874parse_ext_substmt(struct lysp_yang_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200875 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876{
877 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100878 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200879 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200880 struct lysp_stmt *stmt, *par_child;
881
882 stmt = calloc(1, sizeof *stmt);
883 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
884
Radek Krejcibb9b1982019-04-08 14:24:59 +0200885 /* insert into parent statements */
886 if (!*child) {
887 *child = stmt;
888 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200889 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200890 par_child->next = stmt;
891 }
892
Michal Vaskofc2cd072021-02-24 13:17:17 +0100893 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200894 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895
896 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100897 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200898 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200899 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200900 }
901
Radek Krejci8df109d2021-04-23 12:19:08 +0200902 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100903 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100904 stmt->kw = kw;
905
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200906 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
907 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
908 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200909 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200910
911cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912 return ret;
913}
914
Michal Vaskoea5abea2018-09-18 13:10:54 +0200915/**
916 * @brief Parse extension instance.
917 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200918 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200919 * @param[in] ext_name Extension instance substatement name (keyword).
920 * @param[in] ext_name_len Extension instance substatement name length.
Michal Vasko193dacd2022-10-13 08:43:05 +0200921 * @param[in] parent Current statement parent.
922 * @param[in] parent_stmt Type of @p parent statement.
923 * @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 +0200924 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200925 * @return LY_ERR values.
926 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200928parse_ext(struct lysp_yang_ctx *ctx, const char *ext_name, size_t ext_name_len, const void *parent,
929 enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200930{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100931 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200933 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200934 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200935 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200937 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200938
Michal Vaskofc2cd072021-02-24 13:17:17 +0100939 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200940 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%.*s\" without the mandatory prefix.",
941 (int)ext_name_len, ext_name);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100942 return LY_EVALID;
943 }
944
945 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200946 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200947
948 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100949 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200950 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200951 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200952 }
953
Michal Vaskofc2cd072021-02-24 13:17:17 +0100954 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200955 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200956 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100957 e->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200958 e->parent = (void *)parent;
959 e->parent_stmt = parent_stmt;
960 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100961
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200962 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
963 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
964 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200965 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200966
967cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968 return ret;
969}
970
Michal Vaskoea5abea2018-09-18 13:10:54 +0200971/**
972 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
973 * description, etc...
974 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200975 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +0200976 * @param[in] parent Current statement parent.
977 * @param[in] parent_stmt Type of statement in @p value.
978 * @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 +0200979 * @param[in,out] value Place to store the parsed value.
980 * @param[in] arg Type of the YANG keyword argument (of the value).
981 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200982 * @return LY_ERR values.
983 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200985parse_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 +0200986 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100988 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200990 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200991 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992
993 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200994 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995 return LY_EVALID;
996 }
997
998 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100999 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001000
1001 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +02001002 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001003
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001004 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001005 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001006 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001007 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent, parent_stmt, parent_stmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008 break;
1009 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001010 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011 return LY_EVALID;
1012 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001013 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001015
1016cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001017 return ret;
1018}
1019
Michal Vaskoea5abea2018-09-18 13:10:54 +02001020/**
1021 * @brief Parse the yang-version statement.
1022 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001023 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001024 * @param[in,out] mod Module to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001025 * @return LY_ERR values.
1026 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001027static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001028parse_yangversion(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001029{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001030 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001032 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001033 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001034
Michal Vasko193dacd2022-10-13 08:43:05 +02001035 if (mod->version) {
David Sedlákb3077192019-06-19 10:55:37 +02001036 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037 return LY_EVALID;
1038 }
1039
1040 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001041 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001042
Radek Krejci96e48da2020-09-04 13:18:06 +02001043 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001044 mod->version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001045 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001046 mod->version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001047 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001048 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001049 free(buf);
1050 return LY_EVALID;
1051 }
1052 free(buf);
1053
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001054 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001056 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001057 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_YANG_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 break;
1059 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001060 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 return LY_EVALID;
1062 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001063 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001065
1066cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067 return ret;
1068}
1069
Michal Vaskoea5abea2018-09-18 13:10:54 +02001070/**
1071 * @brief Parse the belongs-to statement.
1072 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001073 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001074 * @param[in,out] submod Submodule to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001075 * @return LY_ERR values.
1076 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001077static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001078parse_belongsto(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001080 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001082 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001083 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001084
Michal Vasko193dacd2022-10-13 08:43:05 +02001085 if (submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001086 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001087 return LY_EVALID;
1088 }
1089
Michal Vaskoc3781c32020-10-06 14:04:08 +02001090 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001091 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001092 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001093 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 +01001094 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001095 free(buf);
1096 return LY_EVALID;
1097 }
1098 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001099
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001100 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001101 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001102 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001103 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 +02001104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001105 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001106 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_BELONGS_TO, 0, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 break;
1108 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001109 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 return LY_EVALID;
1111 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001112 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001114
Michal Vasko7fbc8162018-09-17 10:35:16 +02001115 /* mandatory substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +02001116 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001117 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118 return LY_EVALID;
1119 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001120
1121cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001122 return ret;
1123}
1124
Michal Vaskoea5abea2018-09-18 13:10:54 +02001125/**
1126 * @brief Parse the revision-date statement.
1127 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001128 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001129 * @param[in,out] rev Buffer to store the parsed value in.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001130 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001131 * @return LY_ERR values.
1132 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001133static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001134parse_revisiondate(struct lysp_yang_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001136 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001138 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001139 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001140
1141 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001142 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143 return LY_EVALID;
1144 }
1145
1146 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001147 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001148
1149 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001150 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001151 free(buf);
1152 return LY_EVALID;
1153 }
1154
1155 /* store value and spend buf if allocated */
1156 strncpy(rev, word, word_len);
1157 free(buf);
1158
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001159 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001161 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001162 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001163 break;
1164 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001165 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001166 return LY_EVALID;
1167 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001168 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001170
1171cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172 return ret;
1173}
1174
Michal Vaskoea5abea2018-09-18 13:10:54 +02001175/**
1176 * @brief Parse the include statement.
1177 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001178 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001179 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001180 * @param[in,out] includes Parsed includes to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001181 * @return LY_ERR values.
1182 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001184parse_include(struct lysp_yang_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185{
Radek Krejcid33273d2018-10-25 14:55:52 +02001186 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001187 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001188 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001189 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190 struct lysp_include *inc;
1191
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001192 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001193
1194 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001195 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196
Michal Vasko12ef5362022-09-16 15:13:58 +02001197 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001198
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001199 /* submodules share the namespace with the module names, so there must not be
1200 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001201 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001202 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001203 return LY_EVALID;
1204 }
1205
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001206 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001207 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001208 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001209 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001210 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 +02001211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001212 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001213 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001214 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 +02001215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001216 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001217 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001219 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001220 LY_CHECK_RET(parse_ext(ctx, word, word_len, inc, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221 break;
1222 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001223 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001224 return LY_EVALID;
1225 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001226 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001228
1229cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230 return ret;
1231}
1232
Michal Vaskoea5abea2018-09-18 13:10:54 +02001233/**
1234 * @brief Parse the import statement.
1235 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001236 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001237 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001238 * @param[in,out] imports Parsed imports to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001239 * @return LY_ERR values.
1240 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001242parse_import(struct lysp_yang_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001243{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001244 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001246 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001247 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248 struct lysp_import *imp;
1249
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001250 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251
1252 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001253 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001254 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001256 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001258 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001259 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 +02001260 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001262 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001263 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001264 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 +02001265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001266 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001267 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001268 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 +02001269 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001270 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001271 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001272 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001273 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001274 LY_CHECK_RET(parse_ext(ctx, word, word_len, imp, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001275 break;
1276 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001277 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001278 return LY_EVALID;
1279 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001280 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001281 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001282
Michal Vasko7fbc8162018-09-17 10:35:16 +02001283 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001284 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001285
Michal Vasko12ef5362022-09-16 15:13:58 +02001286cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001287 return ret;
1288}
1289
Michal Vaskoea5abea2018-09-18 13:10:54 +02001290/**
1291 * @brief Parse the revision statement.
1292 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001293 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001294 * @param[in,out] revs Parsed revisions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001295 * @return LY_ERR values.
1296 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001297static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001298parse_revision(struct lysp_yang_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001299{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001300 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001301 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001302 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001303 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001304 struct lysp_revision *rev;
1305
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001306 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001307
1308 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001309 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310
1311 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001312 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001313 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314 return LY_EVALID;
1315 }
1316
Radek Krejcib7db73a2018-10-24 14:18:40 +02001317 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318 free(buf);
1319
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001320 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001321 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001322 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001323 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 +02001324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001325 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001326 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 +02001327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001328 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001329 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330 break;
1331 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001332 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001333 return LY_EVALID;
1334 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001335 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001337
1338cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 return ret;
1340}
1341
Michal Vaskoea5abea2018-09-18 13:10:54 +02001342/**
1343 * @brief Parse a generic text field that can have more instances such as base.
1344 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001345 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001346 * @param[in] parent Current statement parent.
1347 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001348 * @param[in,out] texts Parsed values to add to.
1349 * @param[in] arg Type of the expected argument.
1350 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001351 * @return LY_ERR values.
1352 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001354parse_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 +02001355 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001356{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001357 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001358 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001359 const char **item;
1360 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001361 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362
1363 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001364 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001365
1366 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001367 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368
Michal Vasko12ef5362022-09-16 15:13:58 +02001369 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001370 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001372 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001373 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 +02001374 break;
1375 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001376 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001377 return LY_EVALID;
1378 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001379 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001380 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001381
1382cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383 return ret;
1384}
1385
Michal Vaskoea5abea2018-09-18 13:10:54 +02001386/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001387 * @brief Parse a generic text field that can have more instances such as base.
1388 *
1389 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001390 * @param[in] parent_stmt Type of statement stored in @p qnames.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001391 * @param[in,out] qnames Parsed qnames to add to.
1392 * @param[in] arg Type of the expected argument.
1393 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001394 * @return LY_ERR values.
1395 */
1396static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001397parse_qnames(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_qname **qnames, enum yang_arg arg,
1398 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +02001399{
1400 LY_ERR ret = LY_SUCCESS;
1401 char *buf, *word;
1402 struct lysp_qname *item;
1403 size_t word_len;
1404 enum ly_stmt kw;
1405
1406 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001407 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001408
1409 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001410 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001411
Michal Vasko12ef5362022-09-16 15:13:58 +02001412 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001413 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001414 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001415 switch (kw) {
1416 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001417 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 +02001418 break;
1419 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001420 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001421 return LY_EVALID;
1422 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001423 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001424 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001425
1426cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001427 return ret;
1428}
1429
1430/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001431 * @brief Parse the config statement.
1432 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001433 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001434 * @param[in,out] flags Flags to add to.
1435 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001436 * @return LY_ERR values.
1437 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001438static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001439parse_config(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001441 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001442 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001443 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001444 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001445
1446 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001447 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448 return LY_EVALID;
1449 }
1450
1451 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001452 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001453
Radek Krejcif13b87b2020-12-01 22:02:17 +01001454 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001455 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001456 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001457 *flags |= LYS_CONFIG_R;
1458 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001459 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001460 free(buf);
1461 return LY_EVALID;
1462 }
1463 free(buf);
1464
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001465 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001467 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001468 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001469 break;
1470 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001471 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472 return LY_EVALID;
1473 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001474 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001476
1477cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478 return ret;
1479}
1480
Michal Vaskoea5abea2018-09-18 13:10:54 +02001481/**
1482 * @brief Parse the mandatory statement.
1483 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001484 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001485 * @param[in,out] flags Flags to add to.
1486 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001487 * @return LY_ERR values.
1488 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001490parse_mandatory(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001492 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001494 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001495 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496
1497 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001498 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001499 return LY_EVALID;
1500 }
1501
1502 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001503 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001504
Radek Krejcif13b87b2020-12-01 22:02:17 +01001505 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001506 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001507 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 *flags |= LYS_MAND_FALSE;
1509 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001510 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001511 free(buf);
1512 return LY_EVALID;
1513 }
1514 free(buf);
1515
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001516 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001518 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001519 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 break;
1521 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001522 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001523 return LY_EVALID;
1524 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001525 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001527
1528cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001529 return ret;
1530}
1531
Michal Vaskoea5abea2018-09-18 13:10:54 +02001532/**
1533 * @brief Parse a restriction such as range or length.
1534 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001535 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001536 * @param[in] restr_kw Type of this particular restriction.
1537 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001538 * @return LY_ERR values.
1539 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001540static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001541parse_restr(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001542{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001543 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001545 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001546 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547
1548 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001549 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550
Michal Vasko193dacd2022-10-13 08:43:05 +02001551 CHECK_NONEMPTY(ctx, word_len, lyplg_ext_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001552 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001553 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001554 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001556 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001557 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 +02001558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001559 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001560 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 +02001561 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001562 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02001563 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG,
1564 &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001566 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001567 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 +02001568 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001569 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001570 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 break;
1572 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001573 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 return LY_EVALID;
1575 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001576 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001578
1579cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 return ret;
1581}
1582
Michal Vaskoea5abea2018-09-18 13:10:54 +02001583/**
1584 * @brief Parse a restriction that can have more instances such as must.
1585 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001586 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001587 * @param[in] restr_kw Type of this particular restriction.
1588 * @param[in,out] restrs Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001589 * @return LY_ERR values.
1590 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001591static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001592parse_restrs(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001593{
1594 struct lysp_restr *restr;
1595
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001596 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001597 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001598}
1599
Michal Vaskoea5abea2018-09-18 13:10:54 +02001600/**
1601 * @brief Parse the status statement.
1602 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001603 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001604 * @param[in,out] flags Flags to add to.
1605 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001606 * @return LY_ERR values.
1607 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001608static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001609parse_status(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001610{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001611 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001613 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001614 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615
1616 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001617 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001618 return LY_EVALID;
1619 }
1620
1621 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001622 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001623
Radek Krejcif13b87b2020-12-01 22:02:17 +01001624 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001626 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001628 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001629 *flags |= LYS_STATUS_OBSLT;
1630 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001631 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001632 free(buf);
1633 return LY_EVALID;
1634 }
1635 free(buf);
1636
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001637 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001639 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001640 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641 break;
1642 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001643 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 return LY_EVALID;
1645 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001646 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001648
1649cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001650 return ret;
1651}
1652
Michal Vaskoea5abea2018-09-18 13:10:54 +02001653/**
1654 * @brief Parse the when statement.
1655 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001656 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001657 * @param[in,out] when_p When pointer to parse to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001658 * @return LY_ERR values.
1659 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001660LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001661parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001662{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001663 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001665 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001666 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001668 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001669
1670 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001671 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672 return LY_EVALID;
1673 }
1674
1675 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001676 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001677
1678 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001679 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001680 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001681 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001682
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001683 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001684 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001685 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001686 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->dsc, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts),
1687 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001688 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001689 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001690 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->ref, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts),
1691 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001692 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001693 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001694 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 +02001695 break;
1696 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001697 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001698 ret = LY_EVALID;
1699 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001700 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001701 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001702 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001703
1704cleanup:
1705 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001706 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001707 free(when);
1708 } else {
1709 *when_p = when;
1710 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001711 return ret;
1712}
1713
Michal Vaskoea5abea2018-09-18 13:10:54 +02001714/**
1715 * @brief Parse the anydata or anyxml statement.
1716 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001717 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001718 * @param[in] any_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001719 * @param[in] parent Node parent.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001720 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001721 * @return LY_ERR values.
1722 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001723LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001724parse_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 +02001725{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001726 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001727 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001728 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001729 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001730 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001731
David Sedlák60adc092019-08-06 15:57:02 +02001732 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001733 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001734
Radek Krejci39b7fc22021-02-26 23:29:18 +01001735 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001736 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001739 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001740 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001741
1742 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001743 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001744 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001745 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001746 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001747 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001748 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001749 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 +02001750 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001751 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001752 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001754 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001755 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001757 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001758 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001760 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001761 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 +02001762 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001763 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001764 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001766 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001767 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001769 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001770 LY_CHECK_RET(parse_ext(ctx, word, word_len, any, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771 break;
1772 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001773 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 return LY_EVALID;
1775 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001776 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001778
1779cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001780 return ret;
1781}
1782
Michal Vaskoea5abea2018-09-18 13:10:54 +02001783/**
1784 * @brief Parse the value or position statement. Substatement of type enum statement.
1785 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001786 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001787 * @param[in] val_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001788 * @param[in,out] enm Structure to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001789 * @return LY_ERR values.
1790 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001791LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001792parse_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 +02001793{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001794 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001795 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001796 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001797 long long num = 0;
1798 unsigned long long unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001799 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001800
Michal Vasko193dacd2022-10-13 08:43:05 +02001801 if (enm->flags & LYS_SET_VALUE) {
1802 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001803 ret = LY_EVALID;
1804 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001805 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001806 enm->flags |= LYS_SET_VALUE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001807
1808 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001809 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001810
Radek Krejcid6b76452019-09-03 17:03:03 +02001811 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001812 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001813 ret = LY_EVALID;
1814 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815 }
1816
1817 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001818 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001819 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001820 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001821 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001822 ret = LY_EVALID;
1823 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001824 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001826 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001827 if (unum > UINT64_C(4294967295)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001828 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001829 ret = LY_EVALID;
1830 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001831 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001832 }
1833 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001834 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001835 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001836 ret = LY_EVALID;
1837 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001838 }
1839 if (errno == ERANGE) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001840 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001841 ret = LY_EVALID;
1842 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001844 if (val_kw == LY_STMT_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001845 enm->value = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02001847 enm->value = unum;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001849
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001850 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001851 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001852 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001853 ret = parse_ext(ctx, word, word_len, enm, val_kw, 0, &enm->exts);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001854 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001855 break;
1856 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001857 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001858 ret = LY_EVALID;
1859 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001860 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001861 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862 }
Radek Krejci8b764662018-11-14 14:15:13 +01001863
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001864cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001865 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001866 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867}
1868
Michal Vaskoea5abea2018-09-18 13:10:54 +02001869/**
1870 * @brief Parse the enum or bit statement. Substatement of type statement.
1871 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001872 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001873 * @param[in] enum_kw Type of this particular keyword.
1874 * @param[in,out] enums Enums or bits to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001875 * @return LY_ERR values.
1876 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001878parse_type_enum(struct lysp_yang_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001880 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001882 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001883 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001884 struct lysp_type_enum *enm;
1885
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001886 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001887
1888 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001889 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 +02001890 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001891 ret = lysp_check_enum_name((struct lysp_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001892 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001893 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001894
Michal Vasko12ef5362022-09-16 15:13:58 +02001895 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001896 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(enum_kw), enm->name);
Radek Krejci8b764662018-11-14 14:15:13 +01001897
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001898 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001900 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001901 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 +02001902 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001903 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001904 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001905 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, 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_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001908 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 +02001909 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001910 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001911 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001912 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001913 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001914 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1915 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1916 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
David Sedlák9fb515f2019-07-11 10:33:58 +02001917 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001918 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001919 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1920 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1921 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001923 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001924 LY_CHECK_RET(parse_ext(ctx, word, word_len, enm, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925 break;
1926 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001927 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928 return LY_EVALID;
1929 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001930 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001932
1933cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934 return ret;
1935}
1936
Michal Vaskoea5abea2018-09-18 13:10:54 +02001937/**
1938 * @brief Parse the fraction-digits statement. Substatement of type statement.
1939 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001940 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001941 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001942 * @return LY_ERR values.
1943 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001944static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001945parse_type_fracdigits(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001947 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001948 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001949 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001950 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001951 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001952
Michal Vasko193dacd2022-10-13 08:43:05 +02001953 if (type->fraction_digits) {
David Sedlákb3077192019-06-19 10:55:37 +02001954 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001955 return LY_EVALID;
1956 }
1957
1958 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001959 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960
1961 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001962 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001963 ret = LY_EVALID;
1964 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001965 }
1966
1967 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001968 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001970 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001971 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001972 ret = LY_EVALID;
1973 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001974 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001975 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001976 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001977 ret = LY_EVALID;
1978 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001980 type->fraction_digits = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001982 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001983 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001984 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001985 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 +02001986 break;
1987 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001988 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001989 ret = LY_EVALID;
1990 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001992 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001993 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001994
1995cleanup:
1996 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001997 return ret;
1998}
1999
Michal Vaskoea5abea2018-09-18 13:10:54 +02002000/**
2001 * @brief Parse the require-instance statement. Substatement of type statement.
2002 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002003 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002004 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002005 * @return LY_ERR values.
2006 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002007static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002008parse_type_reqinstance(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002009{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002010 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002011 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002012 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002013 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014
Michal Vasko193dacd2022-10-13 08:43:05 +02002015 if (type->flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002016 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017 return LY_EVALID;
2018 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002019 type->flags |= LYS_SET_REQINST;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020
2021 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002022 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023
Radek Krejcif13b87b2020-12-01 22:02:17 +01002024 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002025 type->require_instance = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002026 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002027 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002028 ret = LY_EVALID;
2029 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002031
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002032 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002034 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002035 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 +02002036 break;
2037 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002038 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002039 ret = LY_EVALID;
2040 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002042 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002044
2045cleanup:
2046 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047 return ret;
2048}
2049
Michal Vaskoea5abea2018-09-18 13:10:54 +02002050/**
2051 * @brief Parse the modifier statement. Substatement of type pattern statement.
2052 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002053 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002054 * @param[in,out] restr Restriction to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002055 * @return LY_ERR values.
2056 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002057static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002058parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002059{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002060 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002061 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002062 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002063 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064
Michal Vasko193dacd2022-10-13 08:43:05 +02002065 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002066 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002067 return LY_EVALID;
2068 }
2069
2070 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002071 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002072
Radek Krejcif13b87b2020-12-01 22:02:17 +01002073 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002074 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002075 ret = LY_EVALID;
2076 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002077 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078
2079 /* replace the value in the dictionary */
Michal Vasko193dacd2022-10-13 08:43:05 +02002080 buf = malloc(strlen(restr->arg.str) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002081 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02002082 strcpy(buf, restr->arg.str);
2083 lydict_remove(PARSER_CTX(ctx), restr->arg.str);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002084
Radek Krejcif13b87b2020-12-01 22:02:17 +01002085 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2086 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko183ea332022-11-08 10:50:03 +01002087 ret = lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002088 buf = NULL;
Michal Vasko183ea332022-11-08 10:50:03 +01002089 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002091 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002093 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002094 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 +02002095 break;
2096 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002097 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002098 ret = LY_EVALID;
2099 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002101 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002103
2104cleanup:
2105 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106 return ret;
2107}
2108
Michal Vaskoea5abea2018-09-18 13:10:54 +02002109/**
2110 * @brief Parse the pattern statement. Substatement of type statement.
2111 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002112 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002113 * @param[in,out] patterns Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002114 * @return LY_ERR values.
2115 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002117parse_type_pattern(struct lysp_yang_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002118{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002119 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002121 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002122 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 struct lysp_restr *restr;
2124
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002125 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126
2127 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002128 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129
2130 /* add special meaning first byte */
2131 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002132 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002133 word = buf;
2134 } else {
2135 buf = malloc(word_len + 2);
2136 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002137 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002138 if (word_len) {
2139 memmove(buf + 1, word, word_len);
2140 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002141 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002142 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002143 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002144 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002146 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002148 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002149 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 +02002150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002151 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002152 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 +02002153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002154 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02002155 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 +02002156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002157 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002158 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 +02002159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002160 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002161 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko193dacd2022-10-13 08:43:05 +02002162 LY_CHECK_RET(parse_type_pattern_modifier(ctx, restr));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002164 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002165 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166 break;
2167 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002168 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169 return LY_EVALID;
2170 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002171 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002172 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002173
2174cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175 return ret;
2176}
2177
Michal Vaskoea5abea2018-09-18 13:10:54 +02002178/**
2179 * @brief Parse the type statement.
2180 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002181 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002182 * @param[in,out] type Type to wrote to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002183 * @return LY_ERR values.
2184 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002186parse_type(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002188 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002190 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002191 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002192 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002193 struct lysp_type *nest_type;
2194
2195 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002196 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 return LY_EVALID;
2198 }
2199
2200 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002201 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002202 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002203
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002204 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002205 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002206
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002207 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002208 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002209 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002210 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 +01002211 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002213 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002214 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002215 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002217 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002218 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002219 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002221 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002222 LY_CHECK_RET(parse_type_fracdigits(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002223 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002225 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002226 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002227 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228 return LY_EVALID;
2229 }
2230 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002231 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002232
Radek Krejci33090f92020-12-17 20:12:46 +01002233 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002234 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002236 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002237 if (type->path) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002238 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002239 return LY_EVALID;
2240 }
2241
aPiecek4bb1e372021-05-07 11:01:00 +02002242 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2243 * corresponding structure, so in case of failure, the lysp_module_free function will take
2244 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2245 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2246 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002247 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 +02002248 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002249 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002250 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002251 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002252 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002253 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002254 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002256 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002257 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002258 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002259 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002260 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002261 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002262 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 return LY_EVALID;
2264 }
2265 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002266 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267
Radek Krejci33090f92020-12-17 20:12:46 +01002268 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002269 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002271 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002272 LY_CHECK_RET(parse_type_reqinstance(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002273 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002274 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002275 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002276 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002277 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002278 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002280 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002281 LY_CHECK_RET(parse_ext(ctx, word, word_len, type, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002282 break;
2283 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002284 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002285 return LY_EVALID;
2286 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002287 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002289
2290cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002291 return ret;
2292}
2293
Michal Vaskoea5abea2018-09-18 13:10:54 +02002294/**
2295 * @brief Parse the leaf statement.
2296 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002297 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002298 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002299 * @return LY_ERR values.
2300 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002301LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002302parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002303{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002304 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002305 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002306 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002307 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308 struct lysp_node_leaf *leaf;
2309
David Sedlák60adc092019-08-06 15:57:02 +02002310 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002311 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002313 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314
Michal Vasko7fbc8162018-09-17 10:35:16 +02002315 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002316 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002317 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002318
2319 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002320 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002321 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002322 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002323 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002325 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002326 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 +01002327 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002329 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002330 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 +02002331 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002332 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002333 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002335 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002336 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002338 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002339 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002341 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002342 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 +02002343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002344 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002345 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002347 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002348 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002350 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002351 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 +02002352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002353 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002354 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002356 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002357 LY_CHECK_RET(parse_ext(ctx, word, word_len, leaf, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002358 break;
2359 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002360 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361 return LY_EVALID;
2362 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002363 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002365
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 /* mandatory substatements */
2367 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002368 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 return LY_EVALID;
2370 }
2371
Michal Vasko12ef5362022-09-16 15:13:58 +02002372cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002373 return ret;
2374}
2375
Michal Vaskoea5abea2018-09-18 13:10:54 +02002376/**
2377 * @brief Parse the max-elements statement.
2378 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002379 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002380 * @param[in,out] max Value to write to.
2381 * @param[in,out] flags Flags to write to.
2382 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002383 * @return LY_ERR values.
2384 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002385LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002386parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002387{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002388 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002389 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002390 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002391 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002392 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002393
2394 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002395 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396 return LY_EVALID;
2397 }
2398 *flags |= LYS_SET_MAX;
2399
2400 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002401 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002402
2403 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002404 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002405 ret = LY_EVALID;
2406 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002407 }
2408
Radek Krejci7f9b6512019-09-18 13:11:09 +02002409 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002410 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002411 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002413 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002414 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002415 ret = LY_EVALID;
2416 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002417 }
2418 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002419 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002420 ret = LY_EVALID;
2421 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 }
2423
2424 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002425 } else {
2426 /* unbounded */
2427 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002429
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002430 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002431 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002432 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002433 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 +02002434 break;
2435 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002436 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002437 ret = LY_EVALID;
2438 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002439 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002440 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002441 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002442
2443cleanup:
2444 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445 return ret;
2446}
2447
Michal Vaskoea5abea2018-09-18 13:10:54 +02002448/**
2449 * @brief Parse the min-elements statement.
2450 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002451 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002452 * @param[in,out] min Value to write to.
2453 * @param[in,out] flags Flags to write to.
2454 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002455 * @return LY_ERR values.
2456 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002457LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002458parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002459{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002460 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002461 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002462 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002463 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002464 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465
2466 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002467 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002468 return LY_EVALID;
2469 }
2470 *flags |= LYS_SET_MIN;
2471
2472 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002473 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002474
2475 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002476 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002477 ret = LY_EVALID;
2478 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479 }
2480
2481 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002482 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002484 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002485 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002486 ret = LY_EVALID;
2487 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002488 }
2489 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002490 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002491 ret = LY_EVALID;
2492 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493 }
2494 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002495
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002496 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002498 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002499 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 +02002500 break;
2501 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002502 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002503 ret = LY_EVALID;
2504 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002506 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002508
2509cleanup:
2510 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002511 return ret;
2512}
2513
Michal Vaskoea5abea2018-09-18 13:10:54 +02002514/**
2515 * @brief Parse the ordered-by statement.
2516 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002517 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002518 * @param[in,out] llist List or leaf-list to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002519 * @return LY_ERR values.
2520 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002521static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002522parse_orderedby(struct lysp_yang_ctx *ctx, struct lysp_node *llist)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002524 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002525 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002526 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002527 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528
Michal Vasko193dacd2022-10-13 08:43:05 +02002529 if (llist->flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002530 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 return LY_EVALID;
2532 }
2533
2534 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002535 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002536
Radek Krejcif13b87b2020-12-01 22:02:17 +01002537 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002538 llist->flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002539 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002540 llist->flags |= LYS_ORDBY_USER;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002542 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002543 ret = LY_EVALID;
2544 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002545 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002547 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002548 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002549 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002550 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 +02002551 break;
2552 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002553 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002554 ret = LY_EVALID;
2555 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002557 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002559
2560cleanup:
2561 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002562 return ret;
2563}
2564
Michal Vaskoea5abea2018-09-18 13:10:54 +02002565/**
2566 * @brief Parse the leaf-list statement.
2567 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002568 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002569 * @param[in,out] siblings Siblings to add to.
2570 *
2571 * @return LY_ERR values.
2572 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002573LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002574parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002576 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002578 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002579 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002580 struct lysp_node_leaflist *llist;
2581
David Sedlák60adc092019-08-06 15:57:02 +02002582 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002583 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002585 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002588 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002589 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590
2591 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002592 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002594 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002595 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002596 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002597 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002598 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002599 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, 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_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002602 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 +02002603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002604 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002605 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002607 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002608 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002610 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002611 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002613 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002614 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002616 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02002617 LY_CHECK_RET(parse_orderedby(ctx, &llist->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002619 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002620 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 +02002621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002622 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002623 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002626 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002629 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 +02002630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002631 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002632 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002634 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002635 LY_CHECK_RET(parse_ext(ctx, word, word_len, llist, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 break;
2637 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002638 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 return LY_EVALID;
2640 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002641 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002643
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644 /* mandatory substatements */
2645 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002646 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647 return LY_EVALID;
2648 }
2649
Michal Vasko12ef5362022-09-16 15:13:58 +02002650cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002651 return ret;
2652}
2653
Michal Vaskoea5abea2018-09-18 13:10:54 +02002654/**
2655 * @brief Parse the refine statement.
2656 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002657 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002658 * @param[in,out] refines Refines to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002659 * @return LY_ERR values.
2660 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002662parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002664 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002665 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002666 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002667 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002668 struct lysp_refine *rf;
2669
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002670 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671
2672 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002673 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002674 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002675 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002676
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002677 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002679 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002680 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002682 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002683 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, 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_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002686 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 +02002687 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002688 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002689 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002690 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002692 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002693 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002695 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002696 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002698 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002699 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002701 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002702 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002704 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002705 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 +02002706 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002707 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002708 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 +02002709 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002710 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002711 LY_CHECK_RET(parse_ext(ctx, word, word_len, rf, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712 break;
2713 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002714 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002715 return LY_EVALID;
2716 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002717 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002719
2720cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 return ret;
2722}
2723
Michal Vaskoea5abea2018-09-18 13:10:54 +02002724/**
2725 * @brief Parse the typedef statement.
2726 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002727 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002728 * @param[in,out] typedefs Typedefs to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002729 * @return LY_ERR values.
2730 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002732parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002734 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002736 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002737 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738 struct lysp_tpdf *tpdf;
2739
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002740 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741
2742 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002743 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002744 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745
2746 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002747 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002748 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002749 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002750 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 +01002751 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002753 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002754 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 +02002755 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002756 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002757 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 +02002758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002759 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002760 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002762 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002763 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002765 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002766 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 +02002767 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002768 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002769 LY_CHECK_RET(parse_ext(ctx, word, word_len, tpdf, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 break;
2771 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002772 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773 return LY_EVALID;
2774 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002775 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002777
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 /* mandatory substatements */
2779 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002780 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002781 return LY_EVALID;
2782 }
2783
Radek Krejcibbe09a92018-11-08 09:36:54 +01002784 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002785 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002786 assert(ctx->main_ctx);
2787 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002788 }
2789
Michal Vasko12ef5362022-09-16 15:13:58 +02002790cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002791 return ret;
2792}
2793
Michal Vaskoea5abea2018-09-18 13:10:54 +02002794/**
2795 * @brief Parse the input or output statement.
2796 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002797 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002798 * @param[in] kw Type of this particular keyword
2799 * @param[in,out] inout_p Input/output pointer to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002800 * @return LY_ERR values.
2801 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002803parse_inout(struct lysp_yang_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002804 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002805{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002806 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002807 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002808 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002809 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002810 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002812 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002813 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814 return LY_EVALID;
2815 }
2816
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002817 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002818 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2819 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002820 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002821
2822 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002823 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002824 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002825 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002826 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002827 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002828 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002829 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002832 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002835 LY_CHECK_RET(parse_container(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:
Radek Krejci01180ac2021-01-27 08:48:22 +01002838 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002841 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002844 LY_CHECK_RET(parse_list(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_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002847 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002849 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002850 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002852 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002853 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002854 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002856 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002857 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002859 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002860 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_p, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 break;
2862 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002863 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 return LY_EVALID;
2865 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002866 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002868
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002869cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 return ret;
2871}
2872
Michal Vaskoea5abea2018-09-18 13:10:54 +02002873/**
2874 * @brief Parse the action statement.
2875 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002876 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002877 * @param[in,out] actions Actions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002878 * @return LY_ERR values.
2879 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002880LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002881parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002882{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002883 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002884 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002885 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002886 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002887 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888
Radek Krejci2a9fc652021-01-22 17:44:34 +01002889 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890
2891 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002892 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002893 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002894 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002895 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002896
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002897 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002899 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002900 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 +02002901 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002902 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002903 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002905 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002906 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 +02002907 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002908 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002909 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
2911
Radek Krejcid6b76452019-09-03 17:03:03 +02002912 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002913 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002914 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002915 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002916 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917 break;
2918
Radek Krejcid6b76452019-09-03 17:03:03 +02002919 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002920 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002922 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002923 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002924 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002925 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002926 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 +02002927 break;
2928 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002929 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002930 return LY_EVALID;
2931 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002932 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002933 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002934
2935 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2936 if (!act->input.nodetype) {
2937 act->input.nodetype = LYS_INPUT;
2938 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002939 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002940 }
2941 if (!act->output.nodetype) {
2942 act->output.nodetype = LYS_OUTPUT;
2943 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002944 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002945 }
2946
Michal Vasko12ef5362022-09-16 15:13:58 +02002947cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948 return ret;
2949}
2950
Michal Vaskoea5abea2018-09-18 13:10:54 +02002951/**
2952 * @brief Parse the notification statement.
2953 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002954 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002955 * @param[in,out] notifs Notifications to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002956 * @return LY_ERR values.
2957 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002958LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002959parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002961 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002962 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002963 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002964 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002965 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002966
Radek Krejci2a9fc652021-01-22 17:44:34 +01002967 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968
2969 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002970 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002971 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002972 notif->nodetype = LYS_NOTIF;
2973 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002975 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002977 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002978 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 +02002979 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002980 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002981 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002983 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002984 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 +02002985 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002986 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002987 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002988 break;
2989
Radek Krejcid6b76452019-09-03 17:03:03 +02002990 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002991 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002992 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002993 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002994 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002996 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002997 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002999 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003000 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003002 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003003 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003004 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003005 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003006 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003008 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003009 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003011 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003012 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 break;
3014
Radek Krejcid6b76452019-09-03 17:03:03 +02003015 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003016 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003017 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003019 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003020 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003022 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003023 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003025 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003026 LY_CHECK_RET(parse_ext(ctx, word, word_len, notif, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 break;
3028 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003029 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 return LY_EVALID;
3031 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003032 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003034
Michal Vasko12ef5362022-09-16 15:13:58 +02003035cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003036 return ret;
3037}
3038
Michal Vaskoea5abea2018-09-18 13:10:54 +02003039/**
3040 * @brief Parse the grouping statement.
3041 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003042 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003043 * @param[in,out] groupings Groupings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003044 * @return LY_ERR values.
3045 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003046LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003047parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003048{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003049 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003051 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003052 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003053 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003054
Radek Krejci2a9fc652021-01-22 17:44:34 +01003055 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056
3057 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003058 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003059 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003060 grp->nodetype = LYS_GROUPING;
3061 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003063 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003065 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003066 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 +02003067 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003068 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003069 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 +02003070 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003071 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003072 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073 break;
3074
Radek Krejcid6b76452019-09-03 17:03:03 +02003075 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003076 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003077 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003078 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003079 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003080 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003081 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003082 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003084 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003085 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003087 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003088 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003090 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003091 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003093 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003094 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003096 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003097 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 break;
3099
Radek Krejcid6b76452019-09-03 17:03:03 +02003100 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003101 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003103 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003104 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003105 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003106 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003107 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003108 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003109 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003110 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003111 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003112 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003114 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003115 LY_CHECK_RET(parse_ext(ctx, word, word_len, grp, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003116 break;
3117 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003118 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 return LY_EVALID;
3120 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003121 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003123
aPiecek63e080d2021-06-29 13:53:28 +02003124 /* store data for collision check */
3125 if (parent) {
3126 assert(ctx->main_ctx);
3127 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3128 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003129
Michal Vasko12ef5362022-09-16 15:13:58 +02003130cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003131 return ret;
3132}
3133
Michal Vaskoea5abea2018-09-18 13:10:54 +02003134/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003135 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003136 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003137 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003138 * @param[in,out] augments Augments to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003139 * @return LY_ERR values.
3140 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003141LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003142parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003144 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003146 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003147 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003148 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003149
Radek Krejci2a9fc652021-01-22 17:44:34 +01003150 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151
3152 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003153 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003154 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003155 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003156 aug->nodetype = LYS_AUGMENT;
3157 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003159 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003161 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003162 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 +02003163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003164 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003165 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003167 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003168 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 +02003169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003170 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003171 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003173 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003174 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003175 break;
3176
Radek Krejcid6b76452019-09-03 17:03:03 +02003177 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003178 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003179 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003180 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003181 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003183 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003184 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003186 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003187 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003189 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003190 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003192 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003193 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003195 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003196 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003198 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003199 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003200 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003201 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003202 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203 break;
3204
Radek Krejcid6b76452019-09-03 17:03:03 +02003205 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003206 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003207 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003209 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003210 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003211 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003213 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003214 LY_CHECK_RET(parse_ext(ctx, word, word_len, aug, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003215 break;
3216 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003217 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003218 return LY_EVALID;
3219 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003220 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003222
Michal Vasko12ef5362022-09-16 15:13:58 +02003223cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003224 return ret;
3225}
3226
Michal Vaskoea5abea2018-09-18 13:10:54 +02003227/**
3228 * @brief Parse the uses statement.
3229 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003230 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003231 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003232 * @return LY_ERR values.
3233 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003234LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003235parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003237 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003238 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003239 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003240 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003241 struct lysp_node_uses *uses;
3242
David Sedlák60adc092019-08-06 15:57:02 +02003243 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003244 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003246 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003249 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003250 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251
3252 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003253 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003255 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003256 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 +02003257 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003258 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003259 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003261 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003262 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 +02003263 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003264 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003265 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003267 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003268 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003269 break;
3270
Radek Krejcid6b76452019-09-03 17:03:03 +02003271 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003272 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003274 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003275 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003277 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003278 LY_CHECK_RET(parse_ext(ctx, word, word_len, uses, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279 break;
3280 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003281 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003282 return LY_EVALID;
3283 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003284 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003286
Michal Vasko12ef5362022-09-16 15:13:58 +02003287cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288 return ret;
3289}
3290
Michal Vaskoea5abea2018-09-18 13:10:54 +02003291/**
3292 * @brief Parse the case statement.
3293 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003294 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003295 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003296 * @return LY_ERR values.
3297 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003298LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003299parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003301 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003302 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003303 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003304 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 struct lysp_node_case *cas;
3306
David Sedlák60adc092019-08-06 15:57:02 +02003307 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003308 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003309 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003310 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003313 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003314 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315
3316 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003317 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003319 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003320 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 +02003321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003322 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003323 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003325 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003326 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 +02003327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003328 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003329 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003331 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003332 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003333 break;
3334
Radek Krejcid6b76452019-09-03 17:03:03 +02003335 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003336 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003337 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003338 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003339 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003341 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003342 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003344 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003345 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003347 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003348 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003350 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003351 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003353 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003354 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003356 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003357 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003359 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003360 LY_CHECK_RET(parse_ext(ctx, word, word_len, cas, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 break;
3362 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003363 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003364 return LY_EVALID;
3365 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003366 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003368
3369cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 return ret;
3371}
3372
Michal Vaskoea5abea2018-09-18 13:10:54 +02003373/**
3374 * @brief Parse the choice statement.
3375 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003376 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003377 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003378 * @return LY_ERR values.
3379 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003380LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003381parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003382{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003383 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003385 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003386 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003387 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388
David Sedlák60adc092019-08-06 15:57:02 +02003389 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003390 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003391 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003392 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003395 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003396 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003397
3398 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003399 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003400 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003401 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003402 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003404 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003405 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 +02003406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003407 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003408 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003410 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003411 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003414 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 +02003415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003416 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003417 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003419 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003420 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003422 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003423 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 +02003424 &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003425 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426 break;
3427
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003429 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003430 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003432 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003434 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003435 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003437 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003438 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003439 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003441 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003442 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003444 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003445 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003448 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003451 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003454 LY_CHECK_RET(parse_ext(ctx, word, word_len, choice, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
3456 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003457 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 return LY_EVALID;
3459 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003460 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003462
3463cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 return ret;
3465}
3466
Michal Vaskoea5abea2018-09-18 13:10:54 +02003467/**
3468 * @brief Parse the container statement.
3469 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003470 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003471 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003472 * @return LY_ERR values.
3473 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003474LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003475parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476{
3477 LY_ERR ret = 0;
3478 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003479 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003480 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003481 struct lysp_node_container *cont;
3482
David Sedlák60adc092019-08-06 15:57:02 +02003483 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003484 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003486 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487
Michal Vasko7fbc8162018-09-17 10:35:16 +02003488 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003489 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003490 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491
3492 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003493 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003495 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003496 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003498 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003499 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 +02003500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003501 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003502 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003504 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003505 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 +02003506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003507 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003508 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003510 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003511 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003513 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003514 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 +02003515 break;
3516
Radek Krejcid6b76452019-09-03 17:03:03 +02003517 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003518 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003519 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003520 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003521 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003523 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003524 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003527 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003529 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003530 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003532 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003533 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003535 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003536 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003538 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003539 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 break;
3541
Radek Krejcid6b76452019-09-03 17:03:03 +02003542 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003543 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003545 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003546 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003548 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003549 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003550 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003552 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003553 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003554 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003555 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003556 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003557 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003559 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003560 LY_CHECK_RET(parse_ext(ctx, word, word_len, cont, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003561 break;
3562 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003563 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 return LY_EVALID;
3565 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003566 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003568
Michal Vasko12ef5362022-09-16 15:13:58 +02003569cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570 return ret;
3571}
3572
Michal Vaskoea5abea2018-09-18 13:10:54 +02003573/**
3574 * @brief Parse the list statement.
3575 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003576 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003577 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003578 * @return LY_ERR values.
3579 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003580LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003581parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003583 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003585 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003586 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587 struct lysp_node_list *list;
3588
David Sedlák60adc092019-08-06 15:57:02 +02003589 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003590 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003592 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003595 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003596 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597
3598 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003599 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003601 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003602 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003604 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003605 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 +02003606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003607 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003608 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003610 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003611 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 +02003612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003613 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003614 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003616 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003617 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003619 case LY_STMT_KEY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003620 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 +02003621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003622 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003623 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003625 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003626 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003628 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003629 LY_CHECK_RET(parse_orderedby(ctx, &list->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003631 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003632 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633 break;
3634
Radek Krejcid6b76452019-09-03 17:03:03 +02003635 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003636 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003637 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003638 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003639 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003641 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003642 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003644 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003645 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003647 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003648 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003650 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003651 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003653 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003654 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003655 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003656 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003657 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658 break;
3659
Radek Krejcid6b76452019-09-03 17:03:03 +02003660 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003661 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003662 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003663 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003664 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003666 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003667 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003668 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003670 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003671 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003673 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003674 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003675 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003677 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003678 LY_CHECK_RET(parse_ext(ctx, word, word_len, list, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679 break;
3680 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003681 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682 return LY_EVALID;
3683 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003684 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003686
Michal Vasko12ef5362022-09-16 15:13:58 +02003687cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688 return ret;
3689}
3690
Michal Vaskoea5abea2018-09-18 13:10:54 +02003691/**
3692 * @brief Parse the yin-element statement.
3693 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003694 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003695 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003696 * @return LY_ERR values.
3697 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003698static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003699parse_yinelement(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003700{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003701 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003702 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003703 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003704 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003705
Michal Vasko193dacd2022-10-13 08:43:05 +02003706 if (ext->flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003707 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003708 return LY_EVALID;
3709 }
3710
3711 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003712 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003713
Radek Krejcif13b87b2020-12-01 22:02:17 +01003714 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003715 ext->flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003716 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003717 ext->flags |= LYS_YINELEM_FALSE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003718 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003719 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720 free(buf);
3721 return LY_EVALID;
3722 }
3723 free(buf);
3724
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003725 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003727 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003728 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_YIN_ELEMENT, 0, &ext->exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003729 LY_CHECK_RET(ret);
3730 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003731 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003732 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733 return LY_EVALID;
3734 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003735 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003737
3738cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739 return ret;
3740}
3741
Michal Vaskoea5abea2018-09-18 13:10:54 +02003742/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003743 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003744 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003745 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003746 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003747 * @return LY_ERR values.
3748 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003749static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003750parse_argument(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003751{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003752 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003753 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003754 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003755 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003756
Michal Vasko193dacd2022-10-13 08:43:05 +02003757 if (ext->argname) {
David Sedlákb3077192019-06-19 10:55:37 +02003758 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759 return LY_EVALID;
3760 }
3761
3762 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003763 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko193dacd2022-10-13 08:43:05 +02003764 INSERT_WORD_GOTO(ctx, buf, ext->argname, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003766 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003767 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003768 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003769 LY_CHECK_RET(parse_yinelement(ctx, ext));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003770 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003771 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003772 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_ARGUMENT, 0, &ext->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003773 break;
3774 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003775 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003776 return LY_EVALID;
3777 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003778 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003780
3781cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003782 return ret;
3783}
3784
Michal Vaskoea5abea2018-09-18 13:10:54 +02003785/**
3786 * @brief Parse the extension statement.
3787 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003788 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003789 * @param[in,out] extensions Extensions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003790 * @return LY_ERR values.
3791 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003792static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003793parse_extension(struct lysp_yang_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003795 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003796 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003797 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003798 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799 struct lysp_ext *ex;
3800
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003801 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003802
3803 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003804 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003805 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003806
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003807 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003809 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003810 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 +02003811 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003812 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003813 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 +02003814 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003815 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003816 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003818 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003819 LY_CHECK_RET(parse_argument(ctx, ex));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003820 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003821 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003822 LY_CHECK_RET(parse_ext(ctx, word, word_len, ex, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 break;
3824 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003825 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 return LY_EVALID;
3827 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003828 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003830
3831cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 return ret;
3833}
3834
Michal Vaskoea5abea2018-09-18 13:10:54 +02003835/**
3836 * @brief Parse the deviate statement.
3837 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003838 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003839 * @param[in,out] deviates Deviates to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003840 * @return LY_ERR values.
3841 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003842LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003843parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003845 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003846 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003847 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003848 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003849 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003850 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003851 struct lysp_deviate_add *d_add = NULL;
3852 struct lysp_deviate_rpl *d_rpl = NULL;
3853 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003854 const char **d_units = NULL;
3855 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003856 struct lysp_restr **d_musts = NULL;
3857 uint16_t *d_flags = 0;
3858 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003859
3860 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003861 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862
Radek Krejcif13b87b2020-12-01 22:02:17 +01003863 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003865 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003867 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003868 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003869 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 dev_mod = LYS_DEV_DELETE;
3871 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003872 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003873 ret = LY_EVALID;
3874 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003875 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003876
3877 /* create structure */
3878 switch (dev_mod) {
3879 case LYS_DEV_NOT_SUPPORTED:
3880 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003881 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003882 break;
3883 case LYS_DEV_ADD:
3884 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003885 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003886 d = (struct lysp_deviate *)d_add;
3887 d_units = &d_add->units;
3888 d_uniques = &d_add->uniques;
3889 d_dflts = &d_add->dflts;
3890 d_musts = &d_add->musts;
3891 d_flags = &d_add->flags;
3892 d_min = &d_add->min;
3893 d_max = &d_add->max;
3894 break;
3895 case LYS_DEV_REPLACE:
3896 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003897 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 d = (struct lysp_deviate *)d_rpl;
3899 d_units = &d_rpl->units;
3900 d_flags = &d_rpl->flags;
3901 d_min = &d_rpl->min;
3902 d_max = &d_rpl->max;
3903 break;
3904 case LYS_DEV_DELETE:
3905 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003906 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907 d = (struct lysp_deviate *)d_del;
3908 d_units = &d_del->units;
3909 d_uniques = &d_del->uniques;
3910 d_dflts = &d_del->dflts;
3911 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 break;
3913 default:
3914 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003915 LOGINT(PARSER_CTX(ctx));
3916 ret = LY_EINT;
3917 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003918 }
3919 d->mod = dev_mod;
3920
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003921 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003922 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003923 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003924 switch (dev_mod) {
3925 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003926 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003927 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003928 ret = LY_EVALID;
3929 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003931 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003932 break;
3933 }
3934 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003935 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936 switch (dev_mod) {
3937 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02003938 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003939 ret = LY_EVALID;
3940 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003941 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003942 ret = parse_text_field(ctx, &d_rpl->dflt, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts);
3943 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003944 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003945 break;
3946 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003947 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 +02003948 break;
3949 }
3950 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003951 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003952 switch (dev_mod) {
3953 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003954 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003955 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003956 ret = LY_EVALID;
3957 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003958 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003959 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960 break;
3961 }
3962 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003963 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964 switch (dev_mod) {
3965 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003966 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003967 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003968 ret = LY_EVALID;
3969 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003970 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003971 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003972 break;
3973 }
3974 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003975 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 switch (dev_mod) {
3977 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003978 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003979 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003980 ret = LY_EVALID;
3981 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003982 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003983 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984 break;
3985 }
3986 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003987 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003988 switch (dev_mod) {
3989 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003990 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003991 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003992 ret = LY_EVALID;
3993 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003994 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003995 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 break;
3997 }
3998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003999 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 switch (dev_mod) {
4001 case LYS_DEV_NOT_SUPPORTED:
4002 case LYS_DEV_ADD:
4003 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004004 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004005 ret = LY_EVALID;
4006 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004007 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004008 if (d_rpl->type) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004009 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004010 ret = LY_EVALID;
4011 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004012 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004014 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4015 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016 break;
4017 }
4018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004019 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004020 switch (dev_mod) {
4021 case LYS_DEV_NOT_SUPPORTED:
4022 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004023 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004024 ret = LY_EVALID;
4025 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004026 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004027 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 +02004028 break;
4029 }
4030 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004031 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004032 switch (dev_mod) {
4033 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02004034 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004035 ret = LY_EVALID;
4036 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004037 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004038 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 +02004039 break;
4040 }
4041 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004042 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004043 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 +02004044 break;
4045 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004046 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004047 ret = LY_EVALID;
4048 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004049 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004050 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004051 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004052
4053cleanup:
4054 free(buf);
4055 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004056 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004057 free(d);
4058 } else {
4059 /* insert into siblings */
4060 LY_LIST_INSERT(deviates, d, next);
4061 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004062 return ret;
4063}
4064
Michal Vaskoea5abea2018-09-18 13:10:54 +02004065/**
4066 * @brief Parse the deviation statement.
4067 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004068 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004069 * @param[in,out] deviations Deviations to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004070 * @return LY_ERR values.
4071 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004072LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004073parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004074{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004075 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004077 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004078 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004079 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004080 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004081
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004082 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004083
4084 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004085 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004086 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004087 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004088
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004089 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004090 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004091 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004092 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 +02004093 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004094 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004095 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004096 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004097 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004098 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 +02004099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004100 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004101 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 +02004102 break;
4103 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004104 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004105 ret = LY_EVALID;
4106 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004108 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004109 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004110
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111 /* mandatory substatements */
4112 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004113 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004114 ret = LY_EVALID;
4115 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004116 }
4117
Michal Vasko12ef5362022-09-16 15:13:58 +02004118cleanup:
4119 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004120 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004121 LY_ARRAY_DECREMENT_FREE(*deviations);
4122 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004123 return ret;
4124}
4125
Michal Vaskoea5abea2018-09-18 13:10:54 +02004126/**
4127 * @brief Parse the feature statement.
4128 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004129 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004130 * @param[in,out] features Features to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004131 * @return LY_ERR values.
4132 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004133LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004134parse_feature(struct lysp_yang_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004135{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004136 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004137 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004138 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004139 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004140 struct lysp_feature *feat;
4141
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004142 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004143
4144 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004145 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004146 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004147
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004148 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004149 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004150 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004151 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 +02004152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004153 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004154 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004156 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004157 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 +02004158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004159 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004160 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004162 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004163 LY_CHECK_RET(parse_ext(ctx, word, word_len, feat, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004164 break;
4165 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004166 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004167 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004169 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004171
4172cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004173 return ret;
4174}
4175
Michal Vaskoea5abea2018-09-18 13:10:54 +02004176/**
4177 * @brief Parse the identity statement.
4178 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004179 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004180 * @param[in,out] identities Identities to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004181 * @return LY_ERR values.
4182 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004183LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004184parse_identity(struct lysp_yang_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004185{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004186 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004188 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004189 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 struct lysp_ident *ident;
4191
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004192 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193
4194 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004195 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004196 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004197
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004198 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004200 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004201 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 +02004202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004203 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004204 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004205 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004206 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004207 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004208 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 +02004209 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004210 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004211 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004213 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004214 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004215 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 +01004216 return LY_EVALID;
4217 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004218 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 +02004219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004220 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004221 LY_CHECK_RET(parse_ext(ctx, word, word_len, ident, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004222 break;
4223 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004224 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004225 return LY_EVALID;
4226 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004227 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004228 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004229
4230cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004231 return ret;
4232}
4233
Michal Vaskoea5abea2018-09-18 13:10:54 +02004234/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004235 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004236 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004237 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004238 * @param[in,out] mod Module to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004239 * @return LY_ERR values.
4240 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004241LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004242parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004243{
4244 LY_ERR ret = 0;
4245 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004246 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004247 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004248 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004249 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004250
Michal Vaskoc3781c32020-10-06 14:04:08 +02004251 mod->is_submod = 0;
4252
4253 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004254 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004255 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004256
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004257 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004258
Radek Krejcie3846472018-10-15 15:24:51 +02004259#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004260 if (mod_stmt > SECTION) {\
4261 LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;\
4262 } mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004263
Michal Vasko7fbc8162018-09-17 10:35:16 +02004264 switch (kw) {
4265 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004266 case LY_STMT_NAMESPACE:
4267 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004268 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4269 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004270 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004271 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004272 break;
4273 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004274 case LY_STMT_INCLUDE:
4275 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004276 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004277 break;
4278 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004279 case LY_STMT_ORGANIZATION:
4280 case LY_STMT_CONTACT:
4281 case LY_STMT_DESCRIPTION:
4282 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004283 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004284 break;
4285
4286 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004287 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004288 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004289 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004290 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004291 case LY_STMT_ANYDATA:
4292 case LY_STMT_ANYXML:
4293 case LY_STMT_AUGMENT:
4294 case LY_STMT_CHOICE:
4295 case LY_STMT_CONTAINER:
4296 case LY_STMT_DEVIATION:
4297 case LY_STMT_EXTENSION:
4298 case LY_STMT_FEATURE:
4299 case LY_STMT_GROUPING:
4300 case LY_STMT_IDENTITY:
4301 case LY_STMT_LEAF:
4302 case LY_STMT_LEAF_LIST:
4303 case LY_STMT_LIST:
4304 case LY_STMT_NOTIFICATION:
4305 case LY_STMT_RPC:
4306 case LY_STMT_TYPEDEF:
4307 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004308 mod_stmt = Y_MOD_BODY;
4309 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004310 case LY_STMT_EXTENSION_INSTANCE:
4311 /* no place in the statement order defined */
4312 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004313 default:
4314 /* error handled in the next switch */
4315 break;
4316 }
Radek Krejcie3846472018-10-15 15:24:51 +02004317#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004318
Radek Krejcie3846472018-10-15 15:24:51 +02004319 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004320 switch (kw) {
4321 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004322 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004323 LY_CHECK_RET(parse_yangversion(ctx, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004325 case LY_STMT_NAMESPACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004326 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 +02004327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004328 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02004329 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 +02004330 break;
4331
4332 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004333 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004334 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004335 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004336 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004337 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004338 break;
4339
4340 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004341 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004342 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 +02004343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004344 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004345 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 +02004346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004347 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004348 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 +02004349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004350 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004351 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 +02004352 break;
4353
4354 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004355 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004356 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004357 break;
4358
4359 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004361 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004362 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004364 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004367 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004370 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004373 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004376 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004379 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004381 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004382 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004383 break;
4384
Radek Krejcid6b76452019-09-03 17:03:03 +02004385 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004386 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004389 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004392 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004395 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004397 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004398 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004399 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004400 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004401 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004403 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004404 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004406 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004407 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004409 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004410 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004412 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004413 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004414 break;
4415
4416 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004417 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004418 return LY_EVALID;
4419 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004420 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004421 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004422
Michal Vasko7fbc8162018-09-17 10:35:16 +02004423 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004424 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004425 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004426 return LY_EVALID;
4427 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004428 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004429 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004430 }
4431
Radek Krejcie9e987e2018-10-31 12:50:27 +01004432 /* submodules share the namespace with the module names, so there must not be
4433 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004434 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004435 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004436 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004437 return LY_EVALID;
4438 }
4439
Michal Vasko12ef5362022-09-16 15:13:58 +02004440cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004441 return ret;
4442}
4443
4444/**
4445 * @brief Parse submodule substatements.
4446 *
4447 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004448 * @param[out] submod Parsed submodule structure.
4449 *
4450 * @return LY_ERR values.
4451 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004452LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004453parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004454{
4455 LY_ERR ret = 0;
4456 char *buf, *word;
4457 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004458 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004459 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004460 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004461
Michal Vaskoc3781c32020-10-06 14:04:08 +02004462 submod->is_submod = 1;
4463
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004464 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004465 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004466 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004467
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004468 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004469
4470#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004471 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 +01004472
4473 switch (kw) {
4474 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004475 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004476 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4477 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004478 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004479 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4480 break;
4481 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004482 case LY_STMT_INCLUDE:
4483 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004484 CHECK_ORDER(Y_MOD_LINKAGE);
4485 break;
4486 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004487 case LY_STMT_ORGANIZATION:
4488 case LY_STMT_CONTACT:
4489 case LY_STMT_DESCRIPTION:
4490 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004491 CHECK_ORDER(Y_MOD_META);
4492 break;
4493
4494 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004495 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004496 CHECK_ORDER(Y_MOD_REVISION);
4497 break;
4498 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004499 case LY_STMT_ANYDATA:
4500 case LY_STMT_ANYXML:
4501 case LY_STMT_AUGMENT:
4502 case LY_STMT_CHOICE:
4503 case LY_STMT_CONTAINER:
4504 case LY_STMT_DEVIATION:
4505 case LY_STMT_EXTENSION:
4506 case LY_STMT_FEATURE:
4507 case LY_STMT_GROUPING:
4508 case LY_STMT_IDENTITY:
4509 case LY_STMT_LEAF:
4510 case LY_STMT_LEAF_LIST:
4511 case LY_STMT_LIST:
4512 case LY_STMT_NOTIFICATION:
4513 case LY_STMT_RPC:
4514 case LY_STMT_TYPEDEF:
4515 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004516 mod_stmt = Y_MOD_BODY;
4517 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004518 case LY_STMT_EXTENSION_INSTANCE:
4519 /* no place in the statement order defined */
4520 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004521 default:
4522 /* error handled in the next switch */
4523 break;
4524 }
4525#undef CHECK_ORDER
4526
4527 prev_kw = kw;
4528 switch (kw) {
4529 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004530 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004531 LY_CHECK_RET(parse_yangversion(ctx, (struct lysp_module *)submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004532 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004533 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02004534 LY_CHECK_RET(parse_belongsto(ctx, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004535 break;
4536
4537 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004538 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004539 if (submod->version == LYS_VERSION_1_1) {
4540 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4541 submod->name);
4542 }
Radek Krejci33090f92020-12-17 20:12:46 +01004543 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004545 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004546 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004547 break;
4548
4549 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004550 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004551 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 +01004552 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004553 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004554 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 +01004555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004556 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004557 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 +01004558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004559 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004560 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 +01004561 break;
4562
4563 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004564 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004565 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004566 break;
4567
4568 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004569 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004570 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004571 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004572 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004573 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004575 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004576 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004578 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004579 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004580 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004581 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004582 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004583 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004584 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004585 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004586 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004587 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004588 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004589 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004590 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004591 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004592 break;
4593
Radek Krejcid6b76452019-09-03 17:03:03 +02004594 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004595 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004596 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004597 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004598 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004599 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004600 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004601 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004603 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004604 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004606 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004607 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004609 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004610 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004611 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004612 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004613 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004615 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004616 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004618 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004619 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004621 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004622 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004623 break;
4624
4625 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004626 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004627 return LY_EVALID;
4628 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004629 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004630 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004631
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004632 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004633 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004634 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004635 return LY_EVALID;
4636 }
4637
4638 /* submodules share the namespace with the module names, so there must not be
4639 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004640 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004641 /* main modules may have different revisions */
4642 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004643 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004644 return LY_EVALID;
4645 }
4646
Michal Vasko12ef5362022-09-16 15:13:58 +02004647cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004648 return ret;
4649}
4650
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004651/**
4652 * @brief Skip any redundant characters, namely whitespaces and comments.
4653 *
4654 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004655 * @return LY_SUCCESS on success.
4656 * @return LY_EVALID on invalid comment.
4657 */
4658static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004659skip_redundant_chars(struct lysp_yang_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004660{
4661 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004662 while (ctx->in->current[0]) {
4663 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004664 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004665 ly_in_skip(ctx->in, 2);
4666 LY_CHECK_RET(skip_comment(ctx, 1));
4667 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004668 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004669 ly_in_skip(ctx->in, 2);
4670 LY_CHECK_RET(skip_comment(ctx, 2));
4671 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004672 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004673 if (ctx->in->current[0] == '\n') {
4674 LY_IN_NEW_LINE(ctx->in);
4675 }
Radek Krejci33090f92020-12-17 20:12:46 +01004676 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004677 } else {
4678 break;
4679 }
4680 }
4681
4682 return LY_SUCCESS;
4683}
4684
Radek Krejcid4557c62018-09-17 11:42:09 +02004685LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004686yang_parse_submodule(struct lysp_yang_ctx **context, struct ly_ctx *ly_ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004687 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004688{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004689 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004690 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004691 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004692 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004693 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004694 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004695
aPiecek56be92a2021-07-01 07:18:10 +02004696 assert(context && ly_ctx && main_ctx && in && submod);
4697
David Sedlák1b623122019-08-05 15:27:49 +02004698 /* create context */
4699 *context = calloc(1, sizeof **context);
4700 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004701 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004702 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004703 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004704
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004705 mod_p = calloc(1, sizeof *mod_p);
4706 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004707 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004708 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004709
4710 /* use main context parsed mods adding the current one */
4711 (*context)->parsed_mods = main_ctx->parsed_mods;
4712 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004713
Michal Vaskof8ebf132022-11-21 14:06:48 +01004714 LOG_LOCSET(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004715
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004716 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004717 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004718 LY_CHECK_GOTO(ret, cleanup);
4719
Michal Vasko7fbc8162018-09-17 10:35:16 +02004720 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004721 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004722 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004723
Radek Krejcid6b76452019-09-03 17:03:03 +02004724 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004725 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004726 ret = LY_EINVAL;
4727 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004728 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004729 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004730 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004731 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004732 }
4733
Michal Vasko7fbc8162018-09-17 10:35:16 +02004734 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004735 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004736 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004737
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004738 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004739 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004740 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004741 if (in->current[0]) {
4742 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004743 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004744 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004745 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004746
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004747 mod_p->parsing = 0;
4748 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004749
Radek Krejcibbe09a92018-11-08 09:36:54 +01004750cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004751 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004752 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004753 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004754 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004755 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004756 }
4757
4758 return ret;
4759}
4760
4761LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004762yang_parse_module(struct lysp_yang_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004763{
4764 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004765 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004766 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004767 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004768 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004769 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004770
David Sedlák1b623122019-08-05 15:27:49 +02004771 /* create context */
4772 *context = calloc(1, sizeof **context);
4773 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004774 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004775 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004776 (*context)->in = in;
Michal Vaskod0625d72022-10-06 15:02:50 +02004777 (*context)->main_ctx = (struct lysp_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004778
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004779 mod_p = calloc(1, sizeof *mod_p);
4780 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4781 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004782 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004783
Michal Vaskof8ebf132022-11-21 14:06:48 +01004784 LOG_LOCSET(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004785
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004786 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004787 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004788 LY_CHECK_GOTO(ret, cleanup);
4789
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004790 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004791 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004792 LY_CHECK_GOTO(ret, cleanup);
4793
Radek Krejcid6b76452019-09-03 17:03:03 +02004794 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004795 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 +01004796 ret = LY_EINVAL;
4797 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004798 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004799 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004800 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004801 goto cleanup;
4802 }
4803
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004804 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004805 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004806 LY_CHECK_GOTO(ret, cleanup);
4807
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004808 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004809 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004810 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004811 if (in->current[0]) {
4812 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004813 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004814 goto cleanup;
4815 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004816
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004817 mod->parsed = mod_p;
4818
4819cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004820 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004821 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004822 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004823 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004824 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004825 }
4826
Michal Vasko7fbc8162018-09-17 10:35:16 +02004827 return ret;
4828}