blob: b81cf60fedb963aa95b1593d343e0e21d216842c [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
Michal Vaskoc636ea42022-09-16 10:20:31 +02006 * Copyright (c) 2018 - 2022 CESNET, z.s.p.o.
Michal Vasko7fbc8162018-09-17 10:35:16 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko63f3d842020-07-08 10:10:14 +020014#include "parser_internal.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
17#include <ctype.h>
18#include <errno.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "log.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020030#include "path.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "set.h"
32#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010033#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "tree_schema.h"
Michal Vaskoc636ea42022-09-16 10:20:31 +020035#include "tree_schema_free.h"
Radek Krejci70853c52018-10-15 14:46:16 +020036#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020037
Radek Krejci77114102021-03-10 15:21:57 +010038struct lys_glob_unres;
39
Radek Krejciceaf2122019-01-02 15:03:26 +010040/**
41 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020042 *
Radek Krejciceaf2122019-01-02 15:03:26 +010043 * @param[in] CTX yang parser context to access libyang context.
44 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
45 * @param[out] TARGET variable where to store the pointer to the inserted value.
46 * @param[in] WORD string to store.
47 * @param[in] LEN length of the string in WORD to store.
48 */
Michal Vasko12ef5362022-09-16 15:13:58 +020049#define INSERT_WORD_GOTO(CTX, BUF, TARGET, WORD, LEN, RET, LABEL) \
50 if (BUF) {LY_CHECK_GOTO(RET = lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)), LABEL);}\
51 else {LY_CHECK_GOTO(RET = lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)), LABEL);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020052
Radek Krejciceaf2122019-01-02 15:03:26 +010053/**
Michal Vasko63f3d842020-07-08 10:10:14 +020054 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020055 *
Radek Krejciceaf2122019-01-02 15:03:26 +010056 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010057 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
58 */
Radek Krejcid54412f2020-12-17 20:25:35 +010059#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020060
Michal Vaskoea5abea2018-09-18 13:10:54 +020061/**
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020062 * @brief Loop through all substatements. Starts a for loop and ::YANG_READ_SUBSTMT_NEXT_ITER must be used at its end.
Michal Vaskoea5abea2018-09-18 13:10:54 +020063 *
Michal Vaskoc8806c82022-12-06 10:31:24 +010064 * @param[in] CTX yang parser context.
Michal Vaskoea5abea2018-09-18 13:10:54 +020065 * @param[out] KW YANG keyword read.
66 * @param[out] WORD Pointer to the keyword itself.
67 * @param[out] WORD_LEN Length of the keyword.
Michal Vasko12ef5362022-09-16 15:13:58 +020068 * @param[out] RET Variable for error storing.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020069 * @param[in] ERR_LABEL Label to go to on error.
Michal Vaskoea5abea2018-09-18 13:10:54 +020070 */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020071#define YANG_READ_SUBSTMT_FOR_GOTO(CTX, KW, WORD, WORD_LEN, RET, ERR_LABEL) \
72 ly_bool __loop_end = 0; \
Michal Vasko12ef5362022-09-16 15:13:58 +020073 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020074 goto ERR_LABEL; \
aPieceka24a2252021-05-07 10:52:31 +020075 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020076 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020077 __loop_end = 1; \
78 } else if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
Michal Vasko193dacd2022-10-13 08:43:05 +020079 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", lyplg_ext_stmt2str(KW)); \
Michal Vasko12ef5362022-09-16 15:13:58 +020080 RET = LY_EVALID; \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020081 goto ERR_LABEL; \
82 } else { \
83 YANG_READ_SUBSTMT_NEXT_ITER(CTX, KW, WORD, WORD_LEN, NULL, RET, ERR_LABEL); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020084 } \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020085 while (!__loop_end)
Michal Vasko7fbc8162018-09-17 10:35:16 +020086
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020087/**
88 * @brief Next iteration of ::YANG_READ_SUBSTMT_FOR_GOTO loop.
89 *
Michal Vaskoc8806c82022-12-06 10:31:24 +010090 * @param[in] CTX yang parser context.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020091 * @param[out] KW YANG keyword read.
92 * @param[out] WORD Pointer to the keyword itself.
93 * @param[out] WORD_LEN Length of the keyword.
94 * @param[in] EXTS Final extension instance array to store.
95 * @param[out] RET Variable for error storing.
96 * @param[in] ERR_LABEL Label to go to on error.
97 */
98#define YANG_READ_SUBSTMT_NEXT_ITER(CTX, KW, WORD, WORD_LEN, EXTS, RET, ERR_LABEL) \
99 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
100 goto ERR_LABEL; \
101 } \
102 if (KW == LY_STMT_SYNTAX_RIGHT_BRACE) { \
Michal Vaskoc8806c82022-12-06 10:31:24 +0100103 if (EXTS && (RET = ly_set_add(&(CTX)->main_ctx->ext_inst, (EXTS), 1, NULL))) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200104 goto ERR_LABEL; \
105 } \
106 __loop_end = 1; \
107 }
108
Michal Vaskod0625d72022-10-06 15:02:50 +0200109LY_ERR parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
110LY_ERR parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
111LY_ERR parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
112LY_ERR parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
113LY_ERR parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
114LY_ERR parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200115
Michal Vaskoea5abea2018-09-18 13:10:54 +0200116/**
117 * @brief Add another character to dynamic buffer, a low-level function.
118 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200119 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200120 *
Radek Krejci404251e2018-10-09 12:06:44 +0200121 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200122 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200123 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200124 * @param[in,out] buf Buffer to use, can be moved by realloc().
125 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200127 * @return LY_ERR values.
128 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200129LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200130buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200131{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100132#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100134 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200135 *buf = ly_realloc(*buf, *buf_len);
136 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
137 }
Radek Krejcic0917392019-04-10 13:04:04 +0200138 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200139 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200140 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200141 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200142 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143
Radek Krejci44ceedc2018-10-02 15:54:31 +0200144 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200145 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100146#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200147}
148
Michal Vaskoea5abea2018-09-18 13:10:54 +0200149/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200150 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
151 *
152 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200153 * @param[in] arg Type of the input string to select method of checking character validity.
154 * @param[in,out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first
Michal Vaskoea5abea2018-09-18 13:10:54 +0200155 * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200156 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
157 * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not requested (word is a substring of the data).
Michal Vaskoea5abea2018-09-18 13:10:54 +0200158 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200159 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200160 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
161 * 0 - colon not yet found (no prefix)
162 * 1 - \p c is the colon character
163 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200164 * @return LY_ERR values.
165 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200166LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200167buf_store_char(struct lysp_yang_ctx *ctx, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200168 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200169{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200170 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 size_t len;
172
Radek Krejcif29b7c32019-04-09 16:17:49 +0200173 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
174 assert(!need_buf || (need_buf && word_b));
175
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100177 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
178 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
179 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200180 if (c == '\n') {
181 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100182 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200183 } else {
184 /* note - even the multibyte character is count as 1 */
185 ++ctx->indent;
186 }
187
Radek Krejci44ceedc2018-10-02 15:54:31 +0200188 /* check character validity */
189 switch (arg) {
190 case Y_IDENTIF_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200191 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200192 break;
193 case Y_PREF_IDENTIF_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200194 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200195 break;
196 case Y_STR_ARG:
197 case Y_MAYBE_STR_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200198 LY_CHECK_RET(lysp_check_stringchar((struct lysp_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200199 break;
200 }
201
Michal Vasko7fbc8162018-09-17 10:35:16 +0200202 if (word_b && *word_b) {
203 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100204 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200205 return LY_EMEM;
206 }
207
208 /* in case of realloc */
209 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200210 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200211 /* first time we need a buffer, copy everything read up to now */
212 if (*word_len) {
213 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200214 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200215 *buf_len = *word_len;
216 memcpy(*word_b, *word_p, *word_len);
217 }
218
219 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100220 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200221 return LY_EMEM;
222 }
223
224 /* in case of realloc */
225 *word_p = *word_b;
226 } else {
227 /* just remember the first character pointer */
228 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100229 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200230 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200231 /* ... and update the word's length */
232 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100233 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200234 }
235
236 return LY_SUCCESS;
237}
238
Michal Vaskoea5abea2018-09-18 13:10:54 +0200239/**
240 * @brief Skip YANG comment in data.
241 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200242 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200243 * @param[in] comment Type of the comment to process:
244 * 1 for a one-line comment,
245 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200246 * @return LY_ERR values.
247 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200248LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200249skip_comment(struct lysp_yang_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200250{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100251 /* internal statuses: */
252#define COMMENT_NO 0 /* comment ended */
253#define COMMENT_LINE 1 /* in line comment */
254#define COMMENT_BLOCK 2 /* in block comment */
255#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
256
Radek Krejci33090f92020-12-17 20:12:46 +0100257 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200258 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100259 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100260 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100261 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100262 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200263 }
264 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100265 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100266 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100267 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100268 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100269 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200270 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200271 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100272 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100273 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100274 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100275 } else if (ctx->in->current[0] != '*') {
276 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100277 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200278 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100279 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200280 }
281 break;
282 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200283 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200284 }
285
Radek Krejci33090f92020-12-17 20:12:46 +0100286 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200287 ctx->indent = 0;
288 } else {
289 ++ctx->indent;
290 }
Radek Krejci33090f92020-12-17 20:12:46 +0100291 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200292 }
293
Radek Krejci33090f92020-12-17 20:12:46 +0100294 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200295 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200296 return LY_EVALID;
297 }
298
299 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100300
301#undef COMMENT_NO
302#undef COMMENT_LINE
303#undef COMMENT_BLOCK
304#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200305}
306
Michal Vaskoea5abea2018-09-18 13:10:54 +0200307/**
308 * @brief Read a quoted string from data.
309 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200310 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200311 * @param[in] arg Type of YANG keyword argument expected.
312 * @param[out] word_p Pointer to the read quoted string.
313 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
314 * set to NULL. Otherwise equal to \p word_p.
315 * @param[out] word_len Length of the read quoted string.
316 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
317 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
318 * indenation in the final quoted string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200319 * @return LY_ERR values.
320 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200322read_qstring(struct lysp_yang_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200323 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200324{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100325 /* string parsing status: */
326#define STRING_ENDED 0 /* string ended */
327#define STRING_SINGLE_QUOTED 1 /* string with ' */
328#define STRING_DOUBLE_QUOTED 2 /* string with " */
329#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
330#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
331#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
332
Radek Krejci1deb5be2020-08-26 16:43:36 +0200333 uint8_t string;
334 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200335 ly_bool need_buf = 0;
336 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200337 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200338 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200339
Radek Krejci33090f92020-12-17 20:12:46 +0100340 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100341 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200342 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200343 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100344 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100345 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200346 }
Radek Krejci33090f92020-12-17 20:12:46 +0100347 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200348
Radek Krejci33090f92020-12-17 20:12:46 +0100349 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200350 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100351 case STRING_SINGLE_QUOTED:
Michal Vasko4e55f5a2022-12-14 12:15:00 +0100352 if (ctx->in->current[0] == '\'') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200353 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100354 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100355 MOVE_INPUT(ctx, 1);
Michal Vasko4e55f5a2022-12-14 12:15:00 +0100356 } else {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200357 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100358 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200359 }
360 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100361 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100362 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200363 case '\"':
364 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100365 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100366 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200367 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200368 break;
369 case '\\':
370 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100371 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200372
373 /* the backslash sequence is substituted, so we will need a buffer to store the result */
374 need_buf = 1;
375
376 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100377 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200378
379 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
380 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
381 trailing_ws = 0;
382
383 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
384 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200385 break;
386 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200387 if (current_indent < block_indent) {
388 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100389 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200390 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100391 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100392 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100393 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200394 }
395 break;
396 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200397 if (current_indent < block_indent) {
398 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100399 current_indent += Y_TAB_SPACES;
400 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200401 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200402 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100403 c = ctx->in->current;
404 ctx->in->current = " ";
405 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
406 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100407 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200408 }
Radek Krejci33090f92020-12-17 20:12:46 +0100409 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200410 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100411 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100412 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100413 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200414 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100415 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200416 }
417 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200418 case '\r':
419 if (ctx->in->current[1] != '\n') {
420 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
421 return LY_EVALID;
422 }
423 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200424 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200425 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200426 /* we will be removing the indents so we need our own buffer */
427 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200428
429 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200430 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200431
Radek Krejciff13cd12019-10-25 15:34:24 +0200432 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200433 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200434 }
435
436 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100437 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200438
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200439 /* reset context indentation counter for possible string after this one */
440 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200441 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200442 break;
443 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200444 /* first non-whitespace character, stop eating indentation */
445 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200446
447 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100448 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200449 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200450 break;
451 }
452 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100453 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100455 c = ctx->in->current;
456 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200457 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100458 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100459 /* fix false newline count in buf_store_char() */
460 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200461 break;
462 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100463 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200464 break;
465 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200466 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200467 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200468 break;
469 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100471 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200472 return LY_EVALID;
473 }
474
475 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100476 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200477
Radek Krejcif13b87b2020-12-01 22:02:17 +0100478 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100479 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100481 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100482 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200483 case '+':
484 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100485 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200486 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200488 case '\r':
489 if (ctx->in->current[1] != '\n') {
490 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
491 return LY_EVALID;
492 }
493 MOVE_INPUT(ctx, 1);
494 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200495 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100496 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100497 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200498 case ' ':
499 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200500 /* just skip */
501 break;
502 default:
503 /* string is finished */
504 goto string_end;
505 }
Radek Krejci33090f92020-12-17 20:12:46 +0100506 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200507 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100508 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100509 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200510 case '\r':
511 if (ctx->in->current[1] != '\n') {
512 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
513 return LY_EVALID;
514 }
515 MOVE_INPUT(ctx, 1);
516 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200517 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100518 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100519 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200520 case ' ':
521 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200522 /* skip */
523 break;
524 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100525 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200526 break;
527 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100528 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200529 break;
530 default:
531 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200532 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200533 return LY_EVALID;
534 }
Radek Krejci33090f92020-12-17 20:12:46 +0100535 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200536 break;
537 default:
538 return LY_EINT;
539 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200540 }
541
542string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200543 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200544 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200545 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200546 return LY_EVALID;
547 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200548 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100549
550#undef STRING_ENDED
551#undef STRING_SINGLE_QUOTED
552#undef STRING_DOUBLE_QUOTED
553#undef STRING_DOUBLE_QUOTED_ESCAPED
554#undef STRING_PAUSED_NEXTSTRING
555#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200556}
557
Michal Vaskoea5abea2018-09-18 13:10:54 +0200558/**
559 * @brief Get another YANG string from the raw data.
560 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200561 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200562 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200563 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
564 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200565 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200566 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
567 * set to NULL. Otherwise equal to \p word_p.
568 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200569 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200570 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200571LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200572get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200573 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200574{
Michal Vaskob68ea142021-04-26 13:46:00 +0200575 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200576 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200577 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200578
Michal Vasko7fbc8162018-09-17 10:35:16 +0200579 /* word buffer - dynamically allocated */
580 *word_b = NULL;
581
582 /* word pointer - just a pointer to data */
583 *word_p = NULL;
584
585 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100586 while (ctx->in->current[0]) {
587 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200588 case '\'':
589 case '\"':
590 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200591 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100592 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200593 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200594 ret = LY_EVALID;
595 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200596 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200597 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100598 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200599 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200600 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200601 if (!*word_p) {
602 /* do not return NULL word */
603 *word_p = "";
604 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200605 goto str_end;
606 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100607 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200608 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100609 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200610 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100611 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200612 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100613 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200614 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200615 } else {
616 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200617 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200618 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200619 break;
620 case ' ':
621 if (*word_len) {
622 /* word is finished */
623 goto str_end;
624 }
Radek Krejci33090f92020-12-17 20:12:46 +0100625 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200626 break;
627 case '\t':
628 if (*word_len) {
629 /* word is finished */
630 goto str_end;
631 }
632 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100633 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200634
Radek Krejci33090f92020-12-17 20:12:46 +0100635 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200636 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200637 case '\r':
638 if (ctx->in->current[1] != '\n') {
639 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200640 ret = LY_EVALID;
641 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200642 }
643 MOVE_INPUT(ctx, 1);
644 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200645 case '\n':
646 if (*word_len) {
647 /* word is finished */
648 goto str_end;
649 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100650 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100651 MOVE_INPUT(ctx, 1);
652
Michal Vasko7fbc8162018-09-17 10:35:16 +0200653 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200654 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655 break;
656 case ';':
657 case '{':
658 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
659 /* word is finished */
660 goto str_end;
661 }
662
Radek Krejci33090f92020-12-17 20:12:46 +0100663 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200664 ret = LY_EVALID;
665 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200666 case '}':
667 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100668 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200669 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200670 ret = LY_EVALID;
671 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200673 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200674 break;
675 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200676 }
677
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200678 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200679 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200680 ret = LY_EVALID;
681 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200682
Michal Vasko7fbc8162018-09-17 10:35:16 +0200683str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200684 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200685 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200686 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200687 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200688 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689 *word_p = *word_b;
690 }
691
692 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200693
694error:
695 free(*word_b);
696 *word_b = NULL;
697 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200698}
699
Michal Vaskoea5abea2018-09-18 13:10:54 +0200700/**
701 * @brief Get another YANG keyword from the raw data.
702 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200703 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200704 * @param[out] kw YANG keyword read.
705 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
706 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200707 * @return LY_ERR values.
708 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200709LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200710get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200711{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200712 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200713 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200714 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715
716 if (word_p) {
717 *word_p = NULL;
718 *word_len = 0;
719 }
720
721 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100722 while (ctx->in->current[0]) {
723 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200724 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100725 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200726 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100727 MOVE_INPUT(ctx, 2);
728 LY_CHECK_RET(skip_comment(ctx, 1));
729 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100731 MOVE_INPUT(ctx, 2);
732 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200733 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200734 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200735 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200736 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737 }
Radek Krejci13028282019-06-11 14:56:48 +0200738 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200739 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200740 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100741 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200742 ctx->indent = 0;
743 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200744 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200745 /* skip whitespaces (optsep) */
746 ++ctx->indent;
747 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200749 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100750 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200751 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100752 case '\r':
753 /* possible CRLF endline */
754 if (ctx->in->current[1] == '\n') {
755 break;
756 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100757 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200758 default:
759 /* either a keyword start or an invalid character */
760 goto keyword_start;
761 }
762
Radek Krejci33090f92020-12-17 20:12:46 +0100763 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200764 }
765
766keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100767 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100768 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200769
aPiecek93582ed2021-05-25 14:49:06 +0200770 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
771 goto success;
772 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
773 ctx->depth++;
774 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100775 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200776 return LY_EINVAL;
777 }
778 goto success;
779 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
780 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200781 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200782 }
783
Radek Krejcid6b76452019-09-03 17:03:03 +0200784 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200785 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100786 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200787 case '\r':
788 if (ctx->in->current[1] != '\n') {
789 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
790 return LY_EVALID;
791 }
792 MOVE_INPUT(ctx, 1);
793 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200794 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200795 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200796 case ' ':
797 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200798 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200799 case ':':
800 /* keyword is not actually a keyword, but prefix of an extension.
801 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
802 * and we will be checking the keyword (extension instance) itself */
803 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100804 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200805 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200806 case '{':
Michal Vasko946b70c2023-07-19 08:57:31 +0200807 case ';':
808 /* allowed only for input and output statements which are without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200809 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200810 break;
811 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100812 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200813 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100814 MOVE_INPUT(ctx, 1);
815 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200816 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200817 return LY_EVALID;
818 }
819 } else {
820 /* still can be an extension */
821 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200822
Radek Krejci156ccaf2018-10-15 15:49:17 +0200823extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100824 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200825 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
826 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200827 uint32_t c = 0;
828
Radek Krejci33090f92020-12-17 20:12:46 +0100829 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
830 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200831 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200832 /* check character validity */
Michal Vaskod0625d72022-10-06 15:02:50 +0200833 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100834 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200835 }
Radek Krejci33090f92020-12-17 20:12:46 +0100836 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200837 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200838 return LY_EVALID;
839 }
840
841 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200842 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100843 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200844 return LY_EVALID;
845 }
846
Radek Krejcid6b76452019-09-03 17:03:03 +0200847 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200848 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200849
Radek Krejci626df482018-10-11 15:06:31 +0200850success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200851 if (word_p) {
852 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100853 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200854 }
855
856 return LY_SUCCESS;
857}
858
Michal Vaskoea5abea2018-09-18 13:10:54 +0200859/**
860 * @brief Parse extension instance substatements.
861 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200862 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200863 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200864 * @param[in] word Extension instance substatement name (keyword).
865 * @param[in] word_len Extension instance substatement name length.
866 * @param[in,out] child Children of this extension instance to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200867 * @return LY_ERR values.
868 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200869static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200870parse_ext_substmt(struct lysp_yang_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200871 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200872{
873 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100874 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200875 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876 struct lysp_stmt *stmt, *par_child;
877
878 stmt = calloc(1, sizeof *stmt);
879 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
880
Radek Krejcibb9b1982019-04-08 14:24:59 +0200881 /* insert into parent statements */
882 if (!*child) {
883 *child = stmt;
884 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200885 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200886 par_child->next = stmt;
887 }
888
Michal Vaskofc2cd072021-02-24 13:17:17 +0100889 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200890 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200891
892 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100893 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200894 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200895 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200896 }
897
Radek Krejci8df109d2021-04-23 12:19:08 +0200898 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100899 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100900 stmt->kw = kw;
901
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200902 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
903 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
904 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200906
907cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200908 return ret;
909}
910
Michal Vaskoea5abea2018-09-18 13:10:54 +0200911/**
912 * @brief Parse extension instance.
913 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200914 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200915 * @param[in] ext_name Extension instance substatement name (keyword).
916 * @param[in] ext_name_len Extension instance substatement name length.
Michal Vasko193dacd2022-10-13 08:43:05 +0200917 * @param[in] parent Current statement parent.
918 * @param[in] parent_stmt Type of @p parent statement.
919 * @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 +0200920 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200921 * @return LY_ERR values.
922 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200923static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200924parse_ext(struct lysp_yang_ctx *ctx, const char *ext_name, size_t ext_name_len, const void *parent,
925 enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200926{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100927 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200929 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200930 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200931 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200933 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200934
Michal Vaskofc2cd072021-02-24 13:17:17 +0100935 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
936 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
937 return LY_EVALID;
938 }
939
940 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200941 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200942
943 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100944 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200945 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200946 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200947 }
948
Michal Vaskofc2cd072021-02-24 13:17:17 +0100949 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200950 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200951 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100952 e->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200953 e->parent = (void *)parent;
954 e->parent_stmt = parent_stmt;
955 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100956
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200957 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
958 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
959 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200961
962cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200963 return ret;
964}
965
Michal Vaskoea5abea2018-09-18 13:10:54 +0200966/**
967 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
968 * description, etc...
969 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200970 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +0200971 * @param[in] parent Current statement parent.
972 * @param[in] parent_stmt Type of statement in @p value.
973 * @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 +0200974 * @param[in,out] value Place to store the parsed value.
975 * @param[in] arg Type of the YANG keyword argument (of the value).
976 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200977 * @return LY_ERR values.
978 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200979static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200980parse_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 +0200981 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200982{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100983 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200985 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200986 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987
988 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200989 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 return LY_EVALID;
991 }
992
993 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100994 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995
996 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +0200997 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200998
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200999 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001000 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001001 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001002 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent, parent_stmt, parent_stmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001003 break;
1004 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001005 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006 return LY_EVALID;
1007 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001008 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001010
1011cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001012 return ret;
1013}
1014
Michal Vaskoea5abea2018-09-18 13:10:54 +02001015/**
1016 * @brief Parse the yang-version statement.
1017 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001018 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001019 * @param[in,out] mod Module to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001020 * @return LY_ERR values.
1021 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001022static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001023parse_yangversion(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001024{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001025 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001026 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001027 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001028 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001029
Michal Vasko193dacd2022-10-13 08:43:05 +02001030 if (mod->version) {
David Sedlákb3077192019-06-19 10:55:37 +02001031 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001032 return LY_EVALID;
1033 }
1034
1035 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001036 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037
Radek Krejci96e48da2020-09-04 13:18:06 +02001038 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001039 mod->version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001040 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001041 mod->version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001042 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001043 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001044 free(buf);
1045 return LY_EVALID;
1046 }
1047 free(buf);
1048
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001049 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001050 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001051 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001052 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_YANG_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053 break;
1054 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001055 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056 return LY_EVALID;
1057 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001058 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001059 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001060
1061cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001062 return ret;
1063}
1064
Michal Vaskoea5abea2018-09-18 13:10:54 +02001065/**
1066 * @brief Parse the belongs-to statement.
1067 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001068 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001069 * @param[in,out] submod Submodule to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001070 * @return LY_ERR values.
1071 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001072static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001073parse_belongsto(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001074{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001075 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001076 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001077 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001078 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079
Michal Vasko193dacd2022-10-13 08:43:05 +02001080 if (submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001081 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001082 return LY_EVALID;
1083 }
1084
Michal Vaskoc3781c32020-10-06 14:04:08 +02001085 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001086 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001087 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001088 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 +01001089 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001090 free(buf);
1091 return LY_EVALID;
1092 }
1093 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001094
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001095 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001096 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001097 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001098 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 +02001099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001100 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001101 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_BELONGS_TO, 0, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102 break;
1103 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001104 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001105 return LY_EVALID;
1106 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001107 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001108 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001109
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 /* mandatory substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +02001111 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001112 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113 return LY_EVALID;
1114 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001115
1116cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001117 return ret;
1118}
1119
Michal Vaskoea5abea2018-09-18 13:10:54 +02001120/**
1121 * @brief Parse the revision-date statement.
1122 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001123 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001124 * @param[in,out] rev Buffer to store the parsed value in.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001125 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001126 * @return LY_ERR values.
1127 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001128static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001129parse_revisiondate(struct lysp_yang_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001131 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001133 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001134 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135
1136 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001137 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001138 return LY_EVALID;
1139 }
1140
1141 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001142 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143
1144 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001145 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001146 free(buf);
1147 return LY_EVALID;
1148 }
1149
1150 /* store value and spend buf if allocated */
1151 strncpy(rev, word, word_len);
1152 free(buf);
1153
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001154 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001156 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001157 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158 break;
1159 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001160 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161 return LY_EVALID;
1162 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001163 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001165
1166cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001167 return ret;
1168}
1169
Michal Vaskoea5abea2018-09-18 13:10:54 +02001170/**
1171 * @brief Parse the include statement.
1172 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001173 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001174 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001175 * @param[in,out] includes Parsed includes to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001176 * @return LY_ERR values.
1177 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001179parse_include(struct lysp_yang_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180{
Radek Krejcid33273d2018-10-25 14:55:52 +02001181 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001183 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001184 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185 struct lysp_include *inc;
1186
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001187 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001188
1189 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001190 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191
Michal Vasko12ef5362022-09-16 15:13:58 +02001192 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001193
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001194 /* submodules share the namespace with the module names, so there must not be
1195 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001196 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001197 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001198 return LY_EVALID;
1199 }
1200
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001201 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001202 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001203 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001204 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001205 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 +02001206 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001207 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001208 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001209 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 +02001210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001211 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001212 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001214 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001215 LY_CHECK_RET(parse_ext(ctx, word, word_len, inc, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001216 break;
1217 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001218 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219 return LY_EVALID;
1220 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001221 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001222 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001223
1224cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001225 return ret;
1226}
1227
Michal Vaskoea5abea2018-09-18 13:10:54 +02001228/**
1229 * @brief Parse the import statement.
1230 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001231 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001232 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001233 * @param[in,out] imports Parsed imports to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001234 * @return LY_ERR values.
1235 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001237parse_import(struct lysp_yang_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001238{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001239 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001241 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001242 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001243 struct lysp_import *imp;
1244
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001245 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246
1247 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001248 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001249 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001250
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001251 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001253 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001254 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 +02001255 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001256 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001257 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001258 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001259 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 +02001260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001261 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001262 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001263 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 +02001264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001265 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001266 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001267 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001268 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001269 LY_CHECK_RET(parse_ext(ctx, word, word_len, imp, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001270 break;
1271 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001272 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001273 return LY_EVALID;
1274 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001275 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001277
Michal Vasko7fbc8162018-09-17 10:35:16 +02001278 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001279 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280
Michal Vasko12ef5362022-09-16 15:13:58 +02001281cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001282 return ret;
1283}
1284
Michal Vaskoea5abea2018-09-18 13:10:54 +02001285/**
1286 * @brief Parse the revision statement.
1287 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001288 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001289 * @param[in,out] revs Parsed revisions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001290 * @return LY_ERR values.
1291 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001292static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001293parse_revision(struct lysp_yang_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001294{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001295 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001297 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001298 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001299 struct lysp_revision *rev;
1300
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001301 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001302
1303 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001304 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001305
1306 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001307 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001308 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001309 return LY_EVALID;
1310 }
1311
Radek Krejcib7db73a2018-10-24 14:18:40 +02001312 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001313 free(buf);
1314
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001315 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001317 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001318 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 +02001319 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001320 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001321 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 +02001322 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001323 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001324 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325 break;
1326 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001327 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001328 return LY_EVALID;
1329 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001330 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001332
1333cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 return ret;
1335}
1336
Michal Vaskoea5abea2018-09-18 13:10:54 +02001337/**
1338 * @brief Parse a generic text field that can have more instances such as base.
1339 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001340 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001341 * @param[in] parent Current statement parent.
1342 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001343 * @param[in,out] texts Parsed values to add to.
1344 * @param[in] arg Type of the expected argument.
1345 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001346 * @return LY_ERR values.
1347 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001349parse_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 +02001350 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001351{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001352 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001354 const char **item;
1355 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001356 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001357
1358 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001359 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001360
1361 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001362 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001363
Michal Vasko12ef5362022-09-16 15:13:58 +02001364 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001365 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001367 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001368 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 +02001369 break;
1370 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001371 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001372 return LY_EVALID;
1373 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001374 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001375 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001376
1377cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001378 return ret;
1379}
1380
Michal Vaskoea5abea2018-09-18 13:10:54 +02001381/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001382 * @brief Parse a generic text field that can have more instances such as base.
1383 *
1384 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001385 * @param[in] parent_stmt Type of statement stored in @p qnames.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001386 * @param[in,out] qnames Parsed qnames to add to.
1387 * @param[in] arg Type of the expected argument.
1388 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001389 * @return LY_ERR values.
1390 */
1391static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001392parse_qnames(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_qname **qnames, enum yang_arg arg,
1393 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +02001394{
1395 LY_ERR ret = LY_SUCCESS;
1396 char *buf, *word;
1397 struct lysp_qname *item;
1398 size_t word_len;
1399 enum ly_stmt kw;
1400
1401 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001402 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001403
1404 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001405 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001406
Michal Vasko12ef5362022-09-16 15:13:58 +02001407 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001408 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001409 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001410 switch (kw) {
1411 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001412 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 +02001413 break;
1414 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001415 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001416 return LY_EVALID;
1417 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001418 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001419 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001420
1421cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001422 return ret;
1423}
1424
1425/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001426 * @brief Parse the config statement.
1427 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001428 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001429 * @param[in,out] flags Flags to add to.
1430 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001431 * @return LY_ERR values.
1432 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001433static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001434parse_config(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001436 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001438 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001439 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440
1441 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001442 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443 return LY_EVALID;
1444 }
1445
1446 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001447 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448
Radek Krejcif13b87b2020-12-01 22:02:17 +01001449 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001451 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001452 *flags |= LYS_CONFIG_R;
1453 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001454 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001455 free(buf);
1456 return LY_EVALID;
1457 }
1458 free(buf);
1459
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001460 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001462 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001463 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001464 break;
1465 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001466 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001467 return LY_EVALID;
1468 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001469 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001470 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001471
1472cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473 return ret;
1474}
1475
Michal Vaskoea5abea2018-09-18 13:10:54 +02001476/**
1477 * @brief Parse the mandatory statement.
1478 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001479 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001480 * @param[in,out] flags Flags to add to.
1481 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001482 * @return LY_ERR values.
1483 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001485parse_mandatory(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001487 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001489 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001490 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491
1492 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001493 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001494 return LY_EVALID;
1495 }
1496
1497 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001498 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001499
Radek Krejcif13b87b2020-12-01 22:02:17 +01001500 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001502 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 *flags |= LYS_MAND_FALSE;
1504 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001505 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001506 free(buf);
1507 return LY_EVALID;
1508 }
1509 free(buf);
1510
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001511 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001512 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001513 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001514 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515 break;
1516 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001517 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001518 return LY_EVALID;
1519 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001520 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001521 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001522
1523cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001524 return ret;
1525}
1526
Michal Vaskoea5abea2018-09-18 13:10:54 +02001527/**
1528 * @brief Parse a restriction such as range or length.
1529 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001530 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001531 * @param[in] restr_kw Type of this particular restriction.
1532 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001533 * @return LY_ERR values.
1534 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001536parse_restr(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001538 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001539 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001540 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001541 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001542
1543 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001544 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001545
Michal Vasko193dacd2022-10-13 08:43:05 +02001546 CHECK_NONEMPTY(ctx, word_len, lyplg_ext_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001547 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001548 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001549 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001551 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001552 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 +02001553 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001554 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001555 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 +02001556 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001557 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02001558 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG,
1559 &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001561 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001562 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 +02001563 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001564 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001565 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001566 break;
1567 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001568 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569 return LY_EVALID;
1570 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001571 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001573
1574cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001575 return ret;
1576}
1577
Michal Vaskoea5abea2018-09-18 13:10:54 +02001578/**
1579 * @brief Parse a restriction that can have more instances such as must.
1580 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001581 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001582 * @param[in] restr_kw Type of this particular restriction.
1583 * @param[in,out] restrs Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001584 * @return LY_ERR values.
1585 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001587parse_restrs(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588{
1589 struct lysp_restr *restr;
1590
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001591 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001592 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001593}
1594
Michal Vaskoea5abea2018-09-18 13:10:54 +02001595/**
1596 * @brief Parse the status statement.
1597 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001598 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001599 * @param[in,out] flags Flags to add to.
1600 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001601 * @return LY_ERR values.
1602 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001604parse_status(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001605{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001606 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001608 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001609 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001610
1611 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001612 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001613 return LY_EVALID;
1614 }
1615
1616 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001617 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001618
Radek Krejcif13b87b2020-12-01 22:02:17 +01001619 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001620 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001621 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001623 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 *flags |= LYS_STATUS_OBSLT;
1625 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001626 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627 free(buf);
1628 return LY_EVALID;
1629 }
1630 free(buf);
1631
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001632 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001634 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001635 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 break;
1637 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001638 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639 return LY_EVALID;
1640 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001641 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001642 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001643
1644cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001645 return ret;
1646}
1647
Michal Vaskoea5abea2018-09-18 13:10:54 +02001648/**
1649 * @brief Parse the when statement.
1650 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001651 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001652 * @param[in,out] when_p When pointer to parse to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001653 * @return LY_ERR values.
1654 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001655LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001656parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001657{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001658 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001659 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001660 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001661 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001662 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001663 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664
1665 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001666 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667 return LY_EVALID;
1668 }
1669
1670 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001671 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672
1673 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001674 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001675 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001676 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001677
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001678 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001680 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001681 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->dsc, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts),
1682 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001683 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001684 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001685 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->ref, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts),
1686 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001688 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001689 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 +02001690 break;
1691 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001692 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001693 ret = LY_EVALID;
1694 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001695 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001696 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001698
1699cleanup:
1700 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001701 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001702 free(when);
1703 } else {
1704 *when_p = when;
1705 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001706 return ret;
1707}
1708
Michal Vaskoea5abea2018-09-18 13:10:54 +02001709/**
1710 * @brief Parse the anydata or anyxml statement.
1711 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001712 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001713 * @param[in] any_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001714 * @param[in] parent Node parent.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001715 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001716 * @return LY_ERR values.
1717 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001718LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001719parse_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 +02001720{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001721 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001722 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001723 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001725 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001726
David Sedlák60adc092019-08-06 15:57:02 +02001727 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001728 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001729
Radek Krejci39b7fc22021-02-26 23:29:18 +01001730 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001731 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001732
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001734 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001735 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736
1737 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001738 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001740 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001741 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001742 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001743 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001744 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 +02001745 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001746 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001747 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001748 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001749 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001750 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001751 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001752 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001753 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001755 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001756 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 +02001757 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001758 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001759 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001760 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001761 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001762 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001764 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001765 LY_CHECK_RET(parse_ext(ctx, word, word_len, any, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766 break;
1767 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001768 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001769 return LY_EVALID;
1770 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001771 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001773
1774cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001775 return ret;
1776}
1777
Michal Vaskoea5abea2018-09-18 13:10:54 +02001778/**
1779 * @brief Parse the value or position statement. Substatement of type enum statement.
1780 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001781 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001782 * @param[in] val_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001783 * @param[in,out] enm Structure to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001784 * @return LY_ERR values.
1785 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001786LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001787parse_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 +02001788{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001789 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001790 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001791 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001792 long long num = 0;
1793 unsigned long long unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001794 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001795
Michal Vasko193dacd2022-10-13 08:43:05 +02001796 if (enm->flags & LYS_SET_VALUE) {
1797 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001798 ret = LY_EVALID;
1799 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001800 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001801 enm->flags |= LYS_SET_VALUE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001802
1803 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001804 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001805
Radek Krejcid6b76452019-09-03 17:03:03 +02001806 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001807 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001808 ret = LY_EVALID;
1809 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001810 }
1811
1812 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001813 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001814 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001815 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001816 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001817 ret = LY_EVALID;
1818 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001819 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001820 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001821 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001822 if (unum > UINT64_C(4294967295)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001823 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001824 ret = LY_EVALID;
1825 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001826 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001827 }
1828 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001829 if ((size_t)(ptr - word) != word_len) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001830 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001831 ret = LY_EVALID;
1832 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001833 }
1834 if (errno == ERANGE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001835 LOGVAL_PARSER(ctx, LY_VCODE_OOB, 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 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001839 if (val_kw == LY_STMT_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001840 enm->value = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001841 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02001842 enm->value = unum;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001845 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001847 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001848 ret = parse_ext(ctx, word, word_len, enm, val_kw, 0, &enm->exts);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001849 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001850 break;
1851 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001852 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001853 ret = LY_EVALID;
1854 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001855 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001856 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001857 }
Radek Krejci8b764662018-11-14 14:15:13 +01001858
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001859cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001860 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001861 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862}
1863
Michal Vaskoea5abea2018-09-18 13:10:54 +02001864/**
1865 * @brief Parse the enum or bit statement. Substatement of type statement.
1866 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001867 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001868 * @param[in] enum_kw Type of this particular keyword.
1869 * @param[in,out] enums Enums or bits to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001870 * @return LY_ERR values.
1871 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001872static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001873parse_type_enum(struct lysp_yang_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001875 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001877 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001878 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879 struct lysp_type_enum *enm;
1880
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001881 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001882
1883 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001884 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 +02001885 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001886 ret = lysp_check_enum_name((struct lysp_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001887 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001888 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001889
Michal Vasko12ef5362022-09-16 15:13:58 +02001890 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001891 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(enum_kw), enm->name);
Radek Krejci8b764662018-11-14 14:15:13 +01001892
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001893 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001894 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001895 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001896 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 +02001897 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001898 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001899 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001900 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001901 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001902 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001903 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 +02001904 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001905 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001906 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001907 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001908 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001909 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1910 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1911 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
David Sedlák9fb515f2019-07-11 10:33:58 +02001912 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001913 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001914 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, 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));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001917 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001918 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001919 LY_CHECK_RET(parse_ext(ctx, word, word_len, enm, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001920 break;
1921 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001922 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001923 return LY_EVALID;
1924 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001925 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001927
1928cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929 return ret;
1930}
1931
Michal Vaskoea5abea2018-09-18 13:10:54 +02001932/**
1933 * @brief Parse the fraction-digits statement. Substatement of type statement.
1934 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001935 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001936 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001937 * @return LY_ERR values.
1938 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001939static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001940parse_type_fracdigits(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001941{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001942 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001943 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001944 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001945 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001946 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001947
Michal Vasko193dacd2022-10-13 08:43:05 +02001948 if (type->fraction_digits) {
David Sedlákb3077192019-06-19 10:55:37 +02001949 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950 return LY_EVALID;
1951 }
1952
1953 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001954 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001955
1956 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001957 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001958 ret = LY_EVALID;
1959 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960 }
1961
1962 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001963 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001964 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001965 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001966 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001967 ret = LY_EVALID;
1968 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001970 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001971 LOGVAL_PARSER(ctx, LY_VCODE_OOB, 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 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001975 type->fraction_digits = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001977 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001979 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001980 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 +02001981 break;
1982 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001983 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001984 ret = LY_EVALID;
1985 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001986 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001987 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001988 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001989
1990cleanup:
1991 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 return ret;
1993}
1994
Michal Vaskoea5abea2018-09-18 13:10:54 +02001995/**
1996 * @brief Parse the require-instance statement. Substatement of type statement.
1997 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001998 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001999 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002000 * @return LY_ERR values.
2001 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002002static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002003parse_type_reqinstance(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002004{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002005 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002006 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002007 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002008 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002009
Michal Vasko193dacd2022-10-13 08:43:05 +02002010 if (type->flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002011 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002012 return LY_EVALID;
2013 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002014 type->flags |= LYS_SET_REQINST;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002015
2016 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002017 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018
Radek Krejcif13b87b2020-12-01 22:02:17 +01002019 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002020 type->require_instance = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002021 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002022 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002023 ret = LY_EVALID;
2024 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002027 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002029 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002030 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 +02002031 break;
2032 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002033 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002034 ret = LY_EVALID;
2035 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002036 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002037 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002039
2040cleanup:
2041 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042 return ret;
2043}
2044
Michal Vaskoea5abea2018-09-18 13:10:54 +02002045/**
2046 * @brief Parse the modifier statement. Substatement of type pattern statement.
2047 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002048 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002049 * @param[in,out] restr Restriction to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002050 * @return LY_ERR values.
2051 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002053parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002054{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002055 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002056 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002057 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002058 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002059
Michal Vasko193dacd2022-10-13 08:43:05 +02002060 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002061 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 return LY_EVALID;
2063 }
2064
2065 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002066 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002067
Radek Krejcif13b87b2020-12-01 22:02:17 +01002068 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002069 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002070 ret = LY_EVALID;
2071 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002072 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002073
2074 /* replace the value in the dictionary */
Michal Vasko193dacd2022-10-13 08:43:05 +02002075 buf = malloc(strlen(restr->arg.str) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002076 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02002077 strcpy(buf, restr->arg.str);
2078 lydict_remove(PARSER_CTX(ctx), restr->arg.str);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002079
Radek Krejcif13b87b2020-12-01 22:02:17 +01002080 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2081 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko183ea332022-11-08 10:50:03 +01002082 ret = lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002083 buf = NULL;
Michal Vasko183ea332022-11-08 10:50:03 +01002084 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002085
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002086 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002087 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002088 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002089 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 +02002090 break;
2091 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002092 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002093 ret = LY_EVALID;
2094 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002096 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002097 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002098
2099cleanup:
2100 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002101 return ret;
2102}
2103
Michal Vaskoea5abea2018-09-18 13:10:54 +02002104/**
2105 * @brief Parse the pattern statement. Substatement of type statement.
2106 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002107 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002108 * @param[in,out] patterns Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002109 * @return LY_ERR values.
2110 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002111static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002112parse_type_pattern(struct lysp_yang_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002113{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002114 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002116 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002117 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002118 struct lysp_restr *restr;
2119
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002120 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121
2122 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002123 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002124
2125 /* add special meaning first byte */
2126 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002127 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002128 word = buf;
2129 } else {
2130 buf = malloc(word_len + 2);
2131 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002132 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002133 if (word_len) {
2134 memmove(buf + 1, word, word_len);
2135 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002136 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002137 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002138 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002139 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002140
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002141 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002142 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002143 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002144 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 +02002145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002146 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002147 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 +02002148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002149 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02002150 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 +02002151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002152 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002153 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 +02002154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002155 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002156 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko193dacd2022-10-13 08:43:05 +02002157 LY_CHECK_RET(parse_type_pattern_modifier(ctx, restr));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002159 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002160 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002161 break;
2162 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002163 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002164 return LY_EVALID;
2165 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002166 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002167 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002168
2169cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002170 return ret;
2171}
2172
Michal Vaskoea5abea2018-09-18 13:10:54 +02002173/**
2174 * @brief Parse the type statement.
2175 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002176 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002177 * @param[in,out] type Type to wrote to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002178 * @return LY_ERR values.
2179 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002181parse_type(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002183 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002184 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002185 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002186 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002187 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 struct lysp_type *nest_type;
2189
2190 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002191 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 return LY_EVALID;
2193 }
2194
2195 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002196 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002197 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002198
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002199 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002200 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002201
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002202 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002203 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002204 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002205 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 +01002206 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002208 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002209 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002210 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002212 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002213 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002214 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002216 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002217 LY_CHECK_RET(parse_type_fracdigits(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002218 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002220 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002221 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002222 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002223 return LY_EVALID;
2224 }
2225 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002226 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002227
Radek Krejci33090f92020-12-17 20:12:46 +01002228 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002229 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002231 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002232 if (type->path) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002233 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002234 return LY_EVALID;
2235 }
2236
aPiecek4bb1e372021-05-07 11:01:00 +02002237 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2238 * corresponding structure, so in case of failure, the lysp_module_free function will take
2239 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2240 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2241 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002242 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 +02002243 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002244 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002245 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002246 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002247 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002248 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002249 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002251 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002252 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002253 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002255 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002257 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258 return LY_EVALID;
2259 }
2260 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002261 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002262
Radek Krejci33090f92020-12-17 20:12:46 +01002263 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002264 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002266 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002267 LY_CHECK_RET(parse_type_reqinstance(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002268 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002270 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002271 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002272 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002273 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002274 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002275 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002276 LY_CHECK_RET(parse_ext(ctx, word, word_len, type, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277 break;
2278 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002279 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002280 return LY_EVALID;
2281 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002282 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002284
2285cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 return ret;
2287}
2288
Michal Vaskoea5abea2018-09-18 13:10:54 +02002289/**
2290 * @brief Parse the leaf statement.
2291 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002292 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002293 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002294 * @return LY_ERR values.
2295 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002296LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002297parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002299 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002300 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002301 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002302 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002303 struct lysp_node_leaf *leaf;
2304
David Sedlák60adc092019-08-06 15:57:02 +02002305 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002306 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002308 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002311 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002312 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313
2314 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002315 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002317 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002318 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002320 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002321 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 +01002322 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002324 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002325 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 +02002326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002327 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002328 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002330 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002331 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002332 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002333 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002334 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002335 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002336 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002337 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 +02002338 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002339 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002340 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002341 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002342 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002343 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002345 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002346 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 +02002347 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002348 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002349 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002351 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002352 LY_CHECK_RET(parse_ext(ctx, word, word_len, leaf, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002353 break;
2354 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002355 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002356 return LY_EVALID;
2357 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002358 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002360
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361 /* mandatory substatements */
2362 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002363 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 return LY_EVALID;
2365 }
2366
Michal Vasko12ef5362022-09-16 15:13:58 +02002367cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368 return ret;
2369}
2370
Michal Vaskoea5abea2018-09-18 13:10:54 +02002371/**
2372 * @brief Parse the max-elements statement.
2373 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002374 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002375 * @param[in,out] max Value to write to.
2376 * @param[in,out] flags Flags to write to.
2377 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002378 * @return LY_ERR values.
2379 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002380LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002381parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002383 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002384 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002385 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002386 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002387 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388
2389 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002390 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 return LY_EVALID;
2392 }
2393 *flags |= LYS_SET_MAX;
2394
2395 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002396 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397
2398 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002399 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002400 ret = LY_EVALID;
2401 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002402 }
2403
Radek Krejci7f9b6512019-09-18 13:11:09 +02002404 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002405 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002406 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002407 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002408 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002409 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002410 ret = LY_EVALID;
2411 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412 }
2413 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002414 LOGVAL_PARSER(ctx, LY_VCODE_OOB, 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
2419 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002420 } else {
2421 /* unbounded */
2422 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002424
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002425 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002427 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002428 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 +02002429 break;
2430 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002431 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002432 ret = LY_EVALID;
2433 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002434 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002435 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002437
2438cleanup:
2439 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002440 return ret;
2441}
2442
Michal Vaskoea5abea2018-09-18 13:10:54 +02002443/**
2444 * @brief Parse the min-elements statement.
2445 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002446 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002447 * @param[in,out] min Value to write to.
2448 * @param[in,out] flags Flags to write to.
2449 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002450 * @return LY_ERR values.
2451 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002452LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002453parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002455 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002456 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002457 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002458 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002459 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460
2461 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002462 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 return LY_EVALID;
2464 }
2465 *flags |= LYS_SET_MIN;
2466
2467 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002468 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469
2470 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002471 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002472 ret = LY_EVALID;
2473 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002474 }
2475
2476 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002477 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002479 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002480 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002481 ret = LY_EVALID;
2482 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483 }
2484 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002485 LOGVAL_PARSER(ctx, LY_VCODE_OOB, 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 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002491 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002492 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002493 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002494 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 +02002495 break;
2496 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002497 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002498 ret = LY_EVALID;
2499 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002501 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002503
2504cleanup:
2505 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002506 return ret;
2507}
2508
Michal Vaskoea5abea2018-09-18 13:10:54 +02002509/**
2510 * @brief Parse the ordered-by statement.
2511 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002512 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002513 * @param[in,out] llist List or leaf-list to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002514 * @return LY_ERR values.
2515 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002517parse_orderedby(struct lysp_yang_ctx *ctx, struct lysp_node *llist)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002519 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002520 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002521 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002522 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523
Michal Vasko193dacd2022-10-13 08:43:05 +02002524 if (llist->flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002525 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002526 return LY_EVALID;
2527 }
2528
2529 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002530 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531
Radek Krejcif13b87b2020-12-01 22:02:17 +01002532 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002533 llist->flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002534 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002535 llist->flags |= LYS_ORDBY_USER;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002536 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002537 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002538 ret = LY_EVALID;
2539 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002542 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002544 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002545 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 +02002546 break;
2547 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002548 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002549 ret = LY_EVALID;
2550 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002551 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002552 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002553 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002554
2555cleanup:
2556 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 return ret;
2558}
2559
Michal Vaskoea5abea2018-09-18 13:10:54 +02002560/**
2561 * @brief Parse the leaf-list statement.
2562 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002563 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002564 * @param[in,out] siblings Siblings to add to.
2565 *
2566 * @return LY_ERR values.
2567 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002568LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002569parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002571 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002573 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002574 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 struct lysp_node_leaflist *llist;
2576
David Sedlák60adc092019-08-06 15:57:02 +02002577 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002578 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002579 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002580 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581
Michal Vasko7fbc8162018-09-17 10:35:16 +02002582 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002583 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002584 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002585
2586 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002587 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002588 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002589 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002590 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002591 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002592 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002593 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002594 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002595 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002596 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002597 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 +02002598 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002599 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002600 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002601 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002602 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002603 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002604 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002605 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002606 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002608 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002609 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002611 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02002612 LY_CHECK_RET(parse_orderedby(ctx, &llist->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002614 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002615 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 +02002616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002617 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002618 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002619 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002620 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002621 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002623 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002624 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 +02002625 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002626 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002627 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002628 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002629 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002630 LY_CHECK_RET(parse_ext(ctx, word, word_len, llist, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002631 break;
2632 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002633 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002634 return LY_EVALID;
2635 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002636 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002637 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002638
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 /* mandatory substatements */
2640 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002641 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642 return LY_EVALID;
2643 }
2644
Michal Vasko12ef5362022-09-16 15:13:58 +02002645cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646 return ret;
2647}
2648
Michal Vaskoea5abea2018-09-18 13:10:54 +02002649/**
2650 * @brief Parse the refine statement.
2651 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002652 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002653 * @param[in,out] refines Refines to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002654 * @return LY_ERR values.
2655 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002656static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002657parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002659 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002661 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002662 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 struct lysp_refine *rf;
2664
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002665 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666
2667 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002668 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002669 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002670 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002672 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002674 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002675 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002676 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002677 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002678 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002679 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002680 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002681 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 +02002682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002683 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002684 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002685 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002686 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002687 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002688 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002690 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002691 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002693 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002694 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002696 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002697 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002698 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002699 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002700 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 +02002701 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002702 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002703 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 +02002704 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002705 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002706 LY_CHECK_RET(parse_ext(ctx, word, word_len, rf, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002707 break;
2708 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002709 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710 return LY_EVALID;
2711 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002712 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002713 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002714
2715cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002716 return ret;
2717}
2718
Michal Vaskoea5abea2018-09-18 13:10:54 +02002719/**
2720 * @brief Parse the typedef statement.
2721 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002722 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002723 * @param[in,out] typedefs Typedefs to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002724 * @return LY_ERR values.
2725 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002726static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002727parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002729 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002730 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002731 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002732 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733 struct lysp_tpdf *tpdf;
2734
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002735 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736
2737 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002738 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002739 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002740
2741 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002742 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002743 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002744 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002745 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 +01002746 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002747 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002748 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002749 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 +02002750 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002751 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002752 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 +02002753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002754 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002755 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002757 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002758 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002760 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002761 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 +02002762 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002763 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002764 LY_CHECK_RET(parse_ext(ctx, word, word_len, tpdf, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765 break;
2766 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002767 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002768 return LY_EVALID;
2769 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002770 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002771 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002772
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773 /* mandatory substatements */
2774 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002775 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 return LY_EVALID;
2777 }
2778
Radek Krejcibbe09a92018-11-08 09:36:54 +01002779 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002780 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002781 assert(ctx->main_ctx);
2782 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002783 }
2784
Michal Vasko12ef5362022-09-16 15:13:58 +02002785cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002786 return ret;
2787}
2788
Michal Vaskoea5abea2018-09-18 13:10:54 +02002789/**
2790 * @brief Parse the input or output statement.
2791 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002792 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002793 * @param[in] kw Type of this particular keyword
2794 * @param[in,out] inout_p Input/output pointer to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002795 * @return LY_ERR values.
2796 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002798parse_inout(struct lysp_yang_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002799 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002801 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002803 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002804 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002805 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002807 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002808 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002809 return LY_EVALID;
2810 }
2811
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002812 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002813 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2814 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002815 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002816
2817 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002818 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002819 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002820 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002821 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002822 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002823 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002824 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002825 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002826 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002827 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002829 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002830 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002832 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002833 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002835 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002836 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002837 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002838 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002839 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002841 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002842 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002843 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002844 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002845 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002846 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002847 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002848 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002849 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002850 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002851 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002852 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002854 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002855 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_p, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856 break;
2857 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002858 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 return LY_EVALID;
2860 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002861 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002863
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002864cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865 return ret;
2866}
2867
Michal Vaskoea5abea2018-09-18 13:10:54 +02002868/**
2869 * @brief Parse the action statement.
2870 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002871 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002872 * @param[in,out] actions Actions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002873 * @return LY_ERR values.
2874 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002875LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002876parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002877{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002878 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002879 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002880 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002881 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002882 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883
Radek Krejci2a9fc652021-01-22 17:44:34 +01002884 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002885
2886 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002887 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002888 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002889 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002890 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002891
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002892 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002893 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002894 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002895 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 +02002896 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002897 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002898 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002899 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002900 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002901 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 +02002902 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002903 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002904 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905 break;
2906
Radek Krejcid6b76452019-09-03 17:03:03 +02002907 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002908 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002910 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002911 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002912 break;
2913
Radek Krejcid6b76452019-09-03 17:03:03 +02002914 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002915 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002917 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002918 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002920 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002921 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 +02002922 break;
2923 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002924 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 return LY_EVALID;
2926 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002927 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002929
2930 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2931 if (!act->input.nodetype) {
2932 act->input.nodetype = LYS_INPUT;
2933 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002934 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002935 }
2936 if (!act->output.nodetype) {
2937 act->output.nodetype = LYS_OUTPUT;
2938 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002939 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002940 }
2941
Michal Vasko12ef5362022-09-16 15:13:58 +02002942cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943 return ret;
2944}
2945
Michal Vaskoea5abea2018-09-18 13:10:54 +02002946/**
2947 * @brief Parse the notification statement.
2948 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002949 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002950 * @param[in,out] notifs Notifications to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002951 * @return LY_ERR values.
2952 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002953LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002954parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002956 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002958 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002959 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002960 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961
Radek Krejci2a9fc652021-01-22 17:44:34 +01002962 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002963
2964 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002965 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002966 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002967 notif->nodetype = LYS_NOTIF;
2968 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002969
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002970 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002971 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002972 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002973 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 +02002974 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002975 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002976 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002978 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002979 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 +02002980 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002981 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002982 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 break;
2984
Radek Krejcid6b76452019-09-03 17:03:03 +02002985 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002986 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002987 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002988 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002989 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002991 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002992 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002994 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002995 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002996 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002997 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002998 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002999 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003000 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003001 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003002 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003003 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003004 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003006 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003007 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003008 break;
3009
Radek Krejcid6b76452019-09-03 17:03:03 +02003010 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003011 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003012 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003014 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003015 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003017 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003018 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003020 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003021 LY_CHECK_RET(parse_ext(ctx, word, word_len, notif, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003022 break;
3023 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003024 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003025 return LY_EVALID;
3026 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003027 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003029
Michal Vasko12ef5362022-09-16 15:13:58 +02003030cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003031 return ret;
3032}
3033
Michal Vaskoea5abea2018-09-18 13:10:54 +02003034/**
3035 * @brief Parse the grouping statement.
3036 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003037 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003038 * @param[in,out] groupings Groupings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003039 * @return LY_ERR values.
3040 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003041LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003042parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003044 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003046 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003047 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003048 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049
Radek Krejci2a9fc652021-01-22 17:44:34 +01003050 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003051
3052 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003053 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003054 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003055 grp->nodetype = LYS_GROUPING;
3056 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003057
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003058 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003059 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003060 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003061 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 +02003062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003063 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003064 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 +02003065 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003066 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003067 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003068 break;
3069
Radek Krejcid6b76452019-09-03 17:03:03 +02003070 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003071 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003072 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003073 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003074 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003076 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003077 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003079 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003080 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003082 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003083 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003085 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003086 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003088 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003089 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003090 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003091 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003092 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093 break;
3094
Radek Krejcid6b76452019-09-03 17:03:03 +02003095 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003096 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003098 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003099 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003100 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003102 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003103 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003105 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003106 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003107 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003109 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003110 LY_CHECK_RET(parse_ext(ctx, word, word_len, grp, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003111 break;
3112 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003113 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003114 return LY_EVALID;
3115 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003116 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003118
aPiecek63e080d2021-06-29 13:53:28 +02003119 /* store data for collision check */
3120 if (parent) {
3121 assert(ctx->main_ctx);
3122 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3123 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003124
Michal Vasko12ef5362022-09-16 15:13:58 +02003125cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 return ret;
3127}
3128
Michal Vaskoea5abea2018-09-18 13:10:54 +02003129/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003130 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003131 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003132 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003133 * @param[in,out] augments Augments to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003134 * @return LY_ERR values.
3135 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003136LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003137parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003138{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003139 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003141 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003142 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003143 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144
Radek Krejci2a9fc652021-01-22 17:44:34 +01003145 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146
3147 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003148 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003149 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003150 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003151 aug->nodetype = LYS_AUGMENT;
3152 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003153
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003154 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003155 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003156 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003157 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 +02003158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003159 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003160 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003162 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003163 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 +02003164 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003165 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003166 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003168 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003169 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 break;
3171
Radek Krejcid6b76452019-09-03 17:03:03 +02003172 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003173 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003174 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003175 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003176 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003178 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003179 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003181 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003182 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003184 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003185 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003187 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003188 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003189 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003190 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003191 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003193 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003194 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003196 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003197 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003198 break;
3199
Radek Krejcid6b76452019-09-03 17:03:03 +02003200 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003201 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003202 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003204 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003205 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003206 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003208 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003209 LY_CHECK_RET(parse_ext(ctx, word, word_len, aug, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 break;
3211 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003212 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003213 return LY_EVALID;
3214 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003215 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003217
Michal Vasko12ef5362022-09-16 15:13:58 +02003218cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219 return ret;
3220}
3221
Michal Vaskoea5abea2018-09-18 13:10:54 +02003222/**
3223 * @brief Parse the uses statement.
3224 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003225 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003226 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003227 * @return LY_ERR values.
3228 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003229LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003230parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003232 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003234 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003235 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236 struct lysp_node_uses *uses;
3237
David Sedlák60adc092019-08-06 15:57:02 +02003238 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003239 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003241 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003244 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003245 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246
3247 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003248 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003250 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003251 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 +02003252 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003253 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003254 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003256 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003257 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 +02003258 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003259 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003260 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003261 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003262 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003263 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264 break;
3265
Radek Krejcid6b76452019-09-03 17:03:03 +02003266 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003267 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003269 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003270 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003272 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003273 LY_CHECK_RET(parse_ext(ctx, word, word_len, uses, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 break;
3275 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003276 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 return LY_EVALID;
3278 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003279 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003281
Michal Vasko12ef5362022-09-16 15:13:58 +02003282cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283 return ret;
3284}
3285
Michal Vaskoea5abea2018-09-18 13:10:54 +02003286/**
3287 * @brief Parse the case statement.
3288 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003289 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003290 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003291 * @return LY_ERR values.
3292 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003293LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003294parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003296 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003298 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003299 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300 struct lysp_node_case *cas;
3301
David Sedlák60adc092019-08-06 15:57:02 +02003302 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003303 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003305 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003308 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003309 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310
3311 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003312 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003314 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003315 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 +02003316 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003317 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003318 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003320 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003321 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 +02003322 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003323 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003324 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003326 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003327 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003328 break;
3329
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003331 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003332 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003333 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003334 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003336 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003337 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003339 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003340 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003342 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003343 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003345 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003346 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003347 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003348 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003349 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003351 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003352 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003354 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003355 LY_CHECK_RET(parse_ext(ctx, word, word_len, cas, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 break;
3357 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003358 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359 return LY_EVALID;
3360 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003361 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003363
3364cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365 return ret;
3366}
3367
Michal Vaskoea5abea2018-09-18 13:10:54 +02003368/**
3369 * @brief Parse the choice statement.
3370 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003371 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003372 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003373 * @return LY_ERR values.
3374 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003375LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003376parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003378 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003379 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003380 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003381 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003382 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383
David Sedlák60adc092019-08-06 15:57:02 +02003384 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003385 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003386 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003387 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388
Michal Vasko7fbc8162018-09-17 10:35:16 +02003389 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003390 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003391 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392
3393 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003394 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003396 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003397 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003399 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003400 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 +02003401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003402 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003403 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003405 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003406 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003407 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003408 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003409 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 +02003410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003411 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003412 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003414 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003415 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003417 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003418 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 +02003419 &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003420 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 break;
3422
Radek Krejcid6b76452019-09-03 17:03:03 +02003423 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003424 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003425 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003426 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003427 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003429 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003430 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003432 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003433 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003434 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003436 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003437 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003439 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003440 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003442 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003443 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003445 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003446 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003448 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003449 LY_CHECK_RET(parse_ext(ctx, word, word_len, choice, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003450 break;
3451 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003452 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003453 return LY_EVALID;
3454 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003455 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003457
3458cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 return ret;
3460}
3461
Michal Vaskoea5abea2018-09-18 13:10:54 +02003462/**
3463 * @brief Parse the container statement.
3464 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003465 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003466 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003467 * @return LY_ERR values.
3468 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003469LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003470parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471{
3472 LY_ERR ret = 0;
3473 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003474 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003475 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 struct lysp_node_container *cont;
3477
David Sedlák60adc092019-08-06 15:57:02 +02003478 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003479 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003481 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003482
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003484 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003485 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486
3487 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003488 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003490 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003491 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003492 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003493 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003494 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 +02003495 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003496 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003497 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003498 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003499 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003500 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 +02003501 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003502 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003503 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003504 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003505 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003506 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003508 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003509 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 +02003510 break;
3511
Radek Krejcid6b76452019-09-03 17:03:03 +02003512 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003513 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003514 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003515 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003516 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003518 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003519 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003521 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003522 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003524 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003525 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003527 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003528 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003530 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003531 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003533 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003534 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535 break;
3536
Radek Krejcid6b76452019-09-03 17:03:03 +02003537 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003538 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003540 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003541 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003543 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003544 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003545 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003547 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003548 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003549 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003550 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003551 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003552 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003554 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003555 LY_CHECK_RET(parse_ext(ctx, word, word_len, cont, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556 break;
3557 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003558 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003559 return LY_EVALID;
3560 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003561 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003562 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003563
Michal Vasko12ef5362022-09-16 15:13:58 +02003564cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565 return ret;
3566}
3567
Michal Vaskoea5abea2018-09-18 13:10:54 +02003568/**
3569 * @brief Parse the list statement.
3570 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003571 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003572 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003573 * @return LY_ERR values.
3574 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003575LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003576parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003578 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003580 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003581 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 struct lysp_node_list *list;
3583
David Sedlák60adc092019-08-06 15:57:02 +02003584 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003585 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003587 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003590 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003591 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592
3593 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003594 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003596 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003597 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003599 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003600 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 +02003601 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003602 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003603 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003605 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003606 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 +02003607 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003608 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003609 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003610 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003611 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003612 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003614 case LY_STMT_KEY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003615 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 +02003616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003617 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003618 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003619 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003620 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003621 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003623 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003624 LY_CHECK_RET(parse_orderedby(ctx, &list->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003626 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003627 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 break;
3629
Radek Krejcid6b76452019-09-03 17:03:03 +02003630 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003631 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003632 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003633 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003634 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003636 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003637 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003639 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003640 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003642 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003643 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003645 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003646 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003648 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003649 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003651 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003652 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653 break;
3654
Radek Krejcid6b76452019-09-03 17:03:03 +02003655 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003656 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003658 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003659 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003661 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003662 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003663 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003665 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003666 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003668 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003669 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003670 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003672 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003673 LY_CHECK_RET(parse_ext(ctx, word, word_len, list, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674 break;
3675 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003676 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003677 return LY_EVALID;
3678 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003679 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003680 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003681
Michal Vasko12ef5362022-09-16 15:13:58 +02003682cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003683 return ret;
3684}
3685
Michal Vaskoea5abea2018-09-18 13:10:54 +02003686/**
3687 * @brief Parse the yin-element statement.
3688 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003689 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003690 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003691 * @return LY_ERR values.
3692 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003693static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003694parse_yinelement(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003695{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003696 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003697 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003698 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003699 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003700
Michal Vasko193dacd2022-10-13 08:43:05 +02003701 if (ext->flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003702 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003703 return LY_EVALID;
3704 }
3705
3706 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003707 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003708
Radek Krejcif13b87b2020-12-01 22:02:17 +01003709 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003710 ext->flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003711 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003712 ext->flags |= LYS_YINELEM_FALSE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003713 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003714 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715 free(buf);
3716 return LY_EVALID;
3717 }
3718 free(buf);
3719
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003720 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003721 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003722 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003723 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_YIN_ELEMENT, 0, &ext->exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003724 LY_CHECK_RET(ret);
3725 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003727 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728 return LY_EVALID;
3729 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003730 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003731 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003732
3733cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 return ret;
3735}
3736
Michal Vaskoea5abea2018-09-18 13:10:54 +02003737/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003738 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003739 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003740 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003741 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003742 * @return LY_ERR values.
3743 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003744static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003745parse_argument(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003746{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003747 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003749 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003750 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003751
Michal Vasko193dacd2022-10-13 08:43:05 +02003752 if (ext->argname) {
David Sedlákb3077192019-06-19 10:55:37 +02003753 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003754 return LY_EVALID;
3755 }
3756
3757 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003758 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko193dacd2022-10-13 08:43:05 +02003759 INSERT_WORD_GOTO(ctx, buf, ext->argname, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003761 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003763 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003764 LY_CHECK_RET(parse_yinelement(ctx, ext));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003766 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003767 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_ARGUMENT, 0, &ext->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 break;
3769 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003770 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003771 return LY_EVALID;
3772 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003773 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003774 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003775
3776cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003777 return ret;
3778}
3779
Michal Vaskoea5abea2018-09-18 13:10:54 +02003780/**
3781 * @brief Parse the extension statement.
3782 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003783 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003784 * @param[in,out] extensions Extensions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003785 * @return LY_ERR values.
3786 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003787static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003788parse_extension(struct lysp_yang_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003789{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003790 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003792 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003793 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794 struct lysp_ext *ex;
3795
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003796 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003797
3798 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003799 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003800 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003802 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003803 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003804 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003805 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 +02003806 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003807 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003808 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 +02003809 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003810 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003811 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003813 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003814 LY_CHECK_RET(parse_argument(ctx, ex));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003815 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003816 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003817 LY_CHECK_RET(parse_ext(ctx, word, word_len, ex, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003818 break;
3819 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003820 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003821 return LY_EVALID;
3822 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003823 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003824 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003825
3826cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003827 return ret;
3828}
3829
Michal Vaskoea5abea2018-09-18 13:10:54 +02003830/**
3831 * @brief Parse the deviate statement.
3832 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003833 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003834 * @param[in,out] deviates Deviates to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003835 * @return LY_ERR values.
3836 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003837LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003838parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003839{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003840 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003841 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003842 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003843 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003844 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003845 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846 struct lysp_deviate_add *d_add = NULL;
3847 struct lysp_deviate_rpl *d_rpl = NULL;
3848 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003849 const char **d_units = NULL;
3850 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003851 struct lysp_restr **d_musts = NULL;
3852 uint16_t *d_flags = 0;
3853 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003854
3855 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003856 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003857
Radek Krejcif13b87b2020-12-01 22:02:17 +01003858 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003859 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003860 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003862 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003864 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865 dev_mod = LYS_DEV_DELETE;
3866 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003867 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003868 ret = LY_EVALID;
3869 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003871
3872 /* create structure */
3873 switch (dev_mod) {
3874 case LYS_DEV_NOT_SUPPORTED:
3875 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003876 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 break;
3878 case LYS_DEV_ADD:
3879 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003880 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 d = (struct lysp_deviate *)d_add;
3882 d_units = &d_add->units;
3883 d_uniques = &d_add->uniques;
3884 d_dflts = &d_add->dflts;
3885 d_musts = &d_add->musts;
3886 d_flags = &d_add->flags;
3887 d_min = &d_add->min;
3888 d_max = &d_add->max;
3889 break;
3890 case LYS_DEV_REPLACE:
3891 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003892 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003893 d = (struct lysp_deviate *)d_rpl;
3894 d_units = &d_rpl->units;
3895 d_flags = &d_rpl->flags;
3896 d_min = &d_rpl->min;
3897 d_max = &d_rpl->max;
3898 break;
3899 case LYS_DEV_DELETE:
3900 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003901 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 d = (struct lysp_deviate *)d_del;
3903 d_units = &d_del->units;
3904 d_uniques = &d_del->uniques;
3905 d_dflts = &d_del->dflts;
3906 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907 break;
3908 default:
3909 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003910 LOGINT(PARSER_CTX(ctx));
3911 ret = LY_EINT;
3912 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003913 }
3914 d->mod = dev_mod;
3915
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003916 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003918 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919 switch (dev_mod) {
3920 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003921 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003922 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003923 ret = LY_EVALID;
3924 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003926 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003927 break;
3928 }
3929 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003930 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931 switch (dev_mod) {
3932 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02003933 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003934 ret = LY_EVALID;
3935 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003937 ret = parse_text_field(ctx, &d_rpl->dflt, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts);
3938 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003939 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 break;
3941 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003942 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 +02003943 break;
3944 }
3945 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003946 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 switch (dev_mod) {
3948 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003949 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003950 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003951 ret = LY_EVALID;
3952 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003954 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955 break;
3956 }
3957 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003958 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959 switch (dev_mod) {
3960 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003961 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003962 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003963 ret = LY_EVALID;
3964 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003965 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003966 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967 break;
3968 }
3969 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003970 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003971 switch (dev_mod) {
3972 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003973 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003974 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003975 ret = LY_EVALID;
3976 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003977 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003978 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003979 break;
3980 }
3981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003982 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983 switch (dev_mod) {
3984 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003985 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003986 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003987 ret = LY_EVALID;
3988 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003989 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003990 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991 break;
3992 }
3993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003994 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003995 switch (dev_mod) {
3996 case LYS_DEV_NOT_SUPPORTED:
3997 case LYS_DEV_ADD:
3998 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003999 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004000 ret = LY_EVALID;
4001 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004002 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004003 if (d_rpl->type) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004004 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004005 ret = LY_EVALID;
4006 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004007 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004008 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004009 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4010 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011 break;
4012 }
4013 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004014 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004015 switch (dev_mod) {
4016 case LYS_DEV_NOT_SUPPORTED:
4017 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004018 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004019 ret = LY_EVALID;
4020 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004021 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004022 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 +02004023 break;
4024 }
4025 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004026 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004027 switch (dev_mod) {
4028 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02004029 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004030 ret = LY_EVALID;
4031 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004032 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004033 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 +02004034 break;
4035 }
4036 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004037 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004038 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 +02004039 break;
4040 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004041 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004042 ret = LY_EVALID;
4043 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004044 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004045 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004046 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004047
4048cleanup:
4049 free(buf);
4050 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004051 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004052 free(d);
4053 } else {
4054 /* insert into siblings */
4055 LY_LIST_INSERT(deviates, d, next);
4056 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 return ret;
4058}
4059
Michal Vaskoea5abea2018-09-18 13:10:54 +02004060/**
4061 * @brief Parse the deviation statement.
4062 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004063 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004064 * @param[in,out] deviations Deviations to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004065 * @return LY_ERR values.
4066 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004067LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004068parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004069{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004070 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004071 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004072 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004073 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004074 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004075 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004077 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078
4079 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004080 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004081 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004082 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004083
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004084 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004085 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004086 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004087 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 +02004088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004089 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004090 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004091 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004092 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004093 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 +02004094 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004095 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004096 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 +02004097 break;
4098 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004099 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004100 ret = LY_EVALID;
4101 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004103 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004104 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004105
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106 /* mandatory substatements */
4107 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004108 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004109 ret = LY_EVALID;
4110 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111 }
4112
Michal Vasko12ef5362022-09-16 15:13:58 +02004113cleanup:
4114 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004115 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004116 LY_ARRAY_DECREMENT_FREE(*deviations);
4117 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004118 return ret;
4119}
4120
Michal Vaskoea5abea2018-09-18 13:10:54 +02004121/**
4122 * @brief Parse the feature statement.
4123 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004124 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004125 * @param[in,out] features Features to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004126 * @return LY_ERR values.
4127 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004128LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004129parse_feature(struct lysp_yang_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004130{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004131 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004132 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004133 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004134 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004135 struct lysp_feature *feat;
4136
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004137 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004138
4139 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004140 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004141 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004142
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004143 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004144 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004145 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004146 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 +02004147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004148 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004149 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004151 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004152 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 +02004153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004154 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004155 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004157 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004158 LY_CHECK_RET(parse_ext(ctx, word, word_len, feat, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 break;
4160 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004161 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004162 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004164 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004166
4167cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 return ret;
4169}
4170
Michal Vaskoea5abea2018-09-18 13:10:54 +02004171/**
4172 * @brief Parse the identity statement.
4173 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004174 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004175 * @param[in,out] identities Identities to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004176 * @return LY_ERR values.
4177 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004178LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004179parse_identity(struct lysp_yang_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004180{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004181 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004182 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004183 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004184 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004185 struct lysp_ident *ident;
4186
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004187 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004188
4189 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004190 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004191 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004192
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004193 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004194 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004195 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004196 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 +02004197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004198 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004199 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004200 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004202 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004203 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 +02004204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004205 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004206 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004208 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004209 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004210 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 +01004211 return LY_EVALID;
4212 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004213 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 +02004214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004215 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004216 LY_CHECK_RET(parse_ext(ctx, word, word_len, ident, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004217 break;
4218 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004219 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004220 return LY_EVALID;
4221 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004222 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004223 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004224
4225cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004226 return ret;
4227}
4228
Michal Vaskoea5abea2018-09-18 13:10:54 +02004229/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004230 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004231 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004232 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004233 * @param[in,out] mod Module to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004234 * @return LY_ERR values.
4235 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004236LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004237parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004238{
4239 LY_ERR ret = 0;
4240 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004241 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004242 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004243 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004244 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004245
Michal Vaskoc3781c32020-10-06 14:04:08 +02004246 mod->is_submod = 0;
4247
4248 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004249 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004250 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004251
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004252 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004253
Radek Krejcie3846472018-10-15 15:24:51 +02004254#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004255 if (mod_stmt > SECTION) {\
4256 LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;\
4257 } mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004258
Michal Vasko7fbc8162018-09-17 10:35:16 +02004259 switch (kw) {
4260 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004261 case LY_STMT_NAMESPACE:
4262 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004263 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004265 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004266 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004267 break;
4268 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004269 case LY_STMT_INCLUDE:
4270 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004271 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004272 break;
4273 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004274 case LY_STMT_ORGANIZATION:
4275 case LY_STMT_CONTACT:
4276 case LY_STMT_DESCRIPTION:
4277 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004278 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004279 break;
4280
4281 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004282 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004283 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004284 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004285 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004286 case LY_STMT_ANYDATA:
4287 case LY_STMT_ANYXML:
4288 case LY_STMT_AUGMENT:
4289 case LY_STMT_CHOICE:
4290 case LY_STMT_CONTAINER:
4291 case LY_STMT_DEVIATION:
4292 case LY_STMT_EXTENSION:
4293 case LY_STMT_FEATURE:
4294 case LY_STMT_GROUPING:
4295 case LY_STMT_IDENTITY:
4296 case LY_STMT_LEAF:
4297 case LY_STMT_LEAF_LIST:
4298 case LY_STMT_LIST:
4299 case LY_STMT_NOTIFICATION:
4300 case LY_STMT_RPC:
4301 case LY_STMT_TYPEDEF:
4302 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004303 mod_stmt = Y_MOD_BODY;
4304 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004305 case LY_STMT_EXTENSION_INSTANCE:
4306 /* no place in the statement order defined */
4307 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004308 default:
4309 /* error handled in the next switch */
4310 break;
4311 }
Radek Krejcie3846472018-10-15 15:24:51 +02004312#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004313
Radek Krejcie3846472018-10-15 15:24:51 +02004314 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004315 switch (kw) {
4316 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004317 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004318 LY_CHECK_RET(parse_yangversion(ctx, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004319 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004320 case LY_STMT_NAMESPACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004321 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 +02004322 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004323 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02004324 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 +02004325 break;
4326
4327 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004328 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004329 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004331 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004332 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004333 break;
4334
4335 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004336 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004337 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 +02004338 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004339 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004340 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 +02004341 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004342 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004343 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 +02004344 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004345 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004346 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 +02004347 break;
4348
4349 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004350 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004351 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004352 break;
4353
4354 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004355 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004356 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004357 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004358 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004359 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004360 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004361 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004362 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004364 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004365 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004367 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004368 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004370 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004371 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004373 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004374 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004376 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004377 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004378 break;
4379
Radek Krejcid6b76452019-09-03 17:03:03 +02004380 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004381 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004383 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004384 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004386 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004387 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004389 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004390 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004392 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004393 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004395 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004396 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004397 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004398 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004399 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004401 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004402 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004404 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004405 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004407 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004408 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004409 break;
4410
4411 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004412 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004413 return LY_EVALID;
4414 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004415 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004416 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004417
Michal Vasko7fbc8162018-09-17 10:35:16 +02004418 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004419 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004420 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004421 return LY_EVALID;
4422 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004423 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004424 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004425 }
4426
Radek Krejcie9e987e2018-10-31 12:50:27 +01004427 /* submodules share the namespace with the module names, so there must not be
4428 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004429 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004430 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004431 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004432 return LY_EVALID;
4433 }
4434
Michal Vasko12ef5362022-09-16 15:13:58 +02004435cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004436 return ret;
4437}
4438
4439/**
4440 * @brief Parse submodule substatements.
4441 *
4442 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004443 * @param[out] submod Parsed submodule structure.
4444 *
4445 * @return LY_ERR values.
4446 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004447LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004448parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004449{
4450 LY_ERR ret = 0;
4451 char *buf, *word;
4452 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004453 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004454 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004455 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004456
Michal Vaskoc3781c32020-10-06 14:04:08 +02004457 submod->is_submod = 1;
4458
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004459 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004460 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004461 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004462
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004463 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004464
4465#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004466 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 +01004467
4468 switch (kw) {
4469 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004470 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004471 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4472 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004473 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004474 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4475 break;
4476 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004477 case LY_STMT_INCLUDE:
4478 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004479 CHECK_ORDER(Y_MOD_LINKAGE);
4480 break;
4481 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004482 case LY_STMT_ORGANIZATION:
4483 case LY_STMT_CONTACT:
4484 case LY_STMT_DESCRIPTION:
4485 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004486 CHECK_ORDER(Y_MOD_META);
4487 break;
4488
4489 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004490 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004491 CHECK_ORDER(Y_MOD_REVISION);
4492 break;
4493 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004494 case LY_STMT_ANYDATA:
4495 case LY_STMT_ANYXML:
4496 case LY_STMT_AUGMENT:
4497 case LY_STMT_CHOICE:
4498 case LY_STMT_CONTAINER:
4499 case LY_STMT_DEVIATION:
4500 case LY_STMT_EXTENSION:
4501 case LY_STMT_FEATURE:
4502 case LY_STMT_GROUPING:
4503 case LY_STMT_IDENTITY:
4504 case LY_STMT_LEAF:
4505 case LY_STMT_LEAF_LIST:
4506 case LY_STMT_LIST:
4507 case LY_STMT_NOTIFICATION:
4508 case LY_STMT_RPC:
4509 case LY_STMT_TYPEDEF:
4510 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004511 mod_stmt = Y_MOD_BODY;
4512 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004513 case LY_STMT_EXTENSION_INSTANCE:
4514 /* no place in the statement order defined */
4515 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004516 default:
4517 /* error handled in the next switch */
4518 break;
4519 }
4520#undef CHECK_ORDER
4521
4522 prev_kw = kw;
4523 switch (kw) {
4524 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004525 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004526 LY_CHECK_RET(parse_yangversion(ctx, (struct lysp_module *)submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004527 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004528 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02004529 LY_CHECK_RET(parse_belongsto(ctx, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004530 break;
4531
4532 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004533 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004534 if (submod->version == LYS_VERSION_1_1) {
4535 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4536 submod->name);
4537 }
Radek Krejci33090f92020-12-17 20:12:46 +01004538 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004539 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004540 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004541 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004542 break;
4543
4544 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004545 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004546 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 +01004547 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004548 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004549 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 +01004550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004551 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004552 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 +01004553 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004554 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004555 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 +01004556 break;
4557
4558 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004559 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004560 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004561 break;
4562
4563 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004564 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004565 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004566 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004567 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004568 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004569 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004570 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004571 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004572 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004573 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004574 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004575 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004576 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004577 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004578 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004579 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004580 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004581 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004582 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004583 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004584 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004585 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004586 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004587 break;
4588
Radek Krejcid6b76452019-09-03 17:03:03 +02004589 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004590 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004591 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004592 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004593 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004594 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004595 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004596 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004597 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004598 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004599 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004601 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004602 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004604 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004605 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004607 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004608 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004610 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004611 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004613 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004614 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004616 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004617 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004618 break;
4619
4620 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004621 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004622 return LY_EVALID;
4623 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004624 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004625 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004626
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004627 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004628 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004629 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004630 return LY_EVALID;
4631 }
4632
4633 /* submodules share the namespace with the module names, so there must not be
4634 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004635 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004636 /* main modules may have different revisions */
4637 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004638 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004639 return LY_EVALID;
4640 }
4641
Michal Vasko12ef5362022-09-16 15:13:58 +02004642cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004643 return ret;
4644}
4645
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004646/**
4647 * @brief Skip any redundant characters, namely whitespaces and comments.
4648 *
4649 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004650 * @return LY_SUCCESS on success.
4651 * @return LY_EVALID on invalid comment.
4652 */
4653static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004654skip_redundant_chars(struct lysp_yang_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004655{
4656 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004657 while (ctx->in->current[0]) {
4658 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004659 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004660 ly_in_skip(ctx->in, 2);
4661 LY_CHECK_RET(skip_comment(ctx, 1));
4662 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004663 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004664 ly_in_skip(ctx->in, 2);
4665 LY_CHECK_RET(skip_comment(ctx, 2));
4666 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004667 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004668 if (ctx->in->current[0] == '\n') {
4669 LY_IN_NEW_LINE(ctx->in);
4670 }
Radek Krejci33090f92020-12-17 20:12:46 +01004671 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004672 } else {
4673 break;
4674 }
4675 }
4676
4677 return LY_SUCCESS;
4678}
4679
Radek Krejcid4557c62018-09-17 11:42:09 +02004680LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004681yang_parse_submodule(struct lysp_yang_ctx **context, struct ly_ctx *ly_ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004682 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004683{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004684 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004685 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004686 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004687 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004688 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004689 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004690
aPiecek56be92a2021-07-01 07:18:10 +02004691 assert(context && ly_ctx && main_ctx && in && submod);
4692
David Sedlák1b623122019-08-05 15:27:49 +02004693 /* create context */
4694 *context = calloc(1, sizeof **context);
4695 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004696 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004697 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004698 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004699
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004700 mod_p = calloc(1, sizeof *mod_p);
4701 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004702 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004703 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004704
4705 /* use main context parsed mods adding the current one */
4706 (*context)->parsed_mods = main_ctx->parsed_mods;
4707 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004708
Michal Vaskof8ebf132022-11-21 14:06:48 +01004709 LOG_LOCSET(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004710
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004711 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004712 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004713 LY_CHECK_GOTO(ret, cleanup);
4714
Michal Vasko7fbc8162018-09-17 10:35:16 +02004715 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004716 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004717 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004718
Radek Krejcid6b76452019-09-03 17:03:03 +02004719 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004720 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004721 ret = LY_EINVAL;
4722 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004723 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004724 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004725 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004726 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004727 }
4728
Michal Vasko7fbc8162018-09-17 10:35:16 +02004729 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004730 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004731 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004732
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004733 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004734 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004735 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004736 if (in->current[0]) {
4737 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004738 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004739 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004740 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004741
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004742 mod_p->parsing = 0;
4743 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004744
Radek Krejcibbe09a92018-11-08 09:36:54 +01004745cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004746 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004747 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004748 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004749 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004750 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004751 }
4752
4753 return ret;
4754}
4755
4756LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004757yang_parse_module(struct lysp_yang_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004758{
4759 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004760 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004761 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004762 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004763 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004764 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004765
David Sedlák1b623122019-08-05 15:27:49 +02004766 /* create context */
4767 *context = calloc(1, sizeof **context);
4768 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004769 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004770 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004771 (*context)->in = in;
Michal Vaskod0625d72022-10-06 15:02:50 +02004772 (*context)->main_ctx = (struct lysp_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004773
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004774 mod_p = calloc(1, sizeof *mod_p);
4775 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4776 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004777 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004778
Michal Vaskof8ebf132022-11-21 14:06:48 +01004779 LOG_LOCSET(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004780
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004781 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004782 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004783 LY_CHECK_GOTO(ret, cleanup);
4784
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004785 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004786 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004787 LY_CHECK_GOTO(ret, cleanup);
4788
Radek Krejcid6b76452019-09-03 17:03:03 +02004789 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004790 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 +01004791 ret = LY_EINVAL;
4792 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004793 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004794 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004795 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004796 goto cleanup;
4797 }
4798
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004799 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004800 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004801 LY_CHECK_GOTO(ret, cleanup);
4802
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004803 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004804 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004805 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004806 if (in->current[0]) {
4807 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004808 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004809 goto cleanup;
4810 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004811
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004812 mod->parsed = mod_p;
4813
4814cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004815 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004816 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004817 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004818 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004819 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004820 }
4821
Michal Vasko7fbc8162018-09-17 10:35:16 +02004822 return ret;
4823}