blob: 28bd57bb202f009d3a0e85da36faa65357ff8381 [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
Michal Vaskoc636ea42022-09-16 10:20:31 +02006 * Copyright (c) 2018 - 2022 CESNET, z.s.p.o.
Michal Vasko7fbc8162018-09-17 10:35:16 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko63f3d842020-07-08 10:10:14 +020014#include "parser_internal.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
17#include <ctype.h>
18#include <errno.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "log.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020030#include "path.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "set.h"
32#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010033#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "tree_schema.h"
Michal Vaskoc636ea42022-09-16 10:20:31 +020035#include "tree_schema_free.h"
Radek Krejci70853c52018-10-15 14:46:16 +020036#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020037
Radek Krejci77114102021-03-10 15:21:57 +010038struct lys_glob_unres;
39
Radek Krejciceaf2122019-01-02 15:03:26 +010040/**
41 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020042 *
Radek Krejciceaf2122019-01-02 15:03:26 +010043 * @param[in] CTX yang parser context to access libyang context.
44 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
45 * @param[out] TARGET variable where to store the pointer to the inserted value.
46 * @param[in] WORD string to store.
47 * @param[in] LEN length of the string in WORD to store.
48 */
Michal Vasko12ef5362022-09-16 15:13:58 +020049#define INSERT_WORD_GOTO(CTX, BUF, TARGET, WORD, LEN, RET, LABEL) \
50 if (BUF) {LY_CHECK_GOTO(RET = lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)), LABEL);}\
51 else {LY_CHECK_GOTO(RET = lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)), LABEL);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020052
Radek Krejciceaf2122019-01-02 15:03:26 +010053/**
Michal Vasko63f3d842020-07-08 10:10:14 +020054 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020055 *
Radek Krejciceaf2122019-01-02 15:03:26 +010056 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010057 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
58 */
Radek Krejcid54412f2020-12-17 20:25:35 +010059#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020060
Michal Vaskoea5abea2018-09-18 13:10:54 +020061/**
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020062 * @brief Loop through all substatements. Starts a for loop and ::YANG_READ_SUBSTMT_NEXT_ITER must be used at its end.
Michal Vaskoea5abea2018-09-18 13:10:54 +020063 *
Michal Vaskoc8806c82022-12-06 10:31:24 +010064 * @param[in] CTX yang parser context.
Michal Vaskoea5abea2018-09-18 13:10:54 +020065 * @param[out] KW YANG keyword read.
66 * @param[out] WORD Pointer to the keyword itself.
67 * @param[out] WORD_LEN Length of the keyword.
Michal Vasko12ef5362022-09-16 15:13:58 +020068 * @param[out] RET Variable for error storing.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020069 * @param[in] ERR_LABEL Label to go to on error.
Michal Vaskoea5abea2018-09-18 13:10:54 +020070 */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020071#define YANG_READ_SUBSTMT_FOR_GOTO(CTX, KW, WORD, WORD_LEN, RET, ERR_LABEL) \
72 ly_bool __loop_end = 0; \
Michal Vasko12ef5362022-09-16 15:13:58 +020073 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020074 goto ERR_LABEL; \
aPieceka24a2252021-05-07 10:52:31 +020075 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020076 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020077 __loop_end = 1; \
78 } else if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
Michal Vasko193dacd2022-10-13 08:43:05 +020079 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", lyplg_ext_stmt2str(KW)); \
Michal Vasko12ef5362022-09-16 15:13:58 +020080 RET = LY_EVALID; \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020081 goto ERR_LABEL; \
82 } else { \
83 YANG_READ_SUBSTMT_NEXT_ITER(CTX, KW, WORD, WORD_LEN, NULL, RET, ERR_LABEL); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020084 } \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020085 while (!__loop_end)
Michal Vasko7fbc8162018-09-17 10:35:16 +020086
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020087/**
88 * @brief Next iteration of ::YANG_READ_SUBSTMT_FOR_GOTO loop.
89 *
Michal Vaskoc8806c82022-12-06 10:31:24 +010090 * @param[in] CTX yang parser context.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020091 * @param[out] KW YANG keyword read.
92 * @param[out] WORD Pointer to the keyword itself.
93 * @param[out] WORD_LEN Length of the keyword.
94 * @param[in] EXTS Final extension instance array to store.
95 * @param[out] RET Variable for error storing.
96 * @param[in] ERR_LABEL Label to go to on error.
97 */
98#define YANG_READ_SUBSTMT_NEXT_ITER(CTX, KW, WORD, WORD_LEN, EXTS, RET, ERR_LABEL) \
99 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
100 goto ERR_LABEL; \
101 } \
102 if (KW == LY_STMT_SYNTAX_RIGHT_BRACE) { \
Michal Vaskoc8806c82022-12-06 10:31:24 +0100103 if (EXTS && (RET = ly_set_add(&(CTX)->main_ctx->ext_inst, (EXTS), 1, NULL))) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200104 goto ERR_LABEL; \
105 } \
106 __loop_end = 1; \
107 }
108
Michal Vaskod0625d72022-10-06 15:02:50 +0200109LY_ERR parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
110LY_ERR parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
111LY_ERR parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
112LY_ERR parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
113LY_ERR parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
114LY_ERR parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200115
Michal Vaskoea5abea2018-09-18 13:10:54 +0200116/**
117 * @brief Add another character to dynamic buffer, a low-level function.
118 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200119 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200120 *
Radek Krejci404251e2018-10-09 12:06:44 +0200121 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200122 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200123 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200124 * @param[in,out] buf Buffer to use, can be moved by realloc().
125 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200127 * @return LY_ERR values.
128 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200129LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200130buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200131{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100132#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100134 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200135 *buf = ly_realloc(*buf, *buf_len);
136 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
137 }
Radek Krejcic0917392019-04-10 13:04:04 +0200138 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200139 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200140 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200141 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200142 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143
Radek Krejci44ceedc2018-10-02 15:54:31 +0200144 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200145 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100146#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200147}
148
Michal Vaskoea5abea2018-09-18 13:10:54 +0200149/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200150 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
151 *
152 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200153 * @param[in] arg Type of the input string to select method of checking character validity.
154 * @param[in,out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first
Michal Vaskoea5abea2018-09-18 13:10:54 +0200155 * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200156 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
157 * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not requested (word is a substring of the data).
Michal Vaskoea5abea2018-09-18 13:10:54 +0200158 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200159 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200160 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
161 * 0 - colon not yet found (no prefix)
162 * 1 - \p c is the colon character
163 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200164 * @return LY_ERR values.
165 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200166LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200167buf_store_char(struct lysp_yang_ctx *ctx, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200168 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200169{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200170 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 size_t len;
172
Radek Krejcif29b7c32019-04-09 16:17:49 +0200173 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
174 assert(!need_buf || (need_buf && word_b));
175
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100177 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
178 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
179 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200180 if (c == '\n') {
181 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100182 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200183 } else {
184 /* note - even the multibyte character is count as 1 */
185 ++ctx->indent;
186 }
187
Radek Krejci44ceedc2018-10-02 15:54:31 +0200188 /* check character validity */
189 switch (arg) {
190 case Y_IDENTIF_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200191 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200192 break;
193 case Y_PREF_IDENTIF_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200194 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200195 break;
196 case Y_STR_ARG:
197 case Y_MAYBE_STR_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200198 LY_CHECK_RET(lysp_check_stringchar((struct lysp_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200199 break;
200 }
201
Michal Vasko7fbc8162018-09-17 10:35:16 +0200202 if (word_b && *word_b) {
203 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100204 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200205 return LY_EMEM;
206 }
207
208 /* in case of realloc */
209 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200210 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200211 /* first time we need a buffer, copy everything read up to now */
212 if (*word_len) {
213 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200214 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200215 *buf_len = *word_len;
216 memcpy(*word_b, *word_p, *word_len);
217 }
218
219 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100220 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200221 return LY_EMEM;
222 }
223
224 /* in case of realloc */
225 *word_p = *word_b;
226 } else {
227 /* just remember the first character pointer */
228 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100229 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200230 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200231 /* ... and update the word's length */
232 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100233 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200234 }
235
236 return LY_SUCCESS;
237}
238
Michal Vaskoea5abea2018-09-18 13:10:54 +0200239/**
240 * @brief Skip YANG comment in data.
241 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200242 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200243 * @param[in] comment Type of the comment to process:
244 * 1 for a one-line comment,
245 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200246 * @return LY_ERR values.
247 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200248LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200249skip_comment(struct lysp_yang_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200250{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100251 /* internal statuses: */
252#define COMMENT_NO 0 /* comment ended */
253#define COMMENT_LINE 1 /* in line comment */
254#define COMMENT_BLOCK 2 /* in block comment */
255#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
256
Radek Krejci33090f92020-12-17 20:12:46 +0100257 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200258 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100259 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100260 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100261 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100262 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200263 }
264 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100265 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100266 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100267 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100268 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100269 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200270 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200271 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100272 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100273 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100274 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100275 } else if (ctx->in->current[0] != '*') {
276 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100277 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200278 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100279 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200280 }
281 break;
282 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200283 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200284 }
285
Radek Krejci33090f92020-12-17 20:12:46 +0100286 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200287 ctx->indent = 0;
288 } else {
289 ++ctx->indent;
290 }
Radek Krejci33090f92020-12-17 20:12:46 +0100291 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200292 }
293
Radek Krejci33090f92020-12-17 20:12:46 +0100294 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200295 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200296 return LY_EVALID;
297 }
298
299 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100300
301#undef COMMENT_NO
302#undef COMMENT_LINE
303#undef COMMENT_BLOCK
304#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200305}
306
Michal Vaskoea5abea2018-09-18 13:10:54 +0200307/**
308 * @brief Read a quoted string from data.
309 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200310 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200311 * @param[in] arg Type of YANG keyword argument expected.
312 * @param[out] word_p Pointer to the read quoted string.
313 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
314 * set to NULL. Otherwise equal to \p word_p.
315 * @param[out] word_len Length of the read quoted string.
316 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
317 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
318 * indenation in the final quoted string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200319 * @return LY_ERR values.
320 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200322read_qstring(struct lysp_yang_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200323 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200324{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100325 /* string parsing status: */
326#define STRING_ENDED 0 /* string ended */
327#define STRING_SINGLE_QUOTED 1 /* string with ' */
328#define STRING_DOUBLE_QUOTED 2 /* string with " */
329#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
330#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
331#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
332
Radek Krejci1deb5be2020-08-26 16:43:36 +0200333 uint8_t string;
334 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200335 ly_bool need_buf = 0;
336 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200337 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200338 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200339
Radek Krejci33090f92020-12-17 20:12:46 +0100340 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100341 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200342 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200343 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100344 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100345 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200346 }
Radek Krejci33090f92020-12-17 20:12:46 +0100347 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200348
Radek Krejci33090f92020-12-17 20:12:46 +0100349 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200350 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100351 case STRING_SINGLE_QUOTED:
Michal Vasko4e55f5a2022-12-14 12:15:00 +0100352 if (ctx->in->current[0] == '\'') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200353 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100354 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100355 MOVE_INPUT(ctx, 1);
Michal Vasko4e55f5a2022-12-14 12:15:00 +0100356 } else {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200357 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100358 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200359 }
360 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100361 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100362 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200363 case '\"':
364 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100365 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100366 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200367 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200368 break;
369 case '\\':
370 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100371 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200372
373 /* the backslash sequence is substituted, so we will need a buffer to store the result */
374 need_buf = 1;
375
376 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100377 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200378
379 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
380 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
381 trailing_ws = 0;
382
383 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
384 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200385 break;
386 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200387 if (current_indent < block_indent) {
388 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100389 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200390 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100391 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100392 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100393 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200394 }
395 break;
396 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200397 if (current_indent < block_indent) {
398 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100399 current_indent += Y_TAB_SPACES;
400 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200401 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200402 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100403 c = ctx->in->current;
404 ctx->in->current = " ";
405 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
406 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100407 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200408 }
Radek Krejci33090f92020-12-17 20:12:46 +0100409 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200410 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100411 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100412 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100413 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200414 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100415 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200416 }
417 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200418 case '\r':
Michal Vasko438f3782023-08-09 10:01:15 +0200419 /* newline may be escaped */
420 if ((ctx->in->current[1] != '\n') && strncmp(&ctx->in->current[1], "\\n", 2)) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200421 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
422 return LY_EVALID;
423 }
Michal Vasko38266a42023-08-11 11:28:56 +0200424
425 /* skip this character, do not store it */
426 ++ctx->in->current;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200427 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200428 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200429 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200430 /* we will be removing the indents so we need our own buffer */
431 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200432
433 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200434 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200435
Radek Krejciff13cd12019-10-25 15:34:24 +0200436 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200437 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438 }
439
440 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100441 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200442
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200443 /* reset context indentation counter for possible string after this one */
444 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200445 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200446 break;
447 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200448 /* first non-whitespace character, stop eating indentation */
449 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200450
451 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100452 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200453 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 break;
455 }
456 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100457 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200458 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100459 c = ctx->in->current;
460 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200461 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100462 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100463 /* fix false newline count in buf_store_char() */
464 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200465 break;
466 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100467 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200468 break;
469 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200470 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200471 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200472 break;
473 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200474 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100475 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200476 return LY_EVALID;
477 }
478
479 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100480 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200481
Radek Krejcif13b87b2020-12-01 22:02:17 +0100482 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100483 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200484 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100485 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100486 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 case '+':
488 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100489 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200490 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200491 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200492 case '\r':
493 if (ctx->in->current[1] != '\n') {
494 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
495 return LY_EVALID;
496 }
497 MOVE_INPUT(ctx, 1);
498 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200499 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100500 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100501 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200502 case ' ':
503 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200504 /* just skip */
505 break;
506 default:
507 /* string is finished */
508 goto string_end;
509 }
Radek Krejci33090f92020-12-17 20:12:46 +0100510 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200511 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100512 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100513 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200514 case '\r':
515 if (ctx->in->current[1] != '\n') {
516 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
517 return LY_EVALID;
518 }
519 MOVE_INPUT(ctx, 1);
520 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200521 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100522 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100523 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200524 case ' ':
525 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200526 /* skip */
527 break;
528 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100529 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200530 break;
531 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100532 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200533 break;
534 default:
535 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200536 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537 return LY_EVALID;
538 }
Radek Krejci33090f92020-12-17 20:12:46 +0100539 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200540 break;
541 default:
542 return LY_EINT;
543 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200544 }
545
546string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200547 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200548 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200549 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200550 return LY_EVALID;
551 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200552 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100553
554#undef STRING_ENDED
555#undef STRING_SINGLE_QUOTED
556#undef STRING_DOUBLE_QUOTED
557#undef STRING_DOUBLE_QUOTED_ESCAPED
558#undef STRING_PAUSED_NEXTSTRING
559#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560}
561
Michal Vaskoea5abea2018-09-18 13:10:54 +0200562/**
563 * @brief Get another YANG string from the raw data.
564 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200565 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200566 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200567 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
568 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200569 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200570 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
571 * set to NULL. Otherwise equal to \p word_p.
572 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200573 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200574 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200575LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200576get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200577 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200578{
Michal Vaskob68ea142021-04-26 13:46:00 +0200579 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200580 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200581 uint8_t prefix = 0;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100582 ly_bool str_end = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200583
Michal Vasko7fbc8162018-09-17 10:35:16 +0200584 /* word buffer - dynamically allocated */
585 *word_b = NULL;
586
587 /* word pointer - just a pointer to data */
588 *word_p = NULL;
589
590 *word_len = 0;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100591 while (!str_end) {
Radek Krejci33090f92020-12-17 20:12:46 +0100592 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200593 case '\'':
594 case '\"':
595 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200596 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100597 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200598 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200599 ret = LY_EVALID;
600 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200601 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200602 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100603 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200604 }
Michal Vasko1e4b1162024-01-19 09:16:49 +0100605
Michal Vaskob68ea142021-04-26 13:46:00 +0200606 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200607 if (!*word_p) {
608 /* do not return NULL word */
609 *word_p = "";
610 }
Michal Vasko1e4b1162024-01-19 09:16:49 +0100611 str_end = 1;
612 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200613 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100614 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200615 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100616 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200617 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100618 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200619 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100620 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200621 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200622 } else {
623 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200624 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 +0200625 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200626 break;
627 case ' ':
628 if (*word_len) {
629 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100630 str_end = 1;
631 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200632 }
Radek Krejci33090f92020-12-17 20:12:46 +0100633 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200634 break;
635 case '\t':
636 if (*word_len) {
637 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100638 str_end = 1;
639 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200640 }
641 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100642 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200643
Radek Krejci33090f92020-12-17 20:12:46 +0100644 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200645 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200646 case '\r':
647 if (ctx->in->current[1] != '\n') {
648 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200649 ret = LY_EVALID;
650 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200651 }
652 MOVE_INPUT(ctx, 1);
653 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200654 case '\n':
655 if (*word_len) {
656 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100657 str_end = 1;
658 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100660 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100661 MOVE_INPUT(ctx, 1);
662
Michal Vasko7fbc8162018-09-17 10:35:16 +0200663 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200664 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200665 break;
666 case ';':
667 case '{':
668 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
669 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100670 str_end = 1;
671 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 }
673
Radek Krejci33090f92020-12-17 20:12:46 +0100674 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200675 ret = LY_EVALID;
676 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200677 case '}':
678 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100679 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200680 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200681 ret = LY_EVALID;
682 goto error;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100683 case '\0':
684 /* unexpected EOF */
685 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
686 ret = LY_EVALID;
687 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200689 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 +0200690 break;
691 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200692 }
693
Radek Krejci44ceedc2018-10-02 15:54:31 +0200694 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200696 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200697 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200698 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699 *word_p = *word_b;
700 }
701
702 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200703
704error:
705 free(*word_b);
706 *word_b = NULL;
707 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200708}
709
Michal Vaskoea5abea2018-09-18 13:10:54 +0200710/**
711 * @brief Get another YANG keyword from the raw data.
712 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200713 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200714 * @param[out] kw YANG keyword read.
715 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
716 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200717 * @return LY_ERR values.
718 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200719LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200720get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200722 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200723 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200724 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200725
726 if (word_p) {
727 *word_p = NULL;
728 *word_len = 0;
729 }
730
731 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100732 while (ctx->in->current[0]) {
733 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200734 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100735 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100737 MOVE_INPUT(ctx, 2);
738 LY_CHECK_RET(skip_comment(ctx, 1));
739 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200740 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100741 MOVE_INPUT(ctx, 2);
742 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200743 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200744 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200745 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200746 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200747 }
Radek Krejci13028282019-06-11 14:56:48 +0200748 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200749 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200750 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100751 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200752 ctx->indent = 0;
753 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200754 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200755 /* skip whitespaces (optsep) */
756 ++ctx->indent;
757 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200758 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100760 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200761 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100762 case '\r':
763 /* possible CRLF endline */
764 if (ctx->in->current[1] == '\n') {
765 break;
766 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100767 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200768 default:
769 /* either a keyword start or an invalid character */
770 goto keyword_start;
771 }
772
Radek Krejci33090f92020-12-17 20:12:46 +0100773 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200774 }
775
776keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100777 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100778 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200779
aPiecek93582ed2021-05-25 14:49:06 +0200780 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
781 goto success;
782 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
783 ctx->depth++;
784 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100785 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200786 return LY_EINVAL;
787 }
788 goto success;
789 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
790 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200791 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200792 }
793
Radek Krejcid6b76452019-09-03 17:03:03 +0200794 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200795 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100796 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200797 case '\r':
798 if (ctx->in->current[1] != '\n') {
799 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
800 return LY_EVALID;
801 }
802 MOVE_INPUT(ctx, 1);
803 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200804 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200805 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200806 case ' ':
807 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200808 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200809 case ':':
810 /* keyword is not actually a keyword, but prefix of an extension.
811 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
812 * and we will be checking the keyword (extension instance) itself */
813 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100814 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200815 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200816 case '{':
Michal Vasko946b70c2023-07-19 08:57:31 +0200817 case ';':
818 /* allowed only for input and output statements which are without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200819 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200820 break;
821 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100822 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200823 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100824 MOVE_INPUT(ctx, 1);
825 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200826 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200827 return LY_EVALID;
828 }
829 } else {
830 /* still can be an extension */
831 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200832
Radek Krejci156ccaf2018-10-15 15:49:17 +0200833extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100834 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200835 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
836 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200837 uint32_t c = 0;
838
Radek Krejci33090f92020-12-17 20:12:46 +0100839 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
840 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200841 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200842 /* check character validity */
Michal Vaskod0625d72022-10-06 15:02:50 +0200843 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100844 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845 }
Radek Krejci33090f92020-12-17 20:12:46 +0100846 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200847 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200848 return LY_EVALID;
849 }
850
851 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200852 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100853 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200854 return LY_EVALID;
855 }
856
Radek Krejcid6b76452019-09-03 17:03:03 +0200857 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200858 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200859
Radek Krejci626df482018-10-11 15:06:31 +0200860success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200861 if (word_p) {
862 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100863 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200864 }
865
866 return LY_SUCCESS;
867}
868
Michal Vaskoea5abea2018-09-18 13:10:54 +0200869/**
870 * @brief Parse extension instance substatements.
871 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200872 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200873 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200874 * @param[in] word Extension instance substatement name (keyword).
875 * @param[in] word_len Extension instance substatement name length.
876 * @param[in,out] child Children of this extension instance to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200877 * @return LY_ERR values.
878 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200879static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200880parse_ext_substmt(struct lysp_yang_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200881 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200882{
883 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100884 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200885 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 struct lysp_stmt *stmt, *par_child;
887
888 stmt = calloc(1, sizeof *stmt);
889 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
890
Radek Krejcibb9b1982019-04-08 14:24:59 +0200891 /* insert into parent statements */
892 if (!*child) {
893 *child = stmt;
894 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200895 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200896 par_child->next = stmt;
897 }
898
Michal Vaskofc2cd072021-02-24 13:17:17 +0100899 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200900 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200901
902 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100903 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200904 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200905 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200906 }
907
Radek Krejci8df109d2021-04-23 12:19:08 +0200908 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100909 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100910 stmt->kw = kw;
911
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200912 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
913 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
914 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200915 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200916
917cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918 return ret;
919}
920
Michal Vaskoea5abea2018-09-18 13:10:54 +0200921/**
922 * @brief Parse extension instance.
923 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200924 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200925 * @param[in] ext_name Extension instance substatement name (keyword).
926 * @param[in] ext_name_len Extension instance substatement name length.
Michal Vasko193dacd2022-10-13 08:43:05 +0200927 * @param[in] parent Current statement parent.
928 * @param[in] parent_stmt Type of @p parent statement.
929 * @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 +0200930 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200931 * @return LY_ERR values.
932 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200934parse_ext(struct lysp_yang_ctx *ctx, const char *ext_name, size_t ext_name_len, const void *parent,
935 enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100937 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200938 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200939 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200940 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200941 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200942
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200943 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200944
Michal Vaskofc2cd072021-02-24 13:17:17 +0100945 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200946 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%.*s\" without the mandatory prefix.",
947 (int)ext_name_len, ext_name);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100948 return LY_EVALID;
949 }
950
951 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200952 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200953
954 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100955 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200956 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200957 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200958 }
959
Michal Vaskofc2cd072021-02-24 13:17:17 +0100960 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200961 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200962 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100963 e->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200964 e->parent = (void *)parent;
965 e->parent_stmt = parent_stmt;
966 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100967
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200968 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
969 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
970 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200971 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200972
973cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200974 return ret;
975}
976
Michal Vaskoea5abea2018-09-18 13:10:54 +0200977/**
978 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
979 * description, etc...
980 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200981 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +0200982 * @param[in] parent Current statement parent.
983 * @param[in] parent_stmt Type of statement in @p value.
984 * @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 +0200985 * @param[in,out] value Place to store the parsed value.
986 * @param[in] arg Type of the YANG keyword argument (of the value).
987 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200988 * @return LY_ERR values.
989 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200991parse_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 +0200992 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200993{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100994 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200996 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200997 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200998
999 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001000 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001 return LY_EVALID;
1002 }
1003
1004 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001005 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006
1007 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +02001008 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001010 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001012 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001013 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent, parent_stmt, parent_stmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014 break;
1015 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001016 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001017 return LY_EVALID;
1018 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001019 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001020 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001021
1022cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023 return ret;
1024}
1025
Michal Vaskoea5abea2018-09-18 13:10:54 +02001026/**
1027 * @brief Parse the yang-version statement.
1028 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001029 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001030 * @param[in,out] mod Module to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001031 * @return LY_ERR values.
1032 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001033static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001034parse_yangversion(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001035{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001036 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001038 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001039 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001040
Michal Vasko193dacd2022-10-13 08:43:05 +02001041 if (mod->version) {
David Sedlákb3077192019-06-19 10:55:37 +02001042 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001043 return LY_EVALID;
1044 }
1045
1046 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001047 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001048
Radek Krejci96e48da2020-09-04 13:18:06 +02001049 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001050 mod->version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001051 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001052 mod->version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001054 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055 free(buf);
1056 return LY_EVALID;
1057 }
1058 free(buf);
1059
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001060 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001062 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001063 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_YANG_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064 break;
1065 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001066 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067 return LY_EVALID;
1068 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001069 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001070 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001071
1072cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073 return ret;
1074}
1075
Michal Vaskoea5abea2018-09-18 13:10:54 +02001076/**
1077 * @brief Parse the belongs-to statement.
1078 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001079 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001080 * @param[in,out] submod Submodule to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001081 * @return LY_ERR values.
1082 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001084parse_belongsto(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001085{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001086 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001087 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001088 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001089 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001090
Michal Vasko193dacd2022-10-13 08:43:05 +02001091 if (submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001092 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001093 return LY_EVALID;
1094 }
1095
Michal Vaskoc3781c32020-10-06 14:04:08 +02001096 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001097 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001098 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001099 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 +01001100 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001101 free(buf);
1102 return LY_EVALID;
1103 }
1104 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001105
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001106 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001108 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001109 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 +02001110 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001111 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001112 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_BELONGS_TO, 0, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113 break;
1114 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001115 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116 return LY_EVALID;
1117 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001118 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001119 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001120
Michal Vasko7fbc8162018-09-17 10:35:16 +02001121 /* mandatory substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +02001122 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001123 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124 return LY_EVALID;
1125 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001126
1127cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001128 return ret;
1129}
1130
Michal Vaskoea5abea2018-09-18 13:10:54 +02001131/**
1132 * @brief Parse the revision-date statement.
1133 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001134 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001135 * @param[in,out] rev Buffer to store the parsed value in.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001136 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001137 * @return LY_ERR values.
1138 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001140parse_revisiondate(struct lysp_yang_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001142 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001144 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001145 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001146
1147 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001148 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149 return LY_EVALID;
1150 }
1151
1152 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001153 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001154
1155 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001156 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157 free(buf);
1158 return LY_EVALID;
1159 }
1160
1161 /* store value and spend buf if allocated */
1162 strncpy(rev, word, word_len);
1163 free(buf);
1164
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001165 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001166 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001167 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001168 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169 break;
1170 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001171 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172 return LY_EVALID;
1173 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001174 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001176
1177cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178 return ret;
1179}
1180
Michal Vaskoea5abea2018-09-18 13:10:54 +02001181/**
1182 * @brief Parse the include statement.
1183 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001184 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001185 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001186 * @param[in,out] includes Parsed includes to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001187 * @return LY_ERR values.
1188 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001190parse_include(struct lysp_yang_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191{
Radek Krejcid33273d2018-10-25 14:55:52 +02001192 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001193 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001194 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001195 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196 struct lysp_include *inc;
1197
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001198 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001199
1200 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001201 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001202
Michal Vasko12ef5362022-09-16 15:13:58 +02001203 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001204
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001205 /* submodules share the namespace with the module names, so there must not be
1206 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001207 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001208 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001209 return LY_EVALID;
1210 }
1211
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001212 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001213 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001214 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001215 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001216 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 +02001217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001218 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001219 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko193dacd2022-10-13 08:43:05 +02001220 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 +02001221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001222 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001223 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001225 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001226 LY_CHECK_RET(parse_ext(ctx, word, word_len, inc, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227 break;
1228 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001229 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230 return LY_EVALID;
1231 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001232 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001234
1235cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236 return ret;
1237}
1238
Michal Vaskoea5abea2018-09-18 13:10:54 +02001239/**
1240 * @brief Parse the import statement.
1241 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001242 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001243 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001244 * @param[in,out] imports Parsed imports to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001245 * @return LY_ERR values.
1246 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001247static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001248parse_import(struct lysp_yang_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001250 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001252 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001253 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254 struct lysp_import *imp;
1255
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001256 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257
1258 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001259 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001260 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001262 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001264 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02001265 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 +02001266 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001267 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001268 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001269 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001270 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 +02001271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001272 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001273 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko193dacd2022-10-13 08:43:05 +02001274 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 +02001275 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001276 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001277 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001278 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001279 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001280 LY_CHECK_RET(parse_ext(ctx, word, word_len, imp, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001281 break;
1282 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001283 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001284 return LY_EVALID;
1285 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001286 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001287 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001288
Michal Vasko7fbc8162018-09-17 10:35:16 +02001289 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001290 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001291
Michal Vasko12ef5362022-09-16 15:13:58 +02001292cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001293 return ret;
1294}
1295
Michal Vaskoea5abea2018-09-18 13:10:54 +02001296/**
1297 * @brief Parse the revision statement.
1298 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001299 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001300 * @param[in,out] revs Parsed revisions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001301 * @return LY_ERR values.
1302 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001304parse_revision(struct lysp_yang_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001305{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001306 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001307 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001308 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001309 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310 struct lysp_revision *rev;
1311
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001312 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001313
1314 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001315 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316
1317 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001318 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001319 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001320 return LY_EVALID;
1321 }
1322
Radek Krejcib7db73a2018-10-24 14:18:40 +02001323 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324 free(buf);
1325
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001326 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001328 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001329 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 +02001330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001331 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001332 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 +02001333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001334 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001335 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336 break;
1337 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001338 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 return LY_EVALID;
1340 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001341 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001342 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001343
1344cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345 return ret;
1346}
1347
Michal Vaskoea5abea2018-09-18 13:10:54 +02001348/**
1349 * @brief Parse a generic text field that can have more instances such as base.
1350 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001351 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001352 * @param[in] parent Current statement parent.
1353 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001354 * @param[in,out] texts Parsed values to add to.
1355 * @param[in] arg Type of the expected argument.
1356 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001357 * @return LY_ERR values.
1358 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001359static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001360parse_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 +02001361 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001363 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001365 const char **item;
1366 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001367 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368
1369 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001370 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371
1372 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001373 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374
Michal Vasko12ef5362022-09-16 15:13:58 +02001375 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001376 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001377 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001378 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001379 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 +02001380 break;
1381 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001382 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383 return LY_EVALID;
1384 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001385 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001387
1388cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001389 return ret;
1390}
1391
Michal Vaskoea5abea2018-09-18 13:10:54 +02001392/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001393 * @brief Parse a generic text field that can have more instances such as base.
1394 *
1395 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001396 * @param[in] parent_stmt Type of statement stored in @p qnames.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001397 * @param[in,out] qnames Parsed qnames to add to.
1398 * @param[in] arg Type of the expected argument.
1399 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001400 * @return LY_ERR values.
1401 */
1402static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001403parse_qnames(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_qname **qnames, enum yang_arg arg,
1404 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +02001405{
1406 LY_ERR ret = LY_SUCCESS;
1407 char *buf, *word;
1408 struct lysp_qname *item;
1409 size_t word_len;
1410 enum ly_stmt kw;
1411
1412 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001413 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001414
1415 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001416 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001417
Michal Vasko12ef5362022-09-16 15:13:58 +02001418 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001419 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001420 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001421 switch (kw) {
1422 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001423 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 +02001424 break;
1425 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001426 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001427 return LY_EVALID;
1428 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001429 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001430 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001431
1432cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001433 return ret;
1434}
1435
1436/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001437 * @brief Parse the config statement.
1438 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001439 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001440 * @param[in,out] flags Flags to add to.
1441 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001442 * @return LY_ERR values.
1443 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001444static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001445parse_config(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001446{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001447 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001449 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001450 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001451
1452 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001453 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001454 return LY_EVALID;
1455 }
1456
1457 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001458 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001459
Radek Krejcif13b87b2020-12-01 22:02:17 +01001460 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001462 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463 *flags |= LYS_CONFIG_R;
1464 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001465 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466 free(buf);
1467 return LY_EVALID;
1468 }
1469 free(buf);
1470
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001471 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001473 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001474 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475 break;
1476 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001477 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478 return LY_EVALID;
1479 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001480 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001482
1483cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484 return ret;
1485}
1486
Michal Vaskoea5abea2018-09-18 13:10:54 +02001487/**
1488 * @brief Parse the mandatory statement.
1489 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001490 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001491 * @param[in,out] flags Flags to add to.
1492 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001493 * @return LY_ERR values.
1494 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001495static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001496parse_mandatory(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001497{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001498 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001499 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001500 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001501 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001502
1503 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001504 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505 return LY_EVALID;
1506 }
1507
1508 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001509 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510
Radek Krejcif13b87b2020-12-01 22:02:17 +01001511 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001512 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001513 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 *flags |= LYS_MAND_FALSE;
1515 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001516 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 free(buf);
1518 return LY_EVALID;
1519 }
1520 free(buf);
1521
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001522 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001523 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001524 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001525 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526 break;
1527 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001528 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001529 return LY_EVALID;
1530 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001531 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001532 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001533
1534cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535 return ret;
1536}
1537
Michal Vaskoea5abea2018-09-18 13:10:54 +02001538/**
1539 * @brief Parse a restriction such as range or length.
1540 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001541 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001542 * @param[in] restr_kw Type of this particular restriction.
1543 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001544 * @return LY_ERR values.
1545 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001547parse_restr(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001548{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001549 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001551 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001552 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553
1554 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001555 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556
Michal Vasko193dacd2022-10-13 08:43:05 +02001557 CHECK_NONEMPTY(ctx, word_len, lyplg_ext_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001558 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001559 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001560 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001561 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001562 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001563 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 +02001564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001565 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001566 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 +02001567 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001568 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02001569 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG,
1570 &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001572 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001573 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 +02001574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001575 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001576 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 break;
1578 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001579 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 return LY_EVALID;
1581 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001582 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001583 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001584
1585cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 return ret;
1587}
1588
Michal Vaskoea5abea2018-09-18 13:10:54 +02001589/**
1590 * @brief Parse a restriction that can have more instances such as must.
1591 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001592 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001593 * @param[in] restr_kw Type of this particular restriction.
1594 * @param[in,out] restrs Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001595 * @return LY_ERR values.
1596 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001597static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001598parse_restrs(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001599{
1600 struct lysp_restr *restr;
1601
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001602 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001603 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604}
1605
Michal Vaskoea5abea2018-09-18 13:10:54 +02001606/**
1607 * @brief Parse the status statement.
1608 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001609 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001610 * @param[in,out] flags Flags to add to.
1611 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001612 * @return LY_ERR values.
1613 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001614static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001615parse_status(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001616{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001617 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001618 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001619 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001620 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621
1622 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001623 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 return LY_EVALID;
1625 }
1626
1627 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001628 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001629
Radek Krejcif13b87b2020-12-01 22:02:17 +01001630 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001632 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001634 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001635 *flags |= LYS_STATUS_OBSLT;
1636 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001637 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 free(buf);
1639 return LY_EVALID;
1640 }
1641 free(buf);
1642
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001643 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001645 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001646 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647 break;
1648 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001649 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001650 return LY_EVALID;
1651 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001652 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001654
1655cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001656 return ret;
1657}
1658
Michal Vaskoea5abea2018-09-18 13:10:54 +02001659/**
1660 * @brief Parse the when statement.
1661 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001662 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001663 * @param[in,out] when_p When pointer to parse to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001664 * @return LY_ERR values.
1665 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001666LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001667parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001668{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001669 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001670 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001671 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001672 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001674 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001675
1676 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001677 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001678 return LY_EVALID;
1679 }
1680
1681 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001682 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001683
1684 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001685 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001686 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001687 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001688
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001689 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001691 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001692 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->dsc, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts),
1693 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001695 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001696 LY_CHECK_GOTO(ret = parse_text_field(ctx, when->ref, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts),
1697 cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001698 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001699 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001700 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 +02001701 break;
1702 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001703 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001704 ret = LY_EVALID;
1705 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001706 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001707 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001708 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001709
1710cleanup:
1711 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001712 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001713 free(when);
1714 } else {
1715 *when_p = when;
1716 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001717 return ret;
1718}
1719
Michal Vaskoea5abea2018-09-18 13:10:54 +02001720/**
1721 * @brief Parse the anydata or anyxml statement.
1722 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001723 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001724 * @param[in] any_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001725 * @param[in] parent Node parent.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001726 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001727 * @return LY_ERR values.
1728 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001729LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001730parse_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 +02001731{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001732 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001734 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001736 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737
David Sedlák60adc092019-08-06 15:57:02 +02001738 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001739 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001740
Radek Krejci39b7fc22021-02-26 23:29:18 +01001741 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001742 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001743
Michal Vasko7fbc8162018-09-17 10:35:16 +02001744 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001745 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001746 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001747
1748 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001749 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001750 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001751 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001752 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001754 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001755 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 +02001756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001757 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001758 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001760 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001761 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001763 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001764 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001766 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001767 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 +02001768 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001769 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001770 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001772 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001773 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001775 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001776 LY_CHECK_RET(parse_ext(ctx, word, word_len, any, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 break;
1778 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001779 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001780 return LY_EVALID;
1781 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001782 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001783 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001784
1785cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001786 return ret;
1787}
1788
Michal Vaskoea5abea2018-09-18 13:10:54 +02001789/**
1790 * @brief Parse the value or position statement. Substatement of type enum statement.
1791 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001792 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001793 * @param[in] val_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001794 * @param[in,out] enm Structure to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001795 * @return LY_ERR values.
1796 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001797LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001798parse_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 +02001799{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001800 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001801 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001802 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001803 long long num = 0;
1804 unsigned long long unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001805 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001806
Michal Vasko193dacd2022-10-13 08:43:05 +02001807 if (enm->flags & LYS_SET_VALUE) {
1808 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001809 ret = LY_EVALID;
1810 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001812 enm->flags |= LYS_SET_VALUE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001813
1814 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001815 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816
Radek Krejcid6b76452019-09-03 17:03:03 +02001817 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001818 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001819 ret = LY_EVALID;
1820 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821 }
1822
1823 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001824 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001825 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001826 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001827 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001828 ret = LY_EVALID;
1829 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001830 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001831 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001832 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001833 if (unum > UINT64_C(4294967295)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001834 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001835 ret = LY_EVALID;
1836 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001837 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001838 }
1839 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001840 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001841 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001842 ret = LY_EVALID;
1843 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844 }
1845 if (errno == ERANGE) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001846 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001847 ret = LY_EVALID;
1848 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001849 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001850 if (val_kw == LY_STMT_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001851 enm->value = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001852 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02001853 enm->value = unum;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001854 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001855
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001856 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001857 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001858 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001859 ret = parse_ext(ctx, word, word_len, enm, val_kw, 0, &enm->exts);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001860 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001861 break;
1862 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001863 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001864 ret = LY_EVALID;
1865 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001866 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001867 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868 }
Radek Krejci8b764662018-11-14 14:15:13 +01001869
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001870cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001871 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001872 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001873}
1874
Michal Vaskoea5abea2018-09-18 13:10:54 +02001875/**
1876 * @brief Parse the enum or bit statement. Substatement of type statement.
1877 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001878 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001879 * @param[in] enum_kw Type of this particular keyword.
1880 * @param[in,out] enums Enums or bits to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001881 * @return LY_ERR values.
1882 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001883static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001884parse_type_enum(struct lysp_yang_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001886 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001887 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001888 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001889 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001890 struct lysp_type_enum *enm;
1891
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001892 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001893
1894 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001895 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 +02001896 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001897 ret = lysp_check_enum_name((struct lysp_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001898 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001899 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001900
Michal Vasko12ef5362022-09-16 15:13:58 +02001901 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001902 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(enum_kw), enm->name);
Radek Krejci8b764662018-11-14 14:15:13 +01001903
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001904 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001905 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001906 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001907 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 +02001908 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001909 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001910 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001911 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001912 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001913 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001914 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 +02001915 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001916 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001917 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001918 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001919 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001920 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1921 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1922 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
David Sedlák9fb515f2019-07-11 10:33:58 +02001923 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001924 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001925 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1926 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1927 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001929 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001930 LY_CHECK_RET(parse_ext(ctx, word, word_len, enm, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931 break;
1932 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001933 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934 return LY_EVALID;
1935 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001936 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001937 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001938
1939cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001940 return ret;
1941}
1942
Michal Vaskoea5abea2018-09-18 13:10:54 +02001943/**
1944 * @brief Parse the fraction-digits statement. Substatement of type statement.
1945 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001946 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001947 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001948 * @return LY_ERR values.
1949 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001951parse_type_fracdigits(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001952{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001953 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001954 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001955 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001956 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001957 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001958
Michal Vasko193dacd2022-10-13 08:43:05 +02001959 if (type->fraction_digits) {
David Sedlákb3077192019-06-19 10:55:37 +02001960 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001961 return LY_EVALID;
1962 }
1963
1964 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001965 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001966
1967 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001968 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001969 ret = LY_EVALID;
1970 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001971 }
1972
1973 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001974 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001976 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001977 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001978 ret = LY_EVALID;
1979 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001980 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001981 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001982 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001983 ret = LY_EVALID;
1984 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001985 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001986 type->fraction_digits = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001988 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001991 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 +02001992 break;
1993 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001994 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001995 ret = LY_EVALID;
1996 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001997 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001998 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001999 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002000
2001cleanup:
2002 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 return ret;
2004}
2005
Michal Vaskoea5abea2018-09-18 13:10:54 +02002006/**
2007 * @brief Parse the require-instance statement. Substatement of type statement.
2008 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002009 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002010 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002011 * @return LY_ERR values.
2012 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002013static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002014parse_type_reqinstance(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002015{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002016 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002017 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002018 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002019 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020
Michal Vasko193dacd2022-10-13 08:43:05 +02002021 if (type->flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002022 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023 return LY_EVALID;
2024 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002025 type->flags |= LYS_SET_REQINST;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026
2027 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002028 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029
Radek Krejcif13b87b2020-12-01 22:02:17 +01002030 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002031 type->require_instance = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002032 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002033 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "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 Vasko7fbc8162018-09-17 10:35:16 +02002037
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002038 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002040 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002041 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 +02002042 break;
2043 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002044 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002045 ret = LY_EVALID;
2046 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002048 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002050
2051cleanup:
2052 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053 return ret;
2054}
2055
Michal Vaskoea5abea2018-09-18 13:10:54 +02002056/**
2057 * @brief Parse the modifier statement. Substatement of type pattern statement.
2058 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002059 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002060 * @param[in,out] restr Restriction to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002061 * @return LY_ERR values.
2062 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002063static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002064parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002065{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002066 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002067 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002068 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002069 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070
Michal Vasko193dacd2022-10-13 08:43:05 +02002071 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002072 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002073 return LY_EVALID;
2074 }
2075
2076 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002077 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078
Radek Krejcif13b87b2020-12-01 22:02:17 +01002079 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002080 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002081 ret = LY_EVALID;
2082 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002084
2085 /* replace the value in the dictionary */
Michal Vasko193dacd2022-10-13 08:43:05 +02002086 buf = malloc(strlen(restr->arg.str) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002087 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02002088 strcpy(buf, restr->arg.str);
2089 lydict_remove(PARSER_CTX(ctx), restr->arg.str);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090
Radek Krejcif13b87b2020-12-01 22:02:17 +01002091 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2092 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko183ea332022-11-08 10:50:03 +01002093 ret = lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002094 buf = NULL;
Michal Vasko183ea332022-11-08 10:50:03 +01002095 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002097 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002099 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002100 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 +02002101 break;
2102 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002103 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002104 ret = LY_EVALID;
2105 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002107 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002108 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002109
2110cleanup:
2111 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 return ret;
2113}
2114
Michal Vaskoea5abea2018-09-18 13:10:54 +02002115/**
2116 * @brief Parse the pattern statement. Substatement of type statement.
2117 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002118 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002119 * @param[in,out] patterns Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002120 * @return LY_ERR values.
2121 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002123parse_type_pattern(struct lysp_yang_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002124{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002125 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002127 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002128 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 struct lysp_restr *restr;
2130
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002131 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132
2133 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002134 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135
2136 /* add special meaning first byte */
2137 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002138 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 word = buf;
2140 } else {
2141 buf = malloc(word_len + 2);
2142 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002143 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002144 if (word_len) {
2145 memmove(buf + 1, word, word_len);
2146 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002147 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002148 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002149 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002150 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002152 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002154 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002155 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 +02002156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002157 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002158 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 +02002159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002160 case LY_STMT_ERROR_APP_TAG:
Michal Vasko193dacd2022-10-13 08:43:05 +02002161 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 +02002162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002163 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002164 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 +02002165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002166 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002167 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko193dacd2022-10-13 08:43:05 +02002168 LY_CHECK_RET(parse_type_pattern_modifier(ctx, restr));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002170 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002171 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002172 break;
2173 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002174 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175 return LY_EVALID;
2176 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002177 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002178 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002179
2180cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181 return ret;
2182}
2183
Michal Vaskoea5abea2018-09-18 13:10:54 +02002184/**
2185 * @brief Parse the type statement.
2186 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002187 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002188 * @param[in,out] type Type to wrote to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002189 * @return LY_ERR values.
2190 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002192parse_type(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002193{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002194 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002195 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002196 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002197 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002198 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002199 struct lysp_type *nest_type;
2200
2201 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002202 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002203 return LY_EVALID;
2204 }
2205
2206 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002207 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002208 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002210 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002211 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002212
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002213 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002215 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002216 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 +01002217 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002219 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002220 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002221 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002223 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002224 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002225 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002226 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002227 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002228 LY_CHECK_RET(parse_type_fracdigits(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002229 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002231 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002232 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002233 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234 return LY_EVALID;
2235 }
2236 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002237 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002238
Radek Krejci33090f92020-12-17 20:12:46 +01002239 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002240 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002242 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002243 if (type->path) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002244 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002245 return LY_EVALID;
2246 }
2247
aPiecek4bb1e372021-05-07 11:01:00 +02002248 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2249 * corresponding structure, so in case of failure, the lysp_module_free function will take
2250 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2251 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2252 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002253 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 +02002254 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002255 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002256 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002257 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002258 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002259 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002260 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002261 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002262 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002263 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002264 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002266 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002268 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269 return LY_EVALID;
2270 }
2271 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002272 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002273
Radek Krejci33090f92020-12-17 20:12:46 +01002274 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002275 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002277 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002278 LY_CHECK_RET(parse_type_reqinstance(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002279 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002280 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002281 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002282 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002283 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002284 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002285 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002286 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002287 LY_CHECK_RET(parse_ext(ctx, word, word_len, type, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 break;
2289 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002290 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002291 return LY_EVALID;
2292 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002293 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002294 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002295
2296cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002297 return ret;
2298}
2299
Michal Vaskoea5abea2018-09-18 13:10:54 +02002300/**
2301 * @brief Parse the leaf statement.
2302 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002303 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002304 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002305 * @return LY_ERR values.
2306 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002307LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002308parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002310 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002312 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002313 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314 struct lysp_node_leaf *leaf;
2315
David Sedlák60adc092019-08-06 15:57:02 +02002316 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002317 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002318 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002319 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002320
Michal Vasko7fbc8162018-09-17 10:35:16 +02002321 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002322 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002323 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002324
2325 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002326 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002327 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002328 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002329 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002331 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002332 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 +01002333 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002335 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002336 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 +02002337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002338 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002339 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002341 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002342 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002344 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002345 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002347 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002348 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 +02002349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002350 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002351 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002353 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002354 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002356 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002357 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 +02002358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002359 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002360 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002362 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002363 LY_CHECK_RET(parse_ext(ctx, word, word_len, leaf, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 break;
2365 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002366 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367 return LY_EVALID;
2368 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002369 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002371
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372 /* mandatory substatements */
2373 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002374 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375 return LY_EVALID;
2376 }
2377
Michal Vasko12ef5362022-09-16 15:13:58 +02002378cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002379 return ret;
2380}
2381
Michal Vaskoea5abea2018-09-18 13:10:54 +02002382/**
2383 * @brief Parse the max-elements statement.
2384 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002385 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002386 * @param[in,out] max Value to write to.
2387 * @param[in,out] flags Flags to write to.
2388 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002389 * @return LY_ERR values.
2390 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002391LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002392parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002393{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002394 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002395 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002396 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002397 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002398 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399
2400 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002401 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002402 return LY_EVALID;
2403 }
2404 *flags |= LYS_SET_MAX;
2405
2406 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002407 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408
2409 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002410 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002411 ret = LY_EVALID;
2412 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413 }
2414
Radek Krejci7f9b6512019-09-18 13:11:09 +02002415 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002417 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002419 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002420 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002421 ret = LY_EVALID;
2422 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423 }
2424 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002425 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002426 ret = LY_EVALID;
2427 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428 }
2429
2430 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002431 } else {
2432 /* unbounded */
2433 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002434 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002436 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002438 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002439 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 +02002440 break;
2441 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002442 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002443 ret = LY_EVALID;
2444 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002446 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002447 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002448
2449cleanup:
2450 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002451 return ret;
2452}
2453
Michal Vaskoea5abea2018-09-18 13:10:54 +02002454/**
2455 * @brief Parse the min-elements statement.
2456 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002457 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002458 * @param[in,out] min Value to write to.
2459 * @param[in,out] flags Flags to write to.
2460 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002461 * @return LY_ERR values.
2462 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002463LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002464parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002466 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002467 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002468 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002469 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002470 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002471
2472 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002473 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002474 return LY_EVALID;
2475 }
2476 *flags |= LYS_SET_MIN;
2477
2478 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002479 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480
2481 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002482 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002483 ret = LY_EVALID;
2484 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002485 }
2486
2487 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002488 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002489 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002490 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002491 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002492 ret = LY_EVALID;
2493 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002494 }
2495 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002496 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002497 ret = LY_EVALID;
2498 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 }
2500 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002502 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002504 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002505 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 +02002506 break;
2507 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002508 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002509 ret = LY_EVALID;
2510 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002511 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002512 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002514
2515cleanup:
2516 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 return ret;
2518}
2519
Michal Vaskoea5abea2018-09-18 13:10:54 +02002520/**
2521 * @brief Parse the ordered-by statement.
2522 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002523 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002524 * @param[in,out] llist List or leaf-list to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002525 * @return LY_ERR values.
2526 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002528parse_orderedby(struct lysp_yang_ctx *ctx, struct lysp_node *llist)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002529{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002530 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002531 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002532 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002533 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534
Michal Vasko193dacd2022-10-13 08:43:05 +02002535 if (llist->flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002536 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 return LY_EVALID;
2538 }
2539
2540 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002541 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542
Radek Krejcif13b87b2020-12-01 22:02:17 +01002543 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002544 llist->flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002545 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002546 llist->flags |= LYS_ORDBY_USER;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002547 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002548 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "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 Vasko7fbc8162018-09-17 10:35:16 +02002552
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002553 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002555 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002556 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 +02002557 break;
2558 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002559 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002560 ret = LY_EVALID;
2561 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002562 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002563 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002564 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002565
2566cleanup:
2567 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002568 return ret;
2569}
2570
Michal Vaskoea5abea2018-09-18 13:10:54 +02002571/**
2572 * @brief Parse the leaf-list statement.
2573 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002574 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002575 * @param[in,out] siblings Siblings to add to.
2576 *
2577 * @return LY_ERR values.
2578 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002579LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002580parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002582 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002583 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002584 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002585 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586 struct lysp_node_leaflist *llist;
2587
David Sedlák60adc092019-08-06 15:57:02 +02002588 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002589 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002591 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002592
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002594 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002595 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002596
2597 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002598 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002599 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002600 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002601 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002603 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002604 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002605 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002607 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002608 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 +02002609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002610 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002611 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002613 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002614 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002616 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002617 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002619 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002620 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002622 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02002623 LY_CHECK_RET(parse_orderedby(ctx, &llist->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002626 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 +02002627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002629 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002631 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002632 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002634 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002635 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 +02002636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002637 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002638 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002640 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002641 LY_CHECK_RET(parse_ext(ctx, word, word_len, llist, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642 break;
2643 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002644 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 return LY_EVALID;
2646 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002647 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002648 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002649
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650 /* mandatory substatements */
2651 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002652 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653 return LY_EVALID;
2654 }
2655
Michal Vasko12ef5362022-09-16 15:13:58 +02002656cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657 return ret;
2658}
2659
Michal Vaskoea5abea2018-09-18 13:10:54 +02002660/**
2661 * @brief Parse the refine statement.
2662 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002663 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002664 * @param[in,out] refines Refines to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002665 * @return LY_ERR values.
2666 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002668parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002670 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002672 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002673 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002674 struct lysp_refine *rf;
2675
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002676 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002677
2678 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002679 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002680 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002681 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002683 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002685 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002686 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002688 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002689 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002690 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002691 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002692 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 +02002693 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002694 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002695 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002696 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002698 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002699 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002701 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002702 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002704 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002705 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002707 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002708 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002709 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002710 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002711 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 +02002712 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002713 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002714 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 +02002715 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002716 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002717 LY_CHECK_RET(parse_ext(ctx, word, word_len, rf, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718 break;
2719 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002720 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 return LY_EVALID;
2722 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002723 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002724 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002725
2726cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727 return ret;
2728}
2729
Michal Vaskoea5abea2018-09-18 13:10:54 +02002730/**
2731 * @brief Parse the typedef statement.
2732 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002733 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002734 * @param[in,out] typedefs Typedefs to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002735 * @return LY_ERR values.
2736 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002738parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002740 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002742 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002743 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002744 struct lysp_tpdf *tpdf;
2745
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002746 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002747
2748 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002749 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002750 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751
2752 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002753 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002754 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002755 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02002756 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 +01002757 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002759 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002760 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 +02002761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002762 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002763 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 +02002764 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002765 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002766 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002768 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002769 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002771 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002772 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 +02002773 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002774 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002775 LY_CHECK_RET(parse_ext(ctx, word, word_len, tpdf, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 break;
2777 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002778 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002779 return LY_EVALID;
2780 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002781 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002782 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002783
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784 /* mandatory substatements */
2785 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002786 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 return LY_EVALID;
2788 }
2789
Radek Krejcibbe09a92018-11-08 09:36:54 +01002790 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002791 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002792 assert(ctx->main_ctx);
2793 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002794 }
2795
Michal Vasko12ef5362022-09-16 15:13:58 +02002796cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797 return ret;
2798}
2799
Michal Vaskoea5abea2018-09-18 13:10:54 +02002800/**
2801 * @brief Parse the input or output statement.
2802 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002803 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002804 * @param[in] kw Type of this particular keyword
2805 * @param[in,out] inout_p Input/output pointer to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002806 * @return LY_ERR values.
2807 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002809parse_inout(struct lysp_yang_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002810 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002812 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002813 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002814 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002815 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002816 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002818 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002819 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 return LY_EVALID;
2821 }
2822
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002823 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002824 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2825 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002826 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827
2828 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002829 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002831 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002832 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002833 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002834 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002835 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002837 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002838 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002840 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002841 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002843 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002844 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002846 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002847 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002849 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002850 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002852 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002853 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002855 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002856 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002858 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002859 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002860 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002862 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002863 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002865 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002866 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_p, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 break;
2868 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002869 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 return LY_EVALID;
2871 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002872 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002874
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002875cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 return ret;
2877}
2878
Michal Vaskoea5abea2018-09-18 13:10:54 +02002879/**
2880 * @brief Parse the action statement.
2881 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002882 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002883 * @param[in,out] actions Actions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002884 * @return LY_ERR values.
2885 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002886LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002887parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002889 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002891 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002892 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002893 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002894
Radek Krejci2a9fc652021-01-22 17:44:34 +01002895 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002896
2897 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002898 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002899 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002900 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002901 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002902
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002903 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002905 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002906 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 +02002907 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002908 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002909 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002911 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002912 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 +02002913 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002914 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002915 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
2917
Radek Krejcid6b76452019-09-03 17:03:03 +02002918 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002919 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002920 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002921 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002922 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 break;
2924
Radek Krejcid6b76452019-09-03 17:03:03 +02002925 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002926 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002927 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002928 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002929 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002930 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002931 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002932 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 +02002933 break;
2934 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002935 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002936 return LY_EVALID;
2937 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002938 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002940
2941 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2942 if (!act->input.nodetype) {
2943 act->input.nodetype = LYS_INPUT;
2944 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002945 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002946 }
2947 if (!act->output.nodetype) {
2948 act->output.nodetype = LYS_OUTPUT;
2949 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002950 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002951 }
2952
Michal Vasko12ef5362022-09-16 15:13:58 +02002953cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002954 return ret;
2955}
2956
Michal Vaskoea5abea2018-09-18 13:10:54 +02002957/**
2958 * @brief Parse the notification statement.
2959 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002960 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002961 * @param[in,out] notifs Notifications to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002962 * @return LY_ERR values.
2963 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002964LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002965parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002966{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002967 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002969 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002970 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002971 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002972
Radek Krejci2a9fc652021-01-22 17:44:34 +01002973 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974
2975 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002976 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002977 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002978 notif->nodetype = LYS_NOTIF;
2979 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002980
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002981 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002983 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02002984 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 +02002985 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002986 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002987 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002988 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002989 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002990 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 +02002991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002992 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002993 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 break;
2995
Radek Krejcid6b76452019-09-03 17:03:03 +02002996 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002997 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002998 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002999 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003000 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003002 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01003003 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003004 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003005 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003006 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003008 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003009 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003011 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003012 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003014 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003015 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003017 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003018 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 break;
3020
Radek Krejcid6b76452019-09-03 17:03:03 +02003021 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003022 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003023 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003025 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003026 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003028 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003029 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003031 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003032 LY_CHECK_RET(parse_ext(ctx, word, word_len, notif, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 break;
3034 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003035 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003036 return LY_EVALID;
3037 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003038 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003039 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003040
Michal Vasko12ef5362022-09-16 15:13:58 +02003041cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003042 return ret;
3043}
3044
Michal Vaskoea5abea2018-09-18 13:10:54 +02003045/**
3046 * @brief Parse the grouping statement.
3047 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003048 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003049 * @param[in,out] groupings Groupings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003050 * @return LY_ERR values.
3051 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003052LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003053parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003054{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003055 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003057 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003058 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003059 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060
Radek Krejci2a9fc652021-01-22 17:44:34 +01003061 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062
3063 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003064 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003065 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003066 grp->nodetype = LYS_GROUPING;
3067 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003068
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003069 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003070 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003071 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003072 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 +02003073 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003074 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003075 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 +02003076 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003077 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003078 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 break;
3080
Radek Krejcid6b76452019-09-03 17:03:03 +02003081 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003082 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003083 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003084 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003085 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003087 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003088 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003090 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003091 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003093 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003094 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003096 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003097 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003099 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003100 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003102 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003103 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104 break;
3105
Radek Krejcid6b76452019-09-03 17:03:03 +02003106 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003107 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003109 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003110 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003111 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003113 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003114 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003116 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003117 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003118 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003120 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003121 LY_CHECK_RET(parse_ext(ctx, word, word_len, grp, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 break;
3123 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003124 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125 return LY_EVALID;
3126 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003127 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003129
aPiecek63e080d2021-06-29 13:53:28 +02003130 /* store data for collision check */
3131 if (parent) {
3132 assert(ctx->main_ctx);
3133 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3134 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003135
Michal Vasko12ef5362022-09-16 15:13:58 +02003136cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137 return ret;
3138}
3139
Michal Vaskoea5abea2018-09-18 13:10:54 +02003140/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003141 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003142 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003143 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003144 * @param[in,out] augments Augments to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003145 * @return LY_ERR values.
3146 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003147LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003148parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003149{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003150 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003152 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003153 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003154 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003155
Radek Krejci2a9fc652021-01-22 17:44:34 +01003156 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157
3158 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003159 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003160 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003161 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003162 aug->nodetype = LYS_AUGMENT;
3163 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003165 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003167 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003168 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 +02003169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003170 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003171 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003173 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003174 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 +02003175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003176 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003177 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003179 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003180 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003181 break;
3182
Radek Krejcid6b76452019-09-03 17:03:03 +02003183 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003184 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003185 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003186 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003187 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003189 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003190 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003192 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003193 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003195 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003196 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003198 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003199 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003200 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003201 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003202 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003204 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003205 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003206 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003207 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003208 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003209 break;
3210
Radek Krejcid6b76452019-09-03 17:03:03 +02003211 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003212 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003213 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003216 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003217 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003219 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003220 LY_CHECK_RET(parse_ext(ctx, word, word_len, aug, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221 break;
3222 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003223 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003224 return LY_EVALID;
3225 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003226 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003228
Michal Vasko12ef5362022-09-16 15:13:58 +02003229cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 return ret;
3231}
3232
Michal Vaskoea5abea2018-09-18 13:10:54 +02003233/**
3234 * @brief Parse the uses statement.
3235 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003236 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003237 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003238 * @return LY_ERR values.
3239 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003240LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003241parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003243 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003245 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003246 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247 struct lysp_node_uses *uses;
3248
David Sedlák60adc092019-08-06 15:57:02 +02003249 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003250 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003252 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003255 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003256 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257
3258 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003259 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003261 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003262 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 +02003263 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003264 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003265 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003267 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003268 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 +02003269 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003270 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003271 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003273 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003274 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275 break;
3276
Radek Krejcid6b76452019-09-03 17:03:03 +02003277 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003278 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003280 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003281 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003282 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003283 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003284 LY_CHECK_RET(parse_ext(ctx, word, word_len, uses, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285 break;
3286 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003287 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288 return LY_EVALID;
3289 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003290 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003291 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003292
Michal Vasko12ef5362022-09-16 15:13:58 +02003293cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294 return ret;
3295}
3296
Michal Vaskoea5abea2018-09-18 13:10:54 +02003297/**
3298 * @brief Parse the case statement.
3299 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003300 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003301 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003302 * @return LY_ERR values.
3303 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003304LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003305parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003307 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003309 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003310 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 struct lysp_node_case *cas;
3312
David Sedlák60adc092019-08-06 15:57:02 +02003313 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003314 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003316 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003319 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003320 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321
3322 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003323 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003324 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003325 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003326 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 +02003327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003328 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003329 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003331 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003332 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 +02003333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003334 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003335 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003336 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003337 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003338 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339 break;
3340
Radek Krejcid6b76452019-09-03 17:03:03 +02003341 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003342 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003343 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003344 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003345 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003347 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003348 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003350 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003351 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003353 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003354 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003356 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003357 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003359 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003360 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003362 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003363 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003365 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003366 LY_CHECK_RET(parse_ext(ctx, word, word_len, cas, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367 break;
3368 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003369 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 return LY_EVALID;
3371 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003372 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003373 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003374
3375cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003376 return ret;
3377}
3378
Michal Vaskoea5abea2018-09-18 13:10:54 +02003379/**
3380 * @brief Parse the choice statement.
3381 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003382 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003383 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003384 * @return LY_ERR values.
3385 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003386LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003387parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003389 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003391 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003392 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003393 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394
David Sedlák60adc092019-08-06 15:57:02 +02003395 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003396 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003397 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003398 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399
Michal Vasko7fbc8162018-09-17 10:35:16 +02003400 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003401 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003402 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403
3404 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003405 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003407 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003408 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003410 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003411 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 +02003412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003414 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003416 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003417 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003419 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003420 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 +02003421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003422 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003423 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003425 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003426 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003429 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 +02003430 &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003431 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
3433
Radek Krejcid6b76452019-09-03 17:03:03 +02003434 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003435 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003436 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003437 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003438 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003440 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003441 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003443 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003444 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003445 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003448 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003451 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003454 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003456 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003457 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003459 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003460 LY_CHECK_RET(parse_ext(ctx, word, word_len, choice, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 break;
3462 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003463 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 return LY_EVALID;
3465 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003466 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003468
3469cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003470 return ret;
3471}
3472
Michal Vaskoea5abea2018-09-18 13:10:54 +02003473/**
3474 * @brief Parse the container statement.
3475 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003476 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003477 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003478 * @return LY_ERR values.
3479 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003480LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003481parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003482{
3483 LY_ERR ret = 0;
3484 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003485 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003486 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 struct lysp_node_container *cont;
3488
David Sedlák60adc092019-08-06 15:57:02 +02003489 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003490 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003492 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003495 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003496 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497
3498 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003499 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003501 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003502 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003504 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003505 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 +02003506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003507 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003508 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003510 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003511 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 +02003512 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003513 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003514 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003515 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003516 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003517 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003519 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003520 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 +02003521 break;
3522
Radek Krejcid6b76452019-09-03 17:03:03 +02003523 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003524 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003525 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003527 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003529 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003530 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003532 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003533 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003535 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003536 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003538 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003539 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003541 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003542 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003544 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003545 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 break;
3547
Radek Krejcid6b76452019-09-03 17:03:03 +02003548 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003549 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003551 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003552 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003554 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003555 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003556 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003558 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003559 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003561 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003562 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003563 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003565 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003566 LY_CHECK_RET(parse_ext(ctx, word, word_len, cont, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 break;
3568 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003569 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570 return LY_EVALID;
3571 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003572 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003574
Michal Vasko12ef5362022-09-16 15:13:58 +02003575cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576 return ret;
3577}
3578
Michal Vaskoea5abea2018-09-18 13:10:54 +02003579/**
3580 * @brief Parse the list statement.
3581 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003582 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003583 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003584 * @return LY_ERR values.
3585 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003586LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003587parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003589 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003591 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003592 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593 struct lysp_node_list *list;
3594
David Sedlák60adc092019-08-06 15:57:02 +02003595 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003596 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003598 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003601 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003602 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003603
3604 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003605 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003606 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003607 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003608 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003610 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003611 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 +02003612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003613 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003614 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003616 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003617 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 +02003618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003619 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003620 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003622 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003623 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003625 case LY_STMT_KEY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003626 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 +02003627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003628 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003629 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003631 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003632 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003634 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003635 LY_CHECK_RET(parse_orderedby(ctx, &list->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003637 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003638 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639 break;
3640
Radek Krejcid6b76452019-09-03 17:03:03 +02003641 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003642 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003643 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003644 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003645 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003647 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003648 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003650 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003651 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003653 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003654 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003655 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003656 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003657 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003659 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003660 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003661 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003662 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003663 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664 break;
3665
Radek Krejcid6b76452019-09-03 17:03:03 +02003666 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003667 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003669 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003670 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003672 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003673 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003674 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003676 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003677 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003679 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003680 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003681 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003683 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003684 LY_CHECK_RET(parse_ext(ctx, word, word_len, list, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 break;
3686 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003687 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688 return LY_EVALID;
3689 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003690 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003691 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003692
Michal Vasko12ef5362022-09-16 15:13:58 +02003693cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 return ret;
3695}
3696
Michal Vaskoea5abea2018-09-18 13:10:54 +02003697/**
3698 * @brief Parse the yin-element statement.
3699 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003700 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003701 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003702 * @return LY_ERR values.
3703 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003705parse_yinelement(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003706{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003707 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003708 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003709 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003710 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003711
Michal Vasko193dacd2022-10-13 08:43:05 +02003712 if (ext->flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003713 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714 return LY_EVALID;
3715 }
3716
3717 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003718 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719
Radek Krejcif13b87b2020-12-01 22:02:17 +01003720 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003721 ext->flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003722 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003723 ext->flags |= LYS_YINELEM_FALSE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003725 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726 free(buf);
3727 return LY_EVALID;
3728 }
3729 free(buf);
3730
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003731 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003733 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003734 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_YIN_ELEMENT, 0, &ext->exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003735 LY_CHECK_RET(ret);
3736 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003737 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003738 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739 return LY_EVALID;
3740 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003741 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003743
3744cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 return ret;
3746}
3747
Michal Vaskoea5abea2018-09-18 13:10:54 +02003748/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003749 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003750 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003751 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003752 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003753 * @return LY_ERR values.
3754 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003756parse_argument(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003758 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003760 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003761 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762
Michal Vasko193dacd2022-10-13 08:43:05 +02003763 if (ext->argname) {
David Sedlákb3077192019-06-19 10:55:37 +02003764 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765 return LY_EVALID;
3766 }
3767
3768 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003769 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko193dacd2022-10-13 08:43:05 +02003770 INSERT_WORD_GOTO(ctx, buf, ext->argname, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003771
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003772 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003773 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003774 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003775 LY_CHECK_RET(parse_yinelement(ctx, ext));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003776 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003777 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003778 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_ARGUMENT, 0, &ext->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 break;
3780 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003781 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003782 return LY_EVALID;
3783 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003784 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003786
3787cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 return ret;
3789}
3790
Michal Vaskoea5abea2018-09-18 13:10:54 +02003791/**
3792 * @brief Parse the extension statement.
3793 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003794 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003795 * @param[in,out] extensions Extensions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003796 * @return LY_ERR values.
3797 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003799parse_extension(struct lysp_yang_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003800{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003801 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003802 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003803 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003804 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805 struct lysp_ext *ex;
3806
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003807 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808
3809 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003810 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003811 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003813 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003815 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003816 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 +02003817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003818 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003819 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 +02003820 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003821 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003822 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003824 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003825 LY_CHECK_RET(parse_argument(ctx, ex));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003827 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003828 LY_CHECK_RET(parse_ext(ctx, word, word_len, ex, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 break;
3830 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003831 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 return LY_EVALID;
3833 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003834 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003836
3837cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838 return ret;
3839}
3840
Michal Vaskoea5abea2018-09-18 13:10:54 +02003841/**
3842 * @brief Parse the deviate statement.
3843 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003844 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003845 * @param[in,out] deviates Deviates to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003846 * @return LY_ERR values.
3847 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003848LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003849parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003851 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003852 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003853 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003854 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003855 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003856 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003857 struct lysp_deviate_add *d_add = NULL;
3858 struct lysp_deviate_rpl *d_rpl = NULL;
3859 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003860 const char **d_units = NULL;
3861 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003862 struct lysp_restr **d_musts = NULL;
3863 uint16_t *d_flags = 0;
3864 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865
3866 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003867 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003868
Radek Krejcif13b87b2020-12-01 22:02:17 +01003869 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003871 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003873 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003875 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003876 dev_mod = LYS_DEV_DELETE;
3877 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003878 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003879 ret = LY_EVALID;
3880 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003882
3883 /* create structure */
3884 switch (dev_mod) {
3885 case LYS_DEV_NOT_SUPPORTED:
3886 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003887 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888 break;
3889 case LYS_DEV_ADD:
3890 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003891 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 d = (struct lysp_deviate *)d_add;
3893 d_units = &d_add->units;
3894 d_uniques = &d_add->uniques;
3895 d_dflts = &d_add->dflts;
3896 d_musts = &d_add->musts;
3897 d_flags = &d_add->flags;
3898 d_min = &d_add->min;
3899 d_max = &d_add->max;
3900 break;
3901 case LYS_DEV_REPLACE:
3902 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003903 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 d = (struct lysp_deviate *)d_rpl;
3905 d_units = &d_rpl->units;
3906 d_flags = &d_rpl->flags;
3907 d_min = &d_rpl->min;
3908 d_max = &d_rpl->max;
3909 break;
3910 case LYS_DEV_DELETE:
3911 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003912 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003913 d = (struct lysp_deviate *)d_del;
3914 d_units = &d_del->units;
3915 d_uniques = &d_del->uniques;
3916 d_dflts = &d_del->dflts;
3917 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003918 break;
3919 default:
3920 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003921 LOGINT(PARSER_CTX(ctx));
3922 ret = LY_EINT;
3923 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003924 }
3925 d->mod = dev_mod;
3926
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003927 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003929 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 switch (dev_mod) {
3931 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003932 case LYS_DEV_DELETE:
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 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003937 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938 break;
3939 }
3940 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003941 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942 switch (dev_mod) {
3943 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02003944 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003945 ret = LY_EVALID;
3946 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003948 ret = parse_text_field(ctx, &d_rpl->dflt, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts);
3949 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003950 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003951 break;
3952 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003953 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 +02003954 break;
3955 }
3956 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003957 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003958 switch (dev_mod) {
3959 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003960 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003961 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003962 ret = LY_EVALID;
3963 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003965 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003966 break;
3967 }
3968 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003969 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003970 switch (dev_mod) {
3971 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003972 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003973 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003974 ret = LY_EVALID;
3975 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003977 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978 break;
3979 }
3980 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003981 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003982 switch (dev_mod) {
3983 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003984 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003985 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003986 ret = LY_EVALID;
3987 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003988 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003989 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990 break;
3991 }
3992 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003993 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003994 switch (dev_mod) {
3995 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003996 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003997 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003998 ret = LY_EVALID;
3999 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004001 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004002 break;
4003 }
4004 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004005 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004006 switch (dev_mod) {
4007 case LYS_DEV_NOT_SUPPORTED:
4008 case LYS_DEV_ADD:
4009 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004010 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004011 ret = LY_EVALID;
4012 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004014 if (d_rpl->type) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004015 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004016 ret = LY_EVALID;
4017 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004018 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004020 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4021 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004022 break;
4023 }
4024 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004025 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004026 switch (dev_mod) {
4027 case LYS_DEV_NOT_SUPPORTED:
4028 case LYS_DEV_REPLACE:
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 Vasko12ef5362022-09-16 15:13:58 +02004033 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 +02004034 break;
4035 }
4036 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004037 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004038 switch (dev_mod) {
4039 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02004040 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004041 ret = LY_EVALID;
4042 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004044 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 +02004045 break;
4046 }
4047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004048 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004049 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 +02004050 break;
4051 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004052 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004053 ret = LY_EVALID;
4054 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004055 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004056 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004058
4059cleanup:
4060 free(buf);
4061 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004062 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004063 free(d);
4064 } else {
4065 /* insert into siblings */
4066 LY_LIST_INSERT(deviates, d, next);
4067 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004068 return ret;
4069}
4070
Michal Vaskoea5abea2018-09-18 13:10:54 +02004071/**
4072 * @brief Parse the deviation statement.
4073 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004074 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004075 * @param[in,out] deviations Deviations to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004076 * @return LY_ERR values.
4077 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004078LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004079parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004080{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004081 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004082 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004083 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004084 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004085 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004086 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004087
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004088 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004089
4090 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004091 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004092 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004093 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004094
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004095 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004096 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004097 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004098 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 +02004099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004100 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004101 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004103 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004104 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 +02004105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004106 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004107 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 +02004108 break;
4109 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004110 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004111 ret = LY_EVALID;
4112 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004113 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004114 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004116
Michal Vasko7fbc8162018-09-17 10:35:16 +02004117 /* mandatory substatements */
4118 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004119 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004120 ret = LY_EVALID;
4121 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004122 }
4123
Michal Vasko12ef5362022-09-16 15:13:58 +02004124cleanup:
4125 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004126 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004127 LY_ARRAY_DECREMENT_FREE(*deviations);
4128 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004129 return ret;
4130}
4131
Michal Vaskoea5abea2018-09-18 13:10:54 +02004132/**
4133 * @brief Parse the feature statement.
4134 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004135 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004136 * @param[in,out] features Features to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004137 * @return LY_ERR values.
4138 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004139LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004140parse_feature(struct lysp_yang_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004141{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004142 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004143 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004144 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004145 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004146 struct lysp_feature *feat;
4147
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004148 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004149
4150 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004151 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004152 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004153
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004154 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004156 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004157 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 +02004158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004159 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004160 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004162 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004163 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 +02004164 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004165 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004166 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004168 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004169 LY_CHECK_RET(parse_ext(ctx, word, word_len, feat, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 break;
4171 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004172 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004173 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004174 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004175 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004176 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004177
4178cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004179 return ret;
4180}
4181
Michal Vaskoea5abea2018-09-18 13:10:54 +02004182/**
4183 * @brief Parse the identity statement.
4184 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004185 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004186 * @param[in,out] identities Identities to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004187 * @return LY_ERR values.
4188 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004189LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004190parse_identity(struct lysp_yang_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004191{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004192 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004194 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004195 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 struct lysp_ident *ident;
4197
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004198 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199
4200 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004201 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004202 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004203
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004204 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004206 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004207 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 +02004208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004209 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004210 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004211 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004213 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004214 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 +02004215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004216 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004217 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004219 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004220 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004221 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 +01004222 return LY_EVALID;
4223 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004224 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 +02004225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004226 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004227 LY_CHECK_RET(parse_ext(ctx, word, word_len, ident, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004228 break;
4229 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004230 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004231 return LY_EVALID;
4232 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004233 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004234 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004235
4236cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004237 return ret;
4238}
4239
Michal Vaskoea5abea2018-09-18 13:10:54 +02004240/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004241 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004242 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004243 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004244 * @param[in,out] mod Module to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004245 * @return LY_ERR values.
4246 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004247LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004248parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004249{
4250 LY_ERR ret = 0;
4251 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004252 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004253 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004254 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004255 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004256
Michal Vaskoc3781c32020-10-06 14:04:08 +02004257 mod->is_submod = 0;
4258
4259 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004260 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004261 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004262
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004263 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004264
Radek Krejcie3846472018-10-15 15:24:51 +02004265#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004266 if (mod_stmt > SECTION) {\
4267 LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;\
4268 } mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004269
Michal Vasko7fbc8162018-09-17 10:35:16 +02004270 switch (kw) {
4271 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004272 case LY_STMT_NAMESPACE:
4273 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004274 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4275 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004276 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004277 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004278 break;
4279 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004280 case LY_STMT_INCLUDE:
4281 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004282 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004283 break;
4284 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004285 case LY_STMT_ORGANIZATION:
4286 case LY_STMT_CONTACT:
4287 case LY_STMT_DESCRIPTION:
4288 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004289 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004290 break;
4291
4292 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004293 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004294 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004295 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004296 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004297 case LY_STMT_ANYDATA:
4298 case LY_STMT_ANYXML:
4299 case LY_STMT_AUGMENT:
4300 case LY_STMT_CHOICE:
4301 case LY_STMT_CONTAINER:
4302 case LY_STMT_DEVIATION:
4303 case LY_STMT_EXTENSION:
4304 case LY_STMT_FEATURE:
4305 case LY_STMT_GROUPING:
4306 case LY_STMT_IDENTITY:
4307 case LY_STMT_LEAF:
4308 case LY_STMT_LEAF_LIST:
4309 case LY_STMT_LIST:
4310 case LY_STMT_NOTIFICATION:
4311 case LY_STMT_RPC:
4312 case LY_STMT_TYPEDEF:
4313 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004314 mod_stmt = Y_MOD_BODY;
4315 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004316 case LY_STMT_EXTENSION_INSTANCE:
4317 /* no place in the statement order defined */
4318 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004319 default:
4320 /* error handled in the next switch */
4321 break;
4322 }
Radek Krejcie3846472018-10-15 15:24:51 +02004323#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004324
Radek Krejcie3846472018-10-15 15:24:51 +02004325 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004326 switch (kw) {
4327 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004328 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004329 LY_CHECK_RET(parse_yangversion(ctx, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004331 case LY_STMT_NAMESPACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004332 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 +02004333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004334 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02004335 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 +02004336 break;
4337
4338 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004339 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004340 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004341 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004342 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004343 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004344 break;
4345
4346 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004347 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004348 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 +02004349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004350 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004351 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 +02004352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004353 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004354 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 +02004355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004356 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004357 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 +02004358 break;
4359
4360 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004361 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004362 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004363 break;
4364
4365 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004367 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004368 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004370 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004373 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004376 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004379 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004381 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004382 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004385 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004388 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004389 break;
4390
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004392 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004395 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004397 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004398 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004399 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004400 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004401 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004403 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004404 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004406 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004407 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004409 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004410 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004412 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004413 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004415 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004416 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004418 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004419 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004420 break;
4421
4422 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004423 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004424 return LY_EVALID;
4425 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004426 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004427 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004428
Michal Vasko7fbc8162018-09-17 10:35:16 +02004429 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004430 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004431 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004432 return LY_EVALID;
4433 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004434 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004435 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004436 }
4437
Radek Krejcie9e987e2018-10-31 12:50:27 +01004438 /* submodules share the namespace with the module names, so there must not be
4439 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004440 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004441 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004442 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004443 return LY_EVALID;
4444 }
4445
Michal Vasko12ef5362022-09-16 15:13:58 +02004446cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004447 return ret;
4448}
4449
4450/**
4451 * @brief Parse submodule substatements.
4452 *
4453 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004454 * @param[out] submod Parsed submodule structure.
4455 *
4456 * @return LY_ERR values.
4457 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004458LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004459parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004460{
4461 LY_ERR ret = 0;
4462 char *buf, *word;
4463 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004464 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004465 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004466 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004467
Michal Vaskoc3781c32020-10-06 14:04:08 +02004468 submod->is_submod = 1;
4469
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004470 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004471 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004472 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004473
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004474 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004475
4476#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004477 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 +01004478
4479 switch (kw) {
4480 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004481 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004482 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004484 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004485 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4486 break;
4487 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004488 case LY_STMT_INCLUDE:
4489 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004490 CHECK_ORDER(Y_MOD_LINKAGE);
4491 break;
4492 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004493 case LY_STMT_ORGANIZATION:
4494 case LY_STMT_CONTACT:
4495 case LY_STMT_DESCRIPTION:
4496 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004497 CHECK_ORDER(Y_MOD_META);
4498 break;
4499
4500 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004501 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004502 CHECK_ORDER(Y_MOD_REVISION);
4503 break;
4504 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004505 case LY_STMT_ANYDATA:
4506 case LY_STMT_ANYXML:
4507 case LY_STMT_AUGMENT:
4508 case LY_STMT_CHOICE:
4509 case LY_STMT_CONTAINER:
4510 case LY_STMT_DEVIATION:
4511 case LY_STMT_EXTENSION:
4512 case LY_STMT_FEATURE:
4513 case LY_STMT_GROUPING:
4514 case LY_STMT_IDENTITY:
4515 case LY_STMT_LEAF:
4516 case LY_STMT_LEAF_LIST:
4517 case LY_STMT_LIST:
4518 case LY_STMT_NOTIFICATION:
4519 case LY_STMT_RPC:
4520 case LY_STMT_TYPEDEF:
4521 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004522 mod_stmt = Y_MOD_BODY;
4523 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004524 case LY_STMT_EXTENSION_INSTANCE:
4525 /* no place in the statement order defined */
4526 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004527 default:
4528 /* error handled in the next switch */
4529 break;
4530 }
4531#undef CHECK_ORDER
4532
4533 prev_kw = kw;
4534 switch (kw) {
4535 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004536 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004537 LY_CHECK_RET(parse_yangversion(ctx, (struct lysp_module *)submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004538 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004539 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02004540 LY_CHECK_RET(parse_belongsto(ctx, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004541 break;
4542
4543 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004544 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004545 if (submod->version == LYS_VERSION_1_1) {
4546 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4547 submod->name);
4548 }
Radek Krejci33090f92020-12-17 20:12:46 +01004549 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004551 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004552 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004553 break;
4554
4555 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004556 case LY_STMT_ORGANIZATION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004557 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 +01004558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004559 case LY_STMT_CONTACT:
Michal Vasko193dacd2022-10-13 08:43:05 +02004560 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 +01004561 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004562 case LY_STMT_DESCRIPTION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004563 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 +01004564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004565 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004566 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 +01004567 break;
4568
4569 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004570 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004571 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004572 break;
4573
4574 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004575 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004576 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004577 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004578 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004579 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004580 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004581 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004582 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004583 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004584 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004585 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004586 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004587 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004588 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004589 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004590 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004591 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004593 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004594 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004595 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004596 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004597 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004598 break;
4599
Radek Krejcid6b76452019-09-03 17:03:03 +02004600 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004601 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004603 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004604 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004606 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004607 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004609 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004610 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004611 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004612 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004613 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004615 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004616 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004618 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004619 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004621 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004622 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004624 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004625 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004627 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004628 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004629 break;
4630
4631 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004632 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004633 return LY_EVALID;
4634 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004635 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004636 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004637
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004638 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004639 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004640 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004641 return LY_EVALID;
4642 }
4643
4644 /* submodules share the namespace with the module names, so there must not be
4645 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004646 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004647 /* main modules may have different revisions */
4648 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004649 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004650 return LY_EVALID;
4651 }
4652
Michal Vasko12ef5362022-09-16 15:13:58 +02004653cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004654 return ret;
4655}
4656
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004657/**
4658 * @brief Skip any redundant characters, namely whitespaces and comments.
4659 *
4660 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004661 * @return LY_SUCCESS on success.
4662 * @return LY_EVALID on invalid comment.
4663 */
4664static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004665skip_redundant_chars(struct lysp_yang_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004666{
4667 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004668 while (ctx->in->current[0]) {
4669 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004670 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004671 ly_in_skip(ctx->in, 2);
4672 LY_CHECK_RET(skip_comment(ctx, 1));
4673 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004674 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004675 ly_in_skip(ctx->in, 2);
4676 LY_CHECK_RET(skip_comment(ctx, 2));
4677 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004678 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004679 if (ctx->in->current[0] == '\n') {
4680 LY_IN_NEW_LINE(ctx->in);
4681 }
Radek Krejci33090f92020-12-17 20:12:46 +01004682 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004683 } else {
4684 break;
4685 }
4686 }
4687
4688 return LY_SUCCESS;
4689}
4690
Radek Krejcid4557c62018-09-17 11:42:09 +02004691LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004692yang_parse_submodule(struct lysp_yang_ctx **context, struct ly_ctx *ly_ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004693 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004694{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004695 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004696 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004697 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004698 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004699 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004700 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004701
aPiecek56be92a2021-07-01 07:18:10 +02004702 assert(context && ly_ctx && main_ctx && in && submod);
4703
David Sedlák1b623122019-08-05 15:27:49 +02004704 /* create context */
4705 *context = calloc(1, sizeof **context);
4706 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004707 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004708 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004709 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004710
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004711 mod_p = calloc(1, sizeof *mod_p);
4712 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004713 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004714 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004715
4716 /* use main context parsed mods adding the current one */
4717 (*context)->parsed_mods = main_ctx->parsed_mods;
4718 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004719
Michal Vaskof8ebf132022-11-21 14:06:48 +01004720 LOG_LOCSET(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004721
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004722 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004723 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004724 LY_CHECK_GOTO(ret, cleanup);
4725
Michal Vasko7fbc8162018-09-17 10:35:16 +02004726 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004727 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004728 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004729
Radek Krejcid6b76452019-09-03 17:03:03 +02004730 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004731 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004732 ret = LY_EINVAL;
4733 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004734 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004735 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004736 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004737 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004738 }
4739
Michal Vasko7fbc8162018-09-17 10:35:16 +02004740 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004741 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004742 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004743
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004744 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004745 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004746 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004747 if (in->current[0]) {
4748 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004749 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004750 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004751 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004752
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004753 mod_p->parsing = 0;
4754 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004755
Radek Krejcibbe09a92018-11-08 09:36:54 +01004756cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004757 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004758 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004759 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004760 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004761 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004762 }
4763
4764 return ret;
4765}
4766
4767LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004768yang_parse_module(struct lysp_yang_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004769{
4770 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004771 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004772 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004773 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004774 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004775 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004776
David Sedlák1b623122019-08-05 15:27:49 +02004777 /* create context */
4778 *context = calloc(1, sizeof **context);
4779 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004780 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004781 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004782 (*context)->in = in;
Michal Vaskod0625d72022-10-06 15:02:50 +02004783 (*context)->main_ctx = (struct lysp_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004784
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004785 mod_p = calloc(1, sizeof *mod_p);
4786 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4787 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004788 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004789
Michal Vaskof8ebf132022-11-21 14:06:48 +01004790 LOG_LOCSET(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004791
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004792 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004793 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004794 LY_CHECK_GOTO(ret, cleanup);
4795
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004796 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004797 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004798 LY_CHECK_GOTO(ret, cleanup);
4799
Radek Krejcid6b76452019-09-03 17:03:03 +02004800 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004801 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 +01004802 ret = LY_EINVAL;
4803 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004804 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004805 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004806 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004807 goto cleanup;
4808 }
4809
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004810 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004811 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004812 LY_CHECK_GOTO(ret, cleanup);
4813
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004814 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004815 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004816 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004817 if (in->current[0]) {
4818 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004819 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004820 goto cleanup;
4821 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004822
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004823 mod->parsed = mod_p;
4824
4825cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004826 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004827 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004828 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004829 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004830 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004831 }
4832
Michal Vasko7fbc8162018-09-17 10:35:16 +02004833 return ret;
4834}