blob: 692064bd514bdc4ac31a3cae208d24d34ac218e7 [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 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
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"
Radek Krejci70853c52018-10-15 14:46:16 +020035#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020036
Radek Krejci77114102021-03-10 15:21:57 +010037struct lys_glob_unres;
38
Radek Krejciceaf2122019-01-02 15:03:26 +010039/**
40 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
41 * @param[in] CTX yang parser context to access libyang context.
42 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
43 * @param[out] TARGET variable where to store the pointer to the inserted value.
44 * @param[in] WORD string to store.
45 * @param[in] LEN length of the string in WORD to store.
46 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020047#define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \
Michal Vasko5d24f6c2020-10-13 13:49:06 +020048 if (BUF) {LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)));}\
49 else {LY_CHECK_RET(lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)));}
Radek Krejci44ceedc2018-10-02 15:54:31 +020050
Radek Krejciceaf2122019-01-02 15:03:26 +010051/**
Michal Vasko63f3d842020-07-08 10:10:14 +020052 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Radek Krejciceaf2122019-01-02 15:03:26 +010053 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010054 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
55 */
Radek Krejcid54412f2020-12-17 20:25:35 +010056#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020057
Michal Vaskoea5abea2018-09-18 13:10:54 +020058/**
aPieceka24a2252021-05-07 10:52:31 +020059 * @brief Loop through all substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +020060 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020061 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020062 * @param[out] KW YANG keyword read.
63 * @param[out] WORD Pointer to the keyword itself.
64 * @param[out] WORD_LEN Length of the keyword.
65 * @param[out] ERR Variable for error storing.
aPieceka24a2252021-05-07 10:52:31 +020066 * @param[in] SUC_CMD Command is applied if a semicolon is found, so no
67 * substatements are available. It is expected to contain a return or goto command.
68 * @param[in] ERR_CMD Command is applied if an error occurs before loop through
69 * substatements. It is expected to contain return or goto command.
Michal Vaskoea5abea2018-09-18 13:10:54 +020070 *
71 * @return In case there are no substatements or a fatal error encountered.
72 */
aPieceka24a2252021-05-07 10:52:31 +020073#define YANG_READ_SUBSTMT_FOR(CTX, KW, WORD, WORD_LEN, ERR, SUC_CMD, ERR_CMD) \
74 if ((ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
75 ERR_CMD; \
76 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020077 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
aPieceka24a2252021-05-07 10:52:31 +020078 SUC_CMD; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020079 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020080 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020081 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
aPieceka24a2252021-05-07 10:52:31 +020082 ERR = LY_EVALID; \
83 ERR_CMD; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020084 } \
Radek Krejci33090f92020-12-17 20:12:46 +010085 for (ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020086 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Radek Krejci33090f92020-12-17 20:12:46 +010087 ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020088
Radek Krejci33090f92020-12-17 20:12:46 +010089LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020090 struct lysp_node **siblings);
Radek Krejci33090f92020-12-17 20:12:46 +010091LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
92LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
93LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
94LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +010095LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020096
Michal Vaskoea5abea2018-09-18 13:10:54 +020097/**
98 * @brief Add another character to dynamic buffer, a low-level function.
99 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200100 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200101 *
Radek Krejci404251e2018-10-09 12:06:44 +0200102 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200103 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200104 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200105 * @param[in,out] buf Buffer to use, can be moved by realloc().
106 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200107 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200108 *
109 * @return LY_ERR values.
110 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200111LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200112buf_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 +0200113{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100114#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200115 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100116 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200117 *buf = ly_realloc(*buf, *buf_len);
118 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
119 }
Radek Krejcic0917392019-04-10 13:04:04 +0200120 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200121 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200122 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200123 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200124 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200125
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200127 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100128#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200129}
130
Michal Vaskoea5abea2018-09-18 13:10:54 +0200131/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
133 *
134 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200135 * @param[in] arg Type of the input string to select method of checking character validity.
136 * @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 +0200137 * 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 +0200138 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
139 * @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 +0200140 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200141 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200142 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
143 * 0 - colon not yet found (no prefix)
144 * 1 - \p c is the colon character
145 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200146 *
147 * @return LY_ERR values.
148 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200149LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100150buf_store_char(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200151 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200152{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200153 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200154 size_t len;
155
Radek Krejcif29b7c32019-04-09 16:17:49 +0200156 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
157 assert(!need_buf || (need_buf && word_b));
158
Radek Krejci44ceedc2018-10-02 15:54:31 +0200159 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100160 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
161 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
162 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200163 if (c == '\n') {
164 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100165 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200166 } else {
167 /* note - even the multibyte character is count as 1 */
168 ++ctx->indent;
169 }
170
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 /* check character validity */
172 switch (arg) {
173 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100174 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200175 break;
176 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100177 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200178 break;
179 case Y_STR_ARG:
180 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100181 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200182 break;
183 }
184
Michal Vasko7fbc8162018-09-17 10:35:16 +0200185 if (word_b && *word_b) {
186 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100187 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200188 return LY_EMEM;
189 }
190
191 /* in case of realloc */
192 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200193 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200194 /* first time we need a buffer, copy everything read up to now */
195 if (*word_len) {
196 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200197 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200198 *buf_len = *word_len;
199 memcpy(*word_b, *word_p, *word_len);
200 }
201
202 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100203 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200204 return LY_EMEM;
205 }
206
207 /* in case of realloc */
208 *word_p = *word_b;
209 } else {
210 /* just remember the first character pointer */
211 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100212 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200213 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200214 /* ... and update the word's length */
215 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100216 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200217 }
218
219 return LY_SUCCESS;
220}
221
Michal Vaskoea5abea2018-09-18 13:10:54 +0200222/**
223 * @brief Skip YANG comment in data.
224 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200225 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200226 * @param[in] comment Type of the comment to process:
227 * 1 for a one-line comment,
228 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200229 * @return LY_ERR values.
230 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200231LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100232skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200233{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100234 /* internal statuses: */
235#define COMMENT_NO 0 /* comment ended */
236#define COMMENT_LINE 1 /* in line comment */
237#define COMMENT_BLOCK 2 /* in block comment */
238#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
239
Radek Krejci33090f92020-12-17 20:12:46 +0100240 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200241 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100242 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100243 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100244 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100245 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200246 }
247 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100248 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100249 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100250 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100251 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100252 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200253 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200254 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100255 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100256 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100257 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100258 } else if (ctx->in->current[0] != '*') {
259 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100260 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200261 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100262 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200263 }
264 break;
265 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200266 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200267 }
268
Radek Krejci33090f92020-12-17 20:12:46 +0100269 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200270 ctx->indent = 0;
271 } else {
272 ++ctx->indent;
273 }
Radek Krejci33090f92020-12-17 20:12:46 +0100274 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200275 }
276
Radek Krejci33090f92020-12-17 20:12:46 +0100277 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200278 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200279 return LY_EVALID;
280 }
281
282 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100283
284#undef COMMENT_NO
285#undef COMMENT_LINE
286#undef COMMENT_BLOCK
287#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200288}
289
Michal Vaskoea5abea2018-09-18 13:10:54 +0200290/**
291 * @brief Read a quoted string from data.
292 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200293 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200294 * @param[in] arg Type of YANG keyword argument expected.
295 * @param[out] word_p Pointer to the read quoted string.
296 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
297 * set to NULL. Otherwise equal to \p word_p.
298 * @param[out] word_len Length of the read quoted string.
299 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
300 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
301 * indenation in the final quoted string.
302 *
303 * @return LY_ERR values.
304 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200305static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100306read_qstring(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200307 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200308{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100309 /* string parsing status: */
310#define STRING_ENDED 0 /* string ended */
311#define STRING_SINGLE_QUOTED 1 /* string with ' */
312#define STRING_DOUBLE_QUOTED 2 /* string with " */
313#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
314#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
315#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
316
Radek Krejci1deb5be2020-08-26 16:43:36 +0200317 uint8_t string;
318 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200319 ly_bool need_buf = 0;
320 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200322 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200323
Radek Krejci33090f92020-12-17 20:12:46 +0100324 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100325 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200326 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200327 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100328 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100329 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200330 }
Radek Krejci33090f92020-12-17 20:12:46 +0100331 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200332
Radek Krejci33090f92020-12-17 20:12:46 +0100333 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200334 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100335 case STRING_SINGLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100336 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200337 case '\'':
338 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100339 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100340 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200341 break;
342 default:
343 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100344 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 +0200345 break;
346 }
347 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100348 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100349 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200350 case '\"':
351 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100352 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100353 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200354 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200355 break;
356 case '\\':
357 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100358 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200359
360 /* the backslash sequence is substituted, so we will need a buffer to store the result */
361 need_buf = 1;
362
363 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100364 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200365
366 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
367 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
368 trailing_ws = 0;
369
370 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
371 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200372 break;
373 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200374 if (current_indent < block_indent) {
375 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100376 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200377 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100378 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100379 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 +0100380 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200381 }
382 break;
383 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200384 if (current_indent < block_indent) {
385 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100386 current_indent += Y_TAB_SPACES;
387 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200388 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200389 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100390 c = ctx->in->current;
391 ctx->in->current = " ";
392 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
393 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100394 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200395 }
Radek Krejci33090f92020-12-17 20:12:46 +0100396 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200397 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100398 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100399 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 +0100400 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200401 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100402 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200403 }
404 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200405 case '\r':
406 if (ctx->in->current[1] != '\n') {
407 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
408 return LY_EVALID;
409 }
410 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200411 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200412 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200413 /* we will be removing the indents so we need our own buffer */
414 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200415
416 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200417 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200418
Radek Krejciff13cd12019-10-25 15:34:24 +0200419 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200420 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200421 }
422
423 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100424 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 +0200425
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200426 /* reset context indentation counter for possible string after this one */
427 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200428 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 break;
430 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200431 /* first non-whitespace character, stop eating indentation */
432 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433
434 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100435 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 +0200436 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200437 break;
438 }
439 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100440 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100442 c = ctx->in->current;
443 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200444 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100445 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100446 /* fix false newline count in buf_store_char() */
447 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200448 break;
449 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100450 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200451 break;
452 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200453 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200454 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200455 break;
456 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200457 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100458 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200459 return LY_EVALID;
460 }
461
462 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100463 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 +0200464
Radek Krejcif13b87b2020-12-01 22:02:17 +0100465 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100466 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100468 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100469 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200470 case '+':
471 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100472 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200473 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200474 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200475 case '\r':
476 if (ctx->in->current[1] != '\n') {
477 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
478 return LY_EVALID;
479 }
480 MOVE_INPUT(ctx, 1);
481 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200482 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100483 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100484 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200485 case ' ':
486 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 /* just skip */
488 break;
489 default:
490 /* string is finished */
491 goto string_end;
492 }
Radek Krejci33090f92020-12-17 20:12:46 +0100493 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200494 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100495 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100496 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200497 case '\r':
498 if (ctx->in->current[1] != '\n') {
499 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
500 return LY_EVALID;
501 }
502 MOVE_INPUT(ctx, 1);
503 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200504 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100505 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100506 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200507 case ' ':
508 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200509 /* skip */
510 break;
511 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100512 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200513 break;
514 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100515 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200516 break;
517 default:
518 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200519 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200520 return LY_EVALID;
521 }
Radek Krejci33090f92020-12-17 20:12:46 +0100522 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200523 break;
524 default:
525 return LY_EINT;
526 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200527 }
528
529string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200530 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200531 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200532 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200533 return LY_EVALID;
534 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200535 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100536
537#undef STRING_ENDED
538#undef STRING_SINGLE_QUOTED
539#undef STRING_DOUBLE_QUOTED
540#undef STRING_DOUBLE_QUOTED_ESCAPED
541#undef STRING_PAUSED_NEXTSTRING
542#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200543}
544
Michal Vaskoea5abea2018-09-18 13:10:54 +0200545/**
546 * @brief Get another YANG string from the raw data.
547 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200548 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200549 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200550 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
551 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200552 * @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 +0200553 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
554 * set to NULL. Otherwise equal to \p word_p.
555 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200556 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200557 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200558LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100559get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200560 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200561{
Michal Vaskob68ea142021-04-26 13:46:00 +0200562 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200563 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200564 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200565
Michal Vasko7fbc8162018-09-17 10:35:16 +0200566 /* word buffer - dynamically allocated */
567 *word_b = NULL;
568
569 /* word pointer - just a pointer to data */
570 *word_p = NULL;
571
572 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100573 while (ctx->in->current[0]) {
574 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200575 case '\'':
576 case '\"':
577 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200578 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100579 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200580 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200581 ret = LY_EVALID;
582 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200583 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200584 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100585 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200586 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200587 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200588 if (!*word_p) {
589 /* do not return NULL word */
590 *word_p = "";
591 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200592 goto str_end;
593 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100594 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200595 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100596 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200597 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100598 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200599 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100600 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200601 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200602 } else {
603 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200604 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 +0200605 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200606 break;
607 case ' ':
608 if (*word_len) {
609 /* word is finished */
610 goto str_end;
611 }
Radek Krejci33090f92020-12-17 20:12:46 +0100612 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200613 break;
614 case '\t':
615 if (*word_len) {
616 /* word is finished */
617 goto str_end;
618 }
619 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100620 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200621
Radek Krejci33090f92020-12-17 20:12:46 +0100622 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200623 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200624 case '\r':
625 if (ctx->in->current[1] != '\n') {
626 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200627 ret = LY_EVALID;
628 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200629 }
630 MOVE_INPUT(ctx, 1);
631 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200632 case '\n':
633 if (*word_len) {
634 /* word is finished */
635 goto str_end;
636 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100637 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100638 MOVE_INPUT(ctx, 1);
639
Michal Vasko7fbc8162018-09-17 10:35:16 +0200640 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200641 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200642 break;
643 case ';':
644 case '{':
645 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
646 /* word is finished */
647 goto str_end;
648 }
649
Radek Krejci33090f92020-12-17 20:12:46 +0100650 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200651 ret = LY_EVALID;
652 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200653 case '}':
654 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100655 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200656 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200657 ret = LY_EVALID;
658 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200660 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 +0200661 break;
662 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200663 }
664
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200665 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200666 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200667 ret = LY_EVALID;
668 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200669
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200671 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200673 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200674 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200675 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200676 *word_p = *word_b;
677 }
678
679 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200680
681error:
682 free(*word_b);
683 *word_b = NULL;
684 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200685}
686
Michal Vaskoea5abea2018-09-18 13:10:54 +0200687/**
688 * @brief Get another YANG keyword from the raw data.
689 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200690 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200691 * @param[out] kw YANG keyword read.
692 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
693 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
694 *
695 * @return LY_ERR values.
696 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200697LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100698get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200700 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200701 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200702 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200703
704 if (word_p) {
705 *word_p = NULL;
706 *word_len = 0;
707 }
708
709 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100710 while (ctx->in->current[0]) {
711 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200712 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100713 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200714 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100715 MOVE_INPUT(ctx, 2);
716 LY_CHECK_RET(skip_comment(ctx, 1));
717 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200718 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100719 MOVE_INPUT(ctx, 2);
720 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200722 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200723 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200724 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200725 }
Radek Krejci13028282019-06-11 14:56:48 +0200726 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200727 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200728 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100729 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200730 ctx->indent = 0;
731 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200732 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200733 /* skip whitespaces (optsep) */
734 ++ctx->indent;
735 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100738 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200739 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100740 case '\r':
741 /* possible CRLF endline */
742 if (ctx->in->current[1] == '\n') {
743 break;
744 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100745 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200746 default:
747 /* either a keyword start or an invalid character */
748 goto keyword_start;
749 }
750
Radek Krejci33090f92020-12-17 20:12:46 +0100751 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 }
753
754keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100755 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100756 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200757
aPiecek93582ed2021-05-25 14:49:06 +0200758 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
759 goto success;
760 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
761 ctx->depth++;
762 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vaskoa3afbfe2021-12-07 14:04:47 +0100763 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200764 return LY_EINVAL;
765 }
766 goto success;
767 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
768 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200769 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200770 }
771
Radek Krejcid6b76452019-09-03 17:03:03 +0200772 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200773 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100774 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200775 case '\r':
776 if (ctx->in->current[1] != '\n') {
777 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
778 return LY_EVALID;
779 }
780 MOVE_INPUT(ctx, 1);
781 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200782 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200783 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200784 case ' ':
785 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200786 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200787 case ':':
788 /* keyword is not actually a keyword, but prefix of an extension.
789 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
790 * and we will be checking the keyword (extension instance) itself */
791 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100792 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200793 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200794 case '{':
795 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200796 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200797 break;
798 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100799 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200800 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100801 MOVE_INPUT(ctx, 1);
802 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200803 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200804 return LY_EVALID;
805 }
806 } else {
807 /* still can be an extension */
808 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200809
Radek Krejci156ccaf2018-10-15 15:49:17 +0200810extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100811 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200812 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
813 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200814 uint32_t c = 0;
815
Radek Krejci33090f92020-12-17 20:12:46 +0100816 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
817 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200818 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200819 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200820 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100821 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200822 }
Radek Krejci33090f92020-12-17 20:12:46 +0100823 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200824 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200825 return LY_EVALID;
826 }
827
828 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200829 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100830 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200831 return LY_EVALID;
832 }
833
Radek Krejcid6b76452019-09-03 17:03:03 +0200834 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200835 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200836
Radek Krejci626df482018-10-11 15:06:31 +0200837success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200838 if (word_p) {
839 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100840 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841 }
842
843 return LY_SUCCESS;
844}
845
Michal Vaskoea5abea2018-09-18 13:10:54 +0200846/**
847 * @brief Parse extension instance substatements.
848 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200849 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200850 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200851 * @param[in] word Extension instance substatement name (keyword).
852 * @param[in] word_len Extension instance substatement name length.
853 * @param[in,out] child Children of this extension instance to add to.
854 *
855 * @return LY_ERR values.
856 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200857static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100858parse_ext_substmt(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200859 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200860{
861 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100862 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200863 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200864 struct lysp_stmt *stmt, *par_child;
865
866 stmt = calloc(1, sizeof *stmt);
867 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
868
Radek Krejcibb9b1982019-04-08 14:24:59 +0200869 /* insert into parent statements */
870 if (!*child) {
871 *child = stmt;
872 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200873 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200874 par_child->next = stmt;
875 }
876
Michal Vaskofc2cd072021-02-24 13:17:17 +0100877 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200878 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200879
880 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100881 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200882 if (word) {
aPieceka24a2252021-05-07 10:52:31 +0200883 INSERT_WORD_RET(ctx, buf, stmt->arg, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200884 }
885
Radek Krejci8df109d2021-04-23 12:19:08 +0200886 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskoa3afbfe2021-12-07 14:04:47 +0100887 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100888 stmt->kw = kw;
889
aPieceka24a2252021-05-07 10:52:31 +0200890 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Radek Krejci33090f92020-12-17 20:12:46 +0100891 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200893 return ret;
894}
895
Michal Vaskoea5abea2018-09-18 13:10:54 +0200896/**
897 * @brief Parse extension instance.
898 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200899 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200900 * @param[in] ext_name Extension instance substatement name (keyword).
901 * @param[in] ext_name_len Extension instance substatement name length.
Radek Krejcifc596f92021-02-26 22:40:26 +0100902 * @param[in] insubstmt The statement this extension instance is a substatement of.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200903 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
904 * @param[in,out] exts Extension instances to add to.
905 *
906 * @return LY_ERR values.
907 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200908static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100909parse_ext(struct lys_yang_parser_ctx *ctx, const char *ext_name, size_t ext_name_len, enum ly_stmt insubstmt,
Radek Krejci0f969882020-08-21 16:56:47 +0200910 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200911{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100912 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200913 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200914 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200915 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200916 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200917
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200918 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200919
Michal Vaskofc2cd072021-02-24 13:17:17 +0100920 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
921 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
922 return LY_EVALID;
923 }
924
925 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200926 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927
928 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100929 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200930 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200931 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932 }
933
Michal Vaskofc2cd072021-02-24 13:17:17 +0100934 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200935 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200936 e->parsed = NULL;
Michal Vaskoa3afbfe2021-12-07 14:04:47 +0100937 e->prefix_data = PARSER_CUR_PMOD(ctx);
Radek Krejciab430862021-03-02 20:13:40 +0100938 e->parent_stmt = insubstmt;
939 e->parent_stmt_index = insubstmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100940
aPieceka24a2252021-05-07 10:52:31 +0200941 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Radek Krejci33090f92020-12-17 20:12:46 +0100942 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200943 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200944 return ret;
945}
946
Michal Vaskoea5abea2018-09-18 13:10:54 +0200947/**
948 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
949 * description, etc...
950 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200951 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200952 * @param[in] substmt Type of this substatement.
953 * @param[in] substmt_index Index of this substatement.
954 * @param[in,out] value Place to store the parsed value.
955 * @param[in] arg Type of the YANG keyword argument (of the value).
956 * @param[in,out] exts Extension instances to add to.
957 *
958 * @return LY_ERR values.
959 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100961parse_text_field(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200962 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200963{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100964 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200965 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200966 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200967 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968
969 if (*value) {
Radek Krejci3972b332021-03-02 16:34:31 +0100970 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200971 return LY_EVALID;
972 }
973
974 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100975 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200976
977 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200978 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200979
aPieceka24a2252021-05-07 10:52:31 +0200980 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200982 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100983 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 break;
985 default:
Radek Krejci3972b332021-03-02 16:34:31 +0100986 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987 return LY_EVALID;
988 }
989 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 return ret;
991}
992
Michal Vaskoea5abea2018-09-18 13:10:54 +0200993/**
994 * @brief Parse the yang-version statement.
995 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200996 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100997 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200998 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200999 *
1000 * @return LY_ERR values.
1001 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001002static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001003parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001004{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001005 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001007 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001008 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001010 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +02001011 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001012 return LY_EVALID;
1013 }
1014
1015 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001016 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001017
Radek Krejci96e48da2020-09-04 13:18:06 +02001018 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001019 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001020 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001021 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001022 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001023 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001024 free(buf);
1025 return LY_EVALID;
1026 }
1027 free(buf);
1028
aPieceka24a2252021-05-07 10:52:31 +02001029 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001030 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001031 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001032 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001033 break;
1034 default:
David Sedlákb3077192019-06-19 10:55:37 +02001035 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036 return LY_EVALID;
1037 }
1038 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039 return ret;
1040}
1041
Michal Vaskoea5abea2018-09-18 13:10:54 +02001042/**
1043 * @brief Parse the belongs-to statement.
1044 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001045 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001046 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1047 * @param[in,out] exts Extension instances to add to.
1048 *
1049 * @return LY_ERR values.
1050 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001052parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001054 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001056 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001057 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058
Michal Vaskoc3781c32020-10-06 14:04:08 +02001059 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001060 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 return LY_EVALID;
1062 }
1063
Michal Vaskoc3781c32020-10-06 14:04:08 +02001064 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001065 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01001066 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001067 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Submodule \"belongs-to\" value \"%.*s\" does not match its module name \"%s\".",
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01001068 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001069 free(buf);
1070 return LY_EVALID;
1071 }
1072 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001073
aPieceka24a2252021-05-07 10:52:31 +02001074 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001076 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001077 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001079 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001080 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_BELONGS_TO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081 break;
1082 default:
David Sedlákb3077192019-06-19 10:55:37 +02001083 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001084 return LY_EVALID;
1085 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001086 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001087 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001088checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 /* mandatory substatements */
1090 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001091 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001092 return LY_EVALID;
1093 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094 return ret;
1095}
1096
Michal Vaskoea5abea2018-09-18 13:10:54 +02001097/**
1098 * @brief Parse the revision-date statement.
1099 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001100 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001101 * @param[in,out] rev Array to store the parsed value in.
1102 * @param[in,out] exts Extension instances to add to.
1103 *
1104 * @return LY_ERR values.
1105 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001106static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001107parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001108{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001109 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001111 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001112 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113
1114 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001115 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116 return LY_EVALID;
1117 }
1118
1119 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001120 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001121
1122 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001123 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124 free(buf);
1125 return LY_EVALID;
1126 }
1127
1128 /* store value and spend buf if allocated */
1129 strncpy(rev, word, word_len);
1130 free(buf);
1131
aPieceka24a2252021-05-07 10:52:31 +02001132 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001133 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001134 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001135 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136 break;
1137 default:
David Sedlákb3077192019-06-19 10:55:37 +02001138 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139 return LY_EVALID;
1140 }
1141 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142 return ret;
1143}
1144
Michal Vaskoea5abea2018-09-18 13:10:54 +02001145/**
1146 * @brief Parse the include statement.
1147 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001148 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001149 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001150 * @param[in,out] includes Parsed includes to add to.
1151 *
1152 * @return LY_ERR values.
1153 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001154static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001155parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001156{
Radek Krejcid33273d2018-10-25 14:55:52 +02001157 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001159 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001160 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161 struct lysp_include *inc;
1162
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001163 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164
1165 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001166 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001167
Radek Krejci011e4aa2020-09-04 15:22:31 +02001168 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001169
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001170 /* submodules share the namespace with the module names, so there must not be
1171 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001172 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001173 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001174 return LY_EVALID;
1175 }
1176
aPieceka24a2252021-05-07 10:52:31 +02001177 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001179 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001180 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001181 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001183 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001184 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001185 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001187 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001188 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001190 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001191 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192 break;
1193 default:
David Sedlákb3077192019-06-19 10:55:37 +02001194 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195 return LY_EVALID;
1196 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 return ret;
1199}
1200
Michal Vaskoea5abea2018-09-18 13:10:54 +02001201/**
1202 * @brief Parse the import statement.
1203 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001204 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001205 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001206 * @param[in,out] imports Parsed imports to add to.
1207 *
1208 * @return LY_ERR values.
1209 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001210static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001211parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001213 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001215 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001216 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 struct lysp_import *imp;
1218
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001219 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220
1221 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001222 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001223 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001224
aPieceka24a2252021-05-07 10:52:31 +02001225 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001226 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001227 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001228 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskob36053d2020-03-26 15:49:30 +01001229 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001231 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001232 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001233 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001235 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001236 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001237 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001239 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001240 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001242 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001243 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001244 break;
1245 default:
David Sedlákb3077192019-06-19 10:55:37 +02001246 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001247 return LY_EVALID;
1248 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001250 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001251checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001253 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254
1255 return ret;
1256}
1257
Michal Vaskoea5abea2018-09-18 13:10:54 +02001258/**
1259 * @brief Parse the revision statement.
1260 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001261 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001262 * @param[in,out] revs Parsed revisions to add to.
1263 *
1264 * @return LY_ERR values.
1265 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001267parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001269 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001270 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001271 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001272 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001273 struct lysp_revision *rev;
1274
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001275 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276
1277 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001278 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001279
1280 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001281 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001282 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001283 return LY_EVALID;
1284 }
1285
Radek Krejcib7db73a2018-10-24 14:18:40 +02001286 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001287 free(buf);
1288
aPieceka24a2252021-05-07 10:52:31 +02001289 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001290 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001291 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001292 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001293 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001294 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001295 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001297 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001298 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001299 break;
1300 default:
David Sedlákb3077192019-06-19 10:55:37 +02001301 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001302 return LY_EVALID;
1303 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001304 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001305 return ret;
1306}
1307
Michal Vaskoea5abea2018-09-18 13:10:54 +02001308/**
1309 * @brief Parse a generic text field that can have more instances such as base.
1310 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001311 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001312 * @param[in] substmt Type of this substatement.
1313 * @param[in,out] texts Parsed values to add to.
1314 * @param[in] arg Type of the expected argument.
1315 * @param[in,out] exts Extension instances to add to.
1316 *
1317 * @return LY_ERR values.
1318 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001320parse_text_fields(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, const char ***texts, enum yang_arg arg,
Radek Krejci0f969882020-08-21 16:56:47 +02001321 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001322{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001323 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001325 const char **item;
1326 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001327 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001328
1329 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001330 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331
1332 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001333 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334
Radek Krejci011e4aa2020-09-04 15:22:31 +02001335 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
aPieceka24a2252021-05-07 10:52:31 +02001336 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001338 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001339 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001340 break;
1341 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001342 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001343 return LY_EVALID;
1344 }
1345 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346 return ret;
1347}
1348
Michal Vaskoea5abea2018-09-18 13:10:54 +02001349/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001350 * @brief Parse a generic text field that can have more instances such as base.
1351 *
1352 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001353 * @param[in] substmt Type of this substatement.
1354 * @param[in,out] qnames Parsed qnames to add to.
1355 * @param[in] arg Type of the expected argument.
1356 * @param[in,out] exts Extension instances to add to.
1357 *
1358 * @return LY_ERR values.
1359 */
1360static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001361parse_qnames(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001362 enum yang_arg arg, struct lysp_ext_instance **exts)
1363{
1364 LY_ERR ret = LY_SUCCESS;
1365 char *buf, *word;
1366 struct lysp_qname *item;
1367 size_t word_len;
1368 enum ly_stmt kw;
1369
1370 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001371 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001372
1373 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001374 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001375
1376 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01001377 item->mod = PARSER_CUR_PMOD(ctx);
aPieceka24a2252021-05-07 10:52:31 +02001378 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001379 switch (kw) {
1380 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001381 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001382 break;
1383 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001384 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001385 return LY_EVALID;
1386 }
1387 }
1388 return ret;
1389}
1390
1391/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001392 * @brief Parse the config statement.
1393 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001394 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001395 * @param[in,out] flags Flags to add to.
1396 * @param[in,out] exts Extension instances to add to.
1397 *
1398 * @return LY_ERR values.
1399 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001400static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001401parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001402{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001403 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001404 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001405 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001406 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001407
1408 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001409 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001410 return LY_EVALID;
1411 }
1412
1413 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001414 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415
Radek Krejcif13b87b2020-12-01 22:02:17 +01001416 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001418 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001419 *flags |= LYS_CONFIG_R;
1420 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001421 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001422 free(buf);
1423 return LY_EVALID;
1424 }
1425 free(buf);
1426
aPieceka24a2252021-05-07 10:52:31 +02001427 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001428 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001429 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001430 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431 break;
1432 default:
David Sedlákb3077192019-06-19 10:55:37 +02001433 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434 return LY_EVALID;
1435 }
1436 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437 return ret;
1438}
1439
Michal Vaskoea5abea2018-09-18 13:10:54 +02001440/**
1441 * @brief Parse the mandatory statement.
1442 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001443 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001444 * @param[in,out] flags Flags to add to.
1445 * @param[in,out] exts Extension instances to add to.
1446 *
1447 * @return LY_ERR values.
1448 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001450parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001451{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001452 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001453 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001454 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001455 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001456
1457 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001458 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001459 return LY_EVALID;
1460 }
1461
1462 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001463 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001464
Radek Krejcif13b87b2020-12-01 22:02:17 +01001465 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001467 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468 *flags |= LYS_MAND_FALSE;
1469 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001470 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001471 free(buf);
1472 return LY_EVALID;
1473 }
1474 free(buf);
1475
aPieceka24a2252021-05-07 10:52:31 +02001476 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001477 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001478 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001479 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001480 break;
1481 default:
David Sedlákb3077192019-06-19 10:55:37 +02001482 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483 return LY_EVALID;
1484 }
1485 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486 return ret;
1487}
1488
Michal Vaskoea5abea2018-09-18 13:10:54 +02001489/**
1490 * @brief Parse a restriction such as range or length.
1491 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001492 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001493 * @param[in] restr_kw Type of this particular restriction.
1494 * @param[in,out] exts Extension instances to add to.
1495 *
1496 * @return LY_ERR values.
1497 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001499parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001501 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001502 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001503 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001504 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505
1506 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001507 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508
Michal Vaskob36053d2020-03-26 15:49:30 +01001509 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001510 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01001511 restr->arg.mod = PARSER_CUR_PMOD(ctx);
aPieceka24a2252021-05-07 10:52:31 +02001512 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001514 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001515 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001517 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001518 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001520 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01001521 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001523 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001524 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001526 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001527 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001528 break;
1529 default:
David Sedlákb3077192019-06-19 10:55:37 +02001530 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531 return LY_EVALID;
1532 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001533 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001534 return ret;
1535}
1536
Michal Vaskoea5abea2018-09-18 13:10:54 +02001537/**
1538 * @brief Parse a restriction that can have more instances such as must.
1539 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001540 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001541 * @param[in] restr_kw Type of this particular restriction.
1542 * @param[in,out] restrs Restrictions to add to.
1543 *
1544 * @return LY_ERR values.
1545 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001547parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001548{
1549 struct lysp_restr *restr;
1550
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001551 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001552 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553}
1554
Michal Vaskoea5abea2018-09-18 13:10:54 +02001555/**
1556 * @brief Parse the status statement.
1557 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001558 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001559 * @param[in,out] flags Flags to add to.
1560 * @param[in,out] exts Extension instances to add to.
1561 *
1562 * @return LY_ERR values.
1563 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001565parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001566{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001567 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001569 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001570 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571
1572 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001573 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 return LY_EVALID;
1575 }
1576
1577 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001578 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001579
Radek Krejcif13b87b2020-12-01 22:02:17 +01001580 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001581 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001582 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001583 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001584 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001585 *flags |= LYS_STATUS_OBSLT;
1586 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001587 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588 free(buf);
1589 return LY_EVALID;
1590 }
1591 free(buf);
1592
aPieceka24a2252021-05-07 10:52:31 +02001593 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001594 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001595 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001596 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001597 break;
1598 default:
David Sedlákb3077192019-06-19 10:55:37 +02001599 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600 return LY_EVALID;
1601 }
1602 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603 return ret;
1604}
1605
Michal Vaskoea5abea2018-09-18 13:10:54 +02001606/**
1607 * @brief Parse the when 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] when_p When pointer to parse to.
1611 *
1612 * @return LY_ERR values.
1613 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001614LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001615parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
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 struct lysp_when *when;
1622
1623 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001624 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 return LY_EVALID;
1626 }
1627
1628 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001629 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001630 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631
1632 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001633 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001634 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001635 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636
aPieceka24a2252021-05-07 10:52:31 +02001637 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001639 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001640 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001642 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001643 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001645 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001646 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_WHEN, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647 break;
1648 default:
David Sedlákb3077192019-06-19 10:55:37 +02001649 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001650 return LY_EVALID;
1651 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001652 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653 return ret;
1654}
1655
Michal Vaskoea5abea2018-09-18 13:10:54 +02001656/**
1657 * @brief Parse the anydata or anyxml statement.
1658 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001659 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001660 * @param[in] any_kw Type of this particular keyword.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001661 * @param[in,out] siblings Siblings to add to.
1662 *
1663 * @return LY_ERR values.
1664 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001665LY_ERR
Radek Krejci39b7fc22021-02-26 23:29:18 +01001666parse_any(struct lys_yang_parser_ctx *ctx, enum ly_stmt any_kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001668 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001669 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001670 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001672 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673
David Sedlák60adc092019-08-06 15:57:02 +02001674 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001675 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001676
Radek Krejci39b7fc22021-02-26 23:29:18 +01001677 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001678 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679
Michal Vasko7fbc8162018-09-17 10:35:16 +02001680 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001681 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001682 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001683
1684 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02001685 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001687 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001688 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001689 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001690 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001691 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001692 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001693 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001694 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001695 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001696 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001697 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001698 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001699 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001700 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001701 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001702 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001703 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001704 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001705 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001706 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001708 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001709 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001711 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001712 LY_CHECK_RET(parse_ext(ctx, word, word_len, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 break;
1714 default:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001715 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001716 return LY_EVALID;
1717 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001719 return ret;
1720}
1721
Michal Vaskoea5abea2018-09-18 13:10:54 +02001722/**
1723 * @brief Parse the value or position statement. Substatement of type enum statement.
1724 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001725 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001726 * @param[in] val_kw Type of this particular keyword.
1727 * @param[in,out] value Value to write to.
1728 * @param[in,out] flags Flags to write to.
1729 * @param[in,out] exts Extension instances to add to.
1730 *
1731 * @return LY_ERR values.
1732 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001733LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001734parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, enum ly_stmt val_kw, int64_t *value, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001735 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001737 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001739 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001740 long long int num = 0;
1741 unsigned long long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001742 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001743
1744 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001745 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001746 return LY_EVALID;
1747 }
1748 *flags |= LYS_SET_VALUE;
1749
1750 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001751 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001752
Radek Krejcid6b76452019-09-03 17:03:03 +02001753 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) {
David Sedlákb3077192019-06-19 10:55:37 +02001754 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001755 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756 }
1757
1758 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001759 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001760 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001761 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001762 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001763 goto error;
1764 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001766 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001767 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001768 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001769 goto error;
1770 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771 }
1772 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001773 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001774 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001775 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001776 }
1777 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001778 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001779 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001780 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001781 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001782 *value = num;
1783 } else {
1784 *value = unum;
1785 }
1786 free(buf);
1787
aPieceka24a2252021-05-07 10:52:31 +02001788 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001789 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001790 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001791 LY_CHECK_RET(parse_ext(ctx, word, word_len, val_kw == LY_STMT_VALUE ? LY_STMT_VALUE : LY_STMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001792 break;
1793 default:
David Sedlákb3077192019-06-19 10:55:37 +02001794 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001795 return LY_EVALID;
1796 }
1797 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001798 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001799
1800error:
1801 free(buf);
1802 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001803}
1804
Michal Vaskoea5abea2018-09-18 13:10:54 +02001805/**
1806 * @brief Parse the enum or bit statement. Substatement of type statement.
1807 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001808 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001809 * @param[in] enum_kw Type of this particular keyword.
1810 * @param[in,out] enums Enums or bits to add to.
1811 *
1812 * @return LY_ERR values.
1813 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001815parse_type_enum(struct lys_yang_parser_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001817 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001818 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001819 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001820 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821 struct lysp_type_enum *enm;
1822
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001823 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824
1825 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001826 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 +02001827 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001828 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001829 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001830 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001831
Radek Krejci011e4aa2020-09-04 15:22:31 +02001832 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001833 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1834
aPieceka24a2252021-05-07 10:52:31 +02001835 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001836 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001837 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001838 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001839 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001840 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001841 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001842 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001844 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001845 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001847 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001848 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001849 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001850 case LY_STMT_VALUE:
1851 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Michal Vasko69730152020-10-09 16:30:07 +02001852 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001853 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001854 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001855 case LY_STMT_POSITION:
1856 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Michal Vasko69730152020-10-09 16:30:07 +02001857 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001858 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001859 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001860 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001861 LY_CHECK_RET(parse_ext(ctx, word, word_len, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862 break;
1863 default:
David Sedlákb3077192019-06-19 10:55:37 +02001864 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001865 return LY_EVALID;
1866 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868 return ret;
1869}
1870
Michal Vaskoea5abea2018-09-18 13:10:54 +02001871/**
1872 * @brief Parse the fraction-digits statement. Substatement of type statement.
1873 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001874 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001875 * @param[in,out] fracdig Value to write to.
1876 * @param[in,out] exts Extension instances to add to.
1877 *
1878 * @return LY_ERR values.
1879 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001880static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001881parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001882{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001883 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001884 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001885 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001886 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001887 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888
1889 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001890 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891 return LY_EVALID;
1892 }
1893
1894 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001895 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896
1897 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001898 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 free(buf);
1900 return LY_EVALID;
1901 }
1902
1903 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001904 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001905 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001906 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001907 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908 free(buf);
1909 return LY_EVALID;
1910 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001911 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001912 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001913 free(buf);
1914 return LY_EVALID;
1915 }
1916 *fracdig = num;
1917 free(buf);
1918
aPieceka24a2252021-05-07 10:52:31 +02001919 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001920 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001921 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001922 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FRACTION_DIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001923 break;
1924 default:
David Sedlákb3077192019-06-19 10:55:37 +02001925 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926 return LY_EVALID;
1927 }
1928 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929 return ret;
1930}
1931
Michal Vaskoea5abea2018-09-18 13:10:54 +02001932/**
1933 * @brief Parse the require-instance statement. Substatement of type statement.
1934 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001935 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001936 * @param[in,out] reqinst Value to write to.
1937 * @param[in,out] flags Flags to write to.
1938 * @param[in,out] exts Extension instances to add to.
1939 *
1940 * @return LY_ERR values.
1941 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001942static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001943parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001944 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001945{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001946 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001947 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001948 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001949 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950
1951 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001952 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953 return LY_EVALID;
1954 }
1955 *flags |= LYS_SET_REQINST;
1956
1957 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001958 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001959
Radek Krejcif13b87b2020-12-01 22:02:17 +01001960 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001961 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001962 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001963 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001964 free(buf);
1965 return LY_EVALID;
1966 }
1967 free(buf);
1968
aPieceka24a2252021-05-07 10:52:31 +02001969 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001970 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001971 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001972 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REQUIRE_INSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973 break;
1974 default:
David Sedlákb3077192019-06-19 10:55:37 +02001975 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976 return LY_EVALID;
1977 }
1978 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979 return ret;
1980}
1981
Michal Vaskoea5abea2018-09-18 13:10:54 +02001982/**
1983 * @brief Parse the modifier statement. Substatement of type pattern statement.
1984 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001985 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001986 * @param[in,out] pat Value to write to.
1987 * @param[in,out] exts Extension instances to add to.
1988 *
1989 * @return LY_ERR values.
1990 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001992parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001993{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001994 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001996 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001997 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001998
Radek Krejcif13b87b2020-12-01 22:02:17 +01001999 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002000 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001 return LY_EVALID;
2002 }
2003
2004 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002005 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006
Radek Krejcif13b87b2020-12-01 22:02:17 +01002007 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002008 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002009 free(buf);
2010 return LY_EVALID;
2011 }
2012 free(buf);
2013
2014 /* replace the value in the dictionary */
2015 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002016 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002018 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019
Radek Krejcif13b87b2020-12-01 22:02:17 +01002020 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2021 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002022 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023
aPieceka24a2252021-05-07 10:52:31 +02002024 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002026 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002027 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028 break;
2029 default:
David Sedlákb3077192019-06-19 10:55:37 +02002030 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002031 return LY_EVALID;
2032 }
2033 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034 return ret;
2035}
2036
Michal Vaskoea5abea2018-09-18 13:10:54 +02002037/**
2038 * @brief Parse the pattern statement. Substatement of type statement.
2039 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002040 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002041 * @param[in,out] patterns Restrictions to add to.
2042 *
2043 * @return LY_ERR values.
2044 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002046parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002048 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002050 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002051 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 struct lysp_restr *restr;
2053
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002054 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055
2056 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002057 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058
2059 /* add special meaning first byte */
2060 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002061 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 word = buf;
2063 } else {
2064 buf = malloc(word_len + 2);
2065 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002066 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002067 if (word_len) {
2068 memmove(buf + 1, word, word_len);
2069 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002070 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002071 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002072 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01002073 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074
aPieceka24a2252021-05-07 10:52:31 +02002075 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002077 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002078 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002079 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002080 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002081 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002083 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01002084 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002085 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002086 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002087 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002089 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002090 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002091 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002093 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002094 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095 break;
2096 default:
David Sedlákb3077192019-06-19 10:55:37 +02002097 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098 return LY_EVALID;
2099 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002101 return ret;
2102}
2103
Michal Vaskoea5abea2018-09-18 13:10:54 +02002104/**
2105 * @brief Parse the type statement.
2106 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002107 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002108 * @param[in,out] type Type to wrote to.
2109 *
2110 * @return LY_ERR values.
2111 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002113parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002115 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002117 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002118 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002119 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 struct lysp_type *nest_type;
2121
2122 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002123 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002124 return LY_EVALID;
2125 }
2126
2127 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002128 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002129 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002131 /* set module */
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01002132 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002133
aPieceka24a2252021-05-07 10:52:31 +02002134 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002136 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002137 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 +01002138 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002140 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002141 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002142 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002144 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002145 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002146 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002148 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002149 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002150 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002152 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002154 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002155 return LY_EVALID;
2156 }
2157 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002158 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159
Radek Krejci33090f92020-12-17 20:12:46 +01002160 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002161 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002163 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002164 if (type->path) {
Radek Krejci3972b332021-03-02 16:34:31 +01002165 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002166 return LY_EVALID;
2167 }
2168
aPiecek4bb1e372021-05-07 11:01:00 +02002169 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2170 * corresponding structure, so in case of failure, the lysp_module_free function will take
2171 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2172 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2173 */
2174 LY_CHECK_ERR_RET(ret = parse_text_field(ctx, LY_STMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts),
2175 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002176 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002177 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002178 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002179 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002180 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002181 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002183 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002184 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002185 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002187 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002189 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002190 return LY_EVALID;
2191 }
2192 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002193 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194
Radek Krejci33090f92020-12-17 20:12:46 +01002195 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002196 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002198 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002199 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002200 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002202 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002203 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002204 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002205 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002206 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002207 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002208 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209 break;
2210 default:
David Sedlákb3077192019-06-19 10:55:37 +02002211 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 return LY_EVALID;
2213 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002215 return ret;
2216}
2217
Michal Vaskoea5abea2018-09-18 13:10:54 +02002218/**
2219 * @brief Parse the leaf statement.
2220 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002221 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002222 * @param[in,out] siblings Siblings to add to.
2223 *
2224 * @return LY_ERR values.
2225 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002226LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002227parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002229 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002230 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002231 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002232 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233 struct lysp_node_leaf *leaf;
2234
David Sedlák60adc092019-08-06 15:57:02 +02002235 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002236 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002238 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002239
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002241 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002242 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002243
2244 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002245 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002246 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002247 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002248 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002249 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002250 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002251 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01002252 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002254 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002255 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002257 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002258 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002259 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002260 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002261 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002262 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002263 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002264 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002266 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002267 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002269 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002270 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002272 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002273 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002274 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002275 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002276 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002278 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002279 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002280 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002281 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002282 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 break;
2284 default:
David Sedlákb3077192019-06-19 10:55:37 +02002285 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 return LY_EVALID;
2287 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002289 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002290checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002291 /* mandatory substatements */
2292 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002293 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002294 return LY_EVALID;
2295 }
2296
2297 return ret;
2298}
2299
Michal Vaskoea5abea2018-09-18 13:10:54 +02002300/**
2301 * @brief Parse the max-elements 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] max Value to write to.
2305 * @param[in,out] flags Flags to write to.
2306 * @param[in,out] exts Extension instances to add to.
2307 *
2308 * @return LY_ERR values.
2309 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002310LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002311parse_maxelements(struct lys_yang_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002313 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002315 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002316 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002317 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002318
2319 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002320 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002321 return LY_EVALID;
2322 }
2323 *flags |= LYS_SET_MAX;
2324
2325 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002326 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002327
2328 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002329 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 free(buf);
2331 return LY_EVALID;
2332 }
2333
Radek Krejci7f9b6512019-09-18 13:11:09 +02002334 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002335 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002336 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002338 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002339 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 free(buf);
2341 return LY_EVALID;
2342 }
2343 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002344 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002345 free(buf);
2346 return LY_EVALID;
2347 }
2348
2349 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002350 } else {
2351 /* unbounded */
2352 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002353 }
2354 free(buf);
2355
aPieceka24a2252021-05-07 10:52:31 +02002356 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002358 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002359 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MAX_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 break;
2361 default:
David Sedlákb3077192019-06-19 10:55:37 +02002362 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363 return LY_EVALID;
2364 }
2365 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 return ret;
2367}
2368
Michal Vaskoea5abea2018-09-18 13:10:54 +02002369/**
2370 * @brief Parse the min-elements statement.
2371 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002372 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002373 * @param[in,out] min Value to write to.
2374 * @param[in,out] flags Flags to write to.
2375 * @param[in,out] exts Extension instances to add to.
2376 *
2377 * @return LY_ERR values.
2378 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002379LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002380parse_minelements(struct lys_yang_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002381{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002382 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002384 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002385 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002386 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002387
2388 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002389 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 return LY_EVALID;
2391 }
2392 *flags |= LYS_SET_MIN;
2393
2394 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002395 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396
2397 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002398 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399 free(buf);
2400 return LY_EVALID;
2401 }
2402
2403 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002404 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002405 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002406 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002407 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408 free(buf);
2409 return LY_EVALID;
2410 }
2411 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002412 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413 free(buf);
2414 return LY_EVALID;
2415 }
2416 *min = num;
2417 free(buf);
2418
aPieceka24a2252021-05-07 10:52:31 +02002419 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002421 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002422 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MIN_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423 break;
2424 default:
David Sedlákb3077192019-06-19 10:55:37 +02002425 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426 return LY_EVALID;
2427 }
2428 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002429 return ret;
2430}
2431
Michal Vaskoea5abea2018-09-18 13:10:54 +02002432/**
2433 * @brief Parse the ordered-by statement.
2434 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002435 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002436 * @param[in,out] flags Flags to write to.
2437 * @param[in,out] exts Extension instances to add to.
2438 *
2439 * @return LY_ERR values.
2440 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002441static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002442parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002444 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002446 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002447 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448
2449 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002450 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002451 return LY_EVALID;
2452 }
2453
2454 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002455 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002456
Radek Krejcif13b87b2020-12-01 22:02:17 +01002457 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002459 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460 *flags |= LYS_ORDBY_USER;
2461 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002462 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 free(buf);
2464 return LY_EVALID;
2465 }
2466 free(buf);
2467
aPieceka24a2252021-05-07 10:52:31 +02002468 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002470 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002471 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ORDERED_BY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002472 break;
2473 default:
David Sedlákb3077192019-06-19 10:55:37 +02002474 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 return LY_EVALID;
2476 }
2477 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 return ret;
2479}
2480
Michal Vaskoea5abea2018-09-18 13:10:54 +02002481/**
2482 * @brief Parse the leaf-list statement.
2483 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002484 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002485 * @param[in,out] siblings Siblings to add to.
2486 *
2487 * @return LY_ERR values.
2488 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002489LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002490parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002491{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002492 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002494 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002495 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002496 struct lysp_node_leaflist *llist;
2497
David Sedlák60adc092019-08-06 15:57:02 +02002498 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002499 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002501 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002504 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002505 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002506
2507 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002508 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002509 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002510 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002511 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002512 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002513 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002514 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002515 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002517 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002518 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002520 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002521 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002523 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002524 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002526 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002527 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002529 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002530 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002532 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002533 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002535 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002536 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002538 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002539 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002541 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002542 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002544 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002545 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002547 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002548 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002549 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002550 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002551 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 break;
2553 default:
David Sedlákb3077192019-06-19 10:55:37 +02002554 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 return LY_EVALID;
2556 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002558 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002559checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 /* mandatory substatements */
2561 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002562 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002563 return LY_EVALID;
2564 }
2565
2566 return ret;
2567}
2568
Michal Vaskoea5abea2018-09-18 13:10:54 +02002569/**
2570 * @brief Parse the refine statement.
2571 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002572 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002573 * @param[in,out] refines Refines to add to.
2574 *
2575 * @return LY_ERR values.
2576 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002578parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002579{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002580 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002582 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002583 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 struct lysp_refine *rf;
2585
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002586 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587
2588 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002589 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002590 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002591 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002592
aPieceka24a2252021-05-07 10:52:31 +02002593 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002595 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002596 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002598 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002599 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002601 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002602 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002604 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002605 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002606 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002608 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002609 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002611 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002612 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002614 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002615 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002617 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002618 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002619 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002620 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002621 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002623 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002624 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002625 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002626 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002627 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002628 break;
2629 default:
David Sedlákb3077192019-06-19 10:55:37 +02002630 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002631 return LY_EVALID;
2632 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002634 return ret;
2635}
2636
Michal Vaskoea5abea2018-09-18 13:10:54 +02002637/**
2638 * @brief Parse the typedef statement.
2639 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002640 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002641 * @param[in,out] typedefs Typedefs to add to.
2642 *
2643 * @return LY_ERR values.
2644 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002646parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002648 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002650 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002651 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 struct lysp_tpdf *tpdf;
2653
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002654 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655
2656 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002657 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002658 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002659
2660 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002661 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002663 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002664 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01002665 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002667 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002668 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002670 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002671 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002673 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002674 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002676 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002677 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002679 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002680 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002682 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002683 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 break;
2685 default:
David Sedlákb3077192019-06-19 10:55:37 +02002686 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687 return LY_EVALID;
2688 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002690 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002691checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 /* mandatory substatements */
2693 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002694 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695 return LY_EVALID;
2696 }
2697
Radek Krejcibbe09a92018-11-08 09:36:54 +01002698 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002699 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002700 assert(ctx->main_ctx);
2701 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002702 }
2703
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704 return ret;
2705}
2706
Michal Vaskoea5abea2018-09-18 13:10:54 +02002707/**
2708 * @brief Parse the input or output statement.
2709 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002710 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002711 * @param[in] kw Type of this particular keyword
2712 * @param[in,out] inout_p Input/output pointer to write to.
2713 *
2714 * @return LY_ERR values.
2715 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002716static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002717parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002718 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002719{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002720 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002722 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002723 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002724 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002725
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002726 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002727 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728 return LY_EVALID;
2729 }
2730
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002731 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002732 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2733 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002734 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735
2736 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002737 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002739 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002740 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002741 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002742 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002743 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002744 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002745 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002746 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002747 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002748 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002749 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002750 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002751 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002752 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002754 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002755 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002757 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002758 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002760 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002761 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002762 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002763 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002764 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002766 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002767 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002768 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002770 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002771 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002773 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002774 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 break;
2776 default:
David Sedlákb3077192019-06-19 10:55:37 +02002777 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 return LY_EVALID;
2779 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002780 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002781 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002782
Radek Krejci7fc68292019-06-12 13:51:09 +02002783checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002784 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002785 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2786 return LY_EVALID;
2787 }
2788
Michal Vasko7fbc8162018-09-17 10:35:16 +02002789 return ret;
2790}
2791
Michal Vaskoea5abea2018-09-18 13:10:54 +02002792/**
2793 * @brief Parse the action statement.
2794 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002795 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002796 * @param[in,out] actions Actions to add to.
2797 *
2798 * @return LY_ERR values.
2799 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002800LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002801parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002803 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002804 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002805 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002806 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002807 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808
Radek Krejci2a9fc652021-01-22 17:44:34 +01002809 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002810
2811 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002812 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002813 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002814 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002815 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002816
aPieceka24a2252021-05-07 10:52:31 +02002817 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002819 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002820 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002821 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002822 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002823 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002824 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002825 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002826 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002828 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002829 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830 break;
2831
Radek Krejcid6b76452019-09-03 17:03:03 +02002832 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002833 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002835 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002836 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002837 break;
2838
Radek Krejcid6b76452019-09-03 17:03:03 +02002839 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002840 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002841 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002842 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002843 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002845 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002846 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent ? LY_STMT_ACTION : LY_STMT_RPC, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002847 break;
2848 default:
David Sedlákb3077192019-06-19 10:55:37 +02002849 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002850 return LY_EVALID;
2851 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002852 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002853 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002854
Radek Krejci2a9fc652021-01-22 17:44:34 +01002855checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002856 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2857 if (!act->input.nodetype) {
2858 act->input.nodetype = LYS_INPUT;
2859 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002860 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002861 }
2862 if (!act->output.nodetype) {
2863 act->output.nodetype = LYS_OUTPUT;
2864 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002865 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002866 }
2867
Michal Vasko7fbc8162018-09-17 10:35:16 +02002868 return ret;
2869}
2870
Michal Vaskoea5abea2018-09-18 13:10:54 +02002871/**
2872 * @brief Parse the notification statement.
2873 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002874 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002875 * @param[in,out] notifs Notifications to add to.
2876 *
2877 * @return LY_ERR values.
2878 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002879LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002880parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002882 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002884 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002885 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002886 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002887
Radek Krejci2a9fc652021-01-22 17:44:34 +01002888 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889
2890 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002891 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002892 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002893 notif->nodetype = LYS_NOTIF;
2894 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895
aPieceka24a2252021-05-07 10:52:31 +02002896 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002897 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002898 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002899 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002900 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002901 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002902 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002903 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002904 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002905 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002906 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002907 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002908 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 break;
2910
Radek Krejcid6b76452019-09-03 17:03:03 +02002911 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002912 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002913 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002914 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002915 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002917 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002918 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002920 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002921 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002922 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002923 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002924 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002926 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002927 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002929 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002930 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002932 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002933 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002934 break;
2935
Radek Krejcid6b76452019-09-03 17:03:03 +02002936 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002937 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002938 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002940 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002941 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002942 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002943 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002944 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002945 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002946 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002947 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948 break;
2949 default:
David Sedlákb3077192019-06-19 10:55:37 +02002950 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002951 return LY_EVALID;
2952 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002953 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002954
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955 return ret;
2956}
2957
Michal Vaskoea5abea2018-09-18 13:10:54 +02002958/**
2959 * @brief Parse the grouping statement.
2960 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002961 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002962 * @param[in,out] groupings Groupings to add to.
2963 *
2964 * @return LY_ERR values.
2965 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002966LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002967parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002969 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002971 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002972 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002973 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974
Radek Krejci2a9fc652021-01-22 17:44:34 +01002975 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976
2977 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002978 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002979 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002980 grp->nodetype = LYS_GROUPING;
2981 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982
aPiecek63e080d2021-06-29 13:53:28 +02002983 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002984 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002985 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002986 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002988 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002989 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002991 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002992 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002993 break;
2994
Radek Krejcid6b76452019-09-03 17:03:03 +02002995 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002996 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002997 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002998 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002999 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003001 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003002 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003004 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003005 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003007 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003008 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003010 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003011 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003013 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003014 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003016 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003017 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 break;
3019
Radek Krejcid6b76452019-09-03 17:03:03 +02003020 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003021 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003022 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003023 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003024 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003025 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003027 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003028 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003029 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003030 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003031 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003032 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003034 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003035 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003036 break;
3037 default:
David Sedlákb3077192019-06-19 10:55:37 +02003038 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003039 return LY_EVALID;
3040 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003041 }
aPiecek63e080d2021-06-29 13:53:28 +02003042 LY_CHECK_RET(ret);
3043checks:
3044 /* store data for collision check */
3045 if (parent) {
3046 assert(ctx->main_ctx);
3047 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3048 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003049
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 return ret;
3051}
3052
Michal Vaskoea5abea2018-09-18 13:10:54 +02003053/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003054 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003055 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003056 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003057 * @param[in,out] augments Augments to add to.
3058 *
3059 * @return LY_ERR values.
3060 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003061LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003062parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003064 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003066 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003067 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003068 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069
Radek Krejci2a9fc652021-01-22 17:44:34 +01003070 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071
3072 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003073 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003074 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003075 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003076 aug->nodetype = LYS_AUGMENT;
3077 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078
aPieceka24a2252021-05-07 10:52:31 +02003079 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003080 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003081 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003082 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003084 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003085 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003087 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003088 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003090 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003091 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003093 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003094 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 break;
3096
Radek Krejcid6b76452019-09-03 17:03:03 +02003097 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003098 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003099 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003100 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003101 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003103 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003104 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003106 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003107 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003109 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003110 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003112 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003113 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003115 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003116 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003118 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003119 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003121 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003122 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123 break;
3124
Radek Krejcid6b76452019-09-03 17:03:03 +02003125 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003126 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003127 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003129 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003130 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003131 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003133 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003134 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 break;
3136 default:
David Sedlákb3077192019-06-19 10:55:37 +02003137 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003138 return LY_EVALID;
3139 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003141
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 return ret;
3143}
3144
Michal Vaskoea5abea2018-09-18 13:10:54 +02003145/**
3146 * @brief Parse the uses statement.
3147 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003148 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003149 * @param[in,out] siblings Siblings to add to.
3150 *
3151 * @return LY_ERR values.
3152 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003153LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003154parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003155{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003156 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003158 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003159 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 struct lysp_node_uses *uses;
3161
David Sedlák60adc092019-08-06 15:57:02 +02003162 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003163 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003165 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003168 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003169 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170
3171 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003172 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003174 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003175 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003177 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003178 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003180 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003181 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003183 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003184 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003186 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003187 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 break;
3189
Radek Krejcid6b76452019-09-03 17:03:03 +02003190 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003191 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003193 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003194 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003196 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003197 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003198 break;
3199 default:
David Sedlákb3077192019-06-19 10:55:37 +02003200 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201 return LY_EVALID;
3202 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003204
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 return ret;
3206}
3207
Michal Vaskoea5abea2018-09-18 13:10:54 +02003208/**
3209 * @brief Parse the case statement.
3210 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003211 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003212 * @param[in,out] siblings Siblings to add to.
3213 *
3214 * @return LY_ERR values.
3215 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003216LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003217parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003218{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003219 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003221 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003222 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223 struct lysp_node_case *cas;
3224
David Sedlák60adc092019-08-06 15:57:02 +02003225 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003226 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003228 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003229
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003231 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003232 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233
3234 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003235 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003237 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003238 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003240 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003241 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003243 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003244 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003246 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003247 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003249 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003250 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 break;
3252
Radek Krejcid6b76452019-09-03 17:03:03 +02003253 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003254 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003255 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003256 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003257 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003258 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003259 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003260 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003261 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003262 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003263 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003265 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003266 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003268 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003269 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003271 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003272 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003274 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003275 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003277 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003278 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279 break;
3280 default:
David Sedlákb3077192019-06-19 10:55:37 +02003281 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003282 return LY_EVALID;
3283 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003284 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285 return ret;
3286}
3287
Michal Vaskoea5abea2018-09-18 13:10:54 +02003288/**
3289 * @brief Parse the choice statement.
3290 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003291 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003292 * @param[in,out] siblings Siblings to add to.
3293 *
3294 * @return LY_ERR values.
3295 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003296LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003297parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003298{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003299 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003301 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003302 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003303 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304
David Sedlák60adc092019-08-06 15:57:02 +02003305 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003306 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003307 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003308 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003309
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003311 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003312 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313
3314 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003315 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003317 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003318 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003320 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003321 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003323 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003324 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003326 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003327 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003328 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003329 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003330 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003332 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003333 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003335 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003336 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003338 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01003339 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
Michal Vasko7f45cf22020-10-01 12:49:44 +02003340 &choice->exts));
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01003341 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342 break;
3343
Radek Krejcid6b76452019-09-03 17:03:03 +02003344 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003345 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003346 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003347 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003348 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003350 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003351 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003353 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003354 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003355 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003357 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003358 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003360 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003361 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003363 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003364 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003366 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003367 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003369 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003370 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 break;
3372 default:
David Sedlákb3077192019-06-19 10:55:37 +02003373 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003374 return LY_EVALID;
3375 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003376 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377 return ret;
3378}
3379
Michal Vaskoea5abea2018-09-18 13:10:54 +02003380/**
3381 * @brief Parse the container statement.
3382 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003383 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003384 * @param[in,out] siblings Siblings to add to.
3385 *
3386 * @return LY_ERR values.
3387 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003388LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003389parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390{
3391 LY_ERR ret = 0;
3392 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003393 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003394 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 struct lysp_node_container *cont;
3396
David Sedlák60adc092019-08-06 15:57:02 +02003397 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003398 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003400 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003403 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003404 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405
3406 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003407 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003409 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003410 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003412 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003413 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003415 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003416 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003418 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003419 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003421 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003422 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003424 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003425 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003427 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003428 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 break;
3430
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003432 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003433 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003434 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003435 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003437 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003438 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003440 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003441 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003443 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003444 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003446 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003447 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003449 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003450 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003452 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003453 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003454 break;
3455
Radek Krejcid6b76452019-09-03 17:03:03 +02003456 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003457 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003459 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003460 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003462 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003463 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003464 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003466 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003467 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003468 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003469 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003470 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003471 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003473 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003474 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003475 break;
3476 default:
David Sedlákb3077192019-06-19 10:55:37 +02003477 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478 return LY_EVALID;
3479 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003481
Michal Vasko7fbc8162018-09-17 10:35:16 +02003482 return ret;
3483}
3484
Michal Vaskoea5abea2018-09-18 13:10:54 +02003485/**
3486 * @brief Parse the list statement.
3487 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003488 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003489 * @param[in,out] siblings Siblings to add to.
3490 *
3491 * @return LY_ERR values.
3492 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003493LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003494parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003495{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003496 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003498 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003499 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 struct lysp_node_list *list;
3501
David Sedlák60adc092019-08-06 15:57:02 +02003502 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003503 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003504 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003505 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003506
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003508 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003509 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003510
3511 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003512 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003514 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003515 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003516 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003517 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003518 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003519 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003520 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003521 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003523 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003524 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003527 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003529 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003530 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003532 case LY_STMT_KEY:
Radek Krejcifc596f92021-02-26 22:40:26 +01003533 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003535 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003536 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003538 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003539 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003541 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003542 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003544 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003545 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 break;
3547
Radek Krejcid6b76452019-09-03 17:03:03 +02003548 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003549 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003550 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003551 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003552 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003554 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003555 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003557 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003558 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003559 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003560 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003561 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003562 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003563 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003564 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003566 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003567 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003568 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003569 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003570 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571 break;
3572
Radek Krejcid6b76452019-09-03 17:03:03 +02003573 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003574 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003575 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003576 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003577 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003579 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003580 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003581 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003583 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003584 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003585 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003586 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003587 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003588 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003590 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003591 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 break;
3593 default:
David Sedlákb3077192019-06-19 10:55:37 +02003594 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 return LY_EVALID;
3596 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003598
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 return ret;
3600}
3601
Michal Vaskoea5abea2018-09-18 13:10:54 +02003602/**
3603 * @brief Parse the yin-element statement.
3604 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003605 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003606 * @param[in,out] flags Flags to write to.
3607 * @param[in,out] exts Extension instances to add to.
3608 *
3609 * @return LY_ERR values.
3610 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003611static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003612parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003614 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003616 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003617 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618
3619 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003620 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621 return LY_EVALID;
3622 }
3623
3624 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003625 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626
Radek Krejcif13b87b2020-12-01 22:02:17 +01003627 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003629 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003630 *flags |= LYS_YINELEM_FALSE;
3631 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003632 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633 free(buf);
3634 return LY_EVALID;
3635 }
3636 free(buf);
3637
aPieceka24a2252021-05-07 10:52:31 +02003638 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003640 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003641 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003642 LY_CHECK_RET(ret);
3643 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 default:
David Sedlákb3077192019-06-19 10:55:37 +02003645 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 return LY_EVALID;
3647 }
3648 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 return ret;
3650}
3651
Michal Vaskoea5abea2018-09-18 13:10:54 +02003652/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003653 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003654 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003655 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003656 * @param[in,out] argument Value to write to.
3657 * @param[in,out] flags Flags to write to.
3658 * @param[in,out] exts Extension instances to add to.
3659 *
3660 * @return LY_ERR values.
3661 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003662static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003663parse_argument(struct lys_yang_parser_ctx *ctx, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003665 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003666 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003667 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003668 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669
3670 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003671 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672 return LY_EVALID;
3673 }
3674
3675 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003676 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003677 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678
aPieceka24a2252021-05-07 10:52:31 +02003679 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003680 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003681 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003682 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003683 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003684 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003685 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003686 break;
3687 default:
David Sedlákb3077192019-06-19 10:55:37 +02003688 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003689 return LY_EVALID;
3690 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003691 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692 return ret;
3693}
3694
Michal Vaskoea5abea2018-09-18 13:10:54 +02003695/**
3696 * @brief Parse the extension statement.
3697 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003698 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003699 * @param[in,out] extensions Extensions to add to.
3700 *
3701 * @return LY_ERR values.
3702 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003703static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003704parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003705{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003706 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003707 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003708 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003709 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003710 struct lysp_ext *ex;
3711
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003712 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003713
3714 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003715 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003716 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717
aPieceka24a2252021-05-07 10:52:31 +02003718 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003720 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003721 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003722 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003723 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003724 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003725 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003726 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003727 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003729 case LY_STMT_ARGUMENT:
Radek Krejci9f87b0c2021-03-05 14:45:26 +01003730 LY_CHECK_RET(parse_argument(ctx, &ex->argname, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003731 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003732 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003733 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 break;
3735 default:
David Sedlákb3077192019-06-19 10:55:37 +02003736 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003737 return LY_EVALID;
3738 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003740 return ret;
3741}
3742
Michal Vaskoea5abea2018-09-18 13:10:54 +02003743/**
3744 * @brief Parse the deviate statement.
3745 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003746 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003747 * @param[in,out] deviates Deviates to add to.
3748 *
3749 * @return LY_ERR values.
3750 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003751LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003752parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003753{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003754 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003756 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003757 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003758 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759 struct lysp_deviate_add *d_add = NULL;
3760 struct lysp_deviate_rpl *d_rpl = NULL;
3761 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003762 const char **d_units = NULL;
3763 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003764 struct lysp_restr **d_musts = NULL;
3765 uint16_t *d_flags = 0;
3766 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003767
3768 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003769 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003770
Radek Krejcif13b87b2020-12-01 22:02:17 +01003771 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003773 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003774 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003775 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003776 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003777 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 dev_mod = LYS_DEV_DELETE;
3779 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003780 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 free(buf);
3782 return LY_EVALID;
3783 }
3784 free(buf);
3785
3786 /* create structure */
3787 switch (dev_mod) {
3788 case LYS_DEV_NOT_SUPPORTED:
3789 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003790 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791 break;
3792 case LYS_DEV_ADD:
3793 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003794 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 d = (struct lysp_deviate *)d_add;
3796 d_units = &d_add->units;
3797 d_uniques = &d_add->uniques;
3798 d_dflts = &d_add->dflts;
3799 d_musts = &d_add->musts;
3800 d_flags = &d_add->flags;
3801 d_min = &d_add->min;
3802 d_max = &d_add->max;
3803 break;
3804 case LYS_DEV_REPLACE:
3805 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003806 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003807 d = (struct lysp_deviate *)d_rpl;
3808 d_units = &d_rpl->units;
3809 d_flags = &d_rpl->flags;
3810 d_min = &d_rpl->min;
3811 d_max = &d_rpl->max;
3812 break;
3813 case LYS_DEV_DELETE:
3814 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003815 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003816 d = (struct lysp_deviate *)d_del;
3817 d_units = &d_del->units;
3818 d_uniques = &d_del->uniques;
3819 d_dflts = &d_del->dflts;
3820 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003821 break;
3822 default:
3823 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003824 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003825 }
3826 d->mod = dev_mod;
3827
3828 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003829 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830
aPieceka24a2252021-05-07 10:52:31 +02003831 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003833 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 switch (dev_mod) {
3835 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003836 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003837 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838 return LY_EVALID;
3839 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003840 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 break;
3842 }
3843 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003844 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845 switch (dev_mod) {
3846 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003847 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 return LY_EVALID;
3849 case LYS_DEV_REPLACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003850 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts));
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01003851 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 break;
3853 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003854 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855 break;
3856 }
3857 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003858 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003859 switch (dev_mod) {
3860 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003861 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003862 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863 return LY_EVALID;
3864 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003865 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 break;
3867 }
3868 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003869 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 switch (dev_mod) {
3871 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003872 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003873 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874 return LY_EVALID;
3875 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003876 LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 break;
3878 }
3879 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003880 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 switch (dev_mod) {
3882 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003883 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003884 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885 return LY_EVALID;
3886 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003887 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888 break;
3889 }
3890 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003891 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 switch (dev_mod) {
3893 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003894 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003895 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003896 return LY_EVALID;
3897 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003898 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003899 break;
3900 }
3901 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003902 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003903 switch (dev_mod) {
3904 case LYS_DEV_NOT_SUPPORTED:
3905 case LYS_DEV_ADD:
3906 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003907 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003908 return LY_EVALID;
3909 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003910 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003911 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003912 return LY_EVALID;
3913 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003915 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003916 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917 break;
3918 }
3919 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003920 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921 switch (dev_mod) {
3922 case LYS_DEV_NOT_SUPPORTED:
3923 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003924 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925 return LY_EVALID;
3926 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003927 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928 break;
3929 }
3930 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003931 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003932 switch (dev_mod) {
3933 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003934 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003935 return LY_EVALID;
3936 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003937 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938 break;
3939 }
3940 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003941 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003942 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATE, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003943 break;
3944 default:
David Sedlákb3077192019-06-19 10:55:37 +02003945 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946 return LY_EVALID;
3947 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003948 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 return ret;
3950}
3951
Michal Vaskoea5abea2018-09-18 13:10:54 +02003952/**
3953 * @brief Parse the deviation statement.
3954 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003955 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003956 * @param[in,out] deviations Deviations to add to.
3957 *
3958 * @return LY_ERR values.
3959 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003960LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003961parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003963 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003965 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003966 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967 struct lysp_deviation *dev;
3968
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003969 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003970
3971 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003972 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003973 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003974 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003975
aPieceka24a2252021-05-07 10:52:31 +02003976 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003977 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003978 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003979 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003980 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003981 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003982 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003984 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003985 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003986 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003987 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003988 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATION, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003989 break;
3990 default:
David Sedlákb3077192019-06-19 10:55:37 +02003991 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003992 return LY_EVALID;
3993 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003994 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003995 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003996checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 /* mandatory substatements */
3998 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003999 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 return LY_EVALID;
4001 }
4002
4003 return ret;
4004}
4005
Michal Vaskoea5abea2018-09-18 13:10:54 +02004006/**
4007 * @brief Parse the feature statement.
4008 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004009 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004010 * @param[in,out] features Features to add to.
4011 *
4012 * @return LY_ERR values.
4013 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004014LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004015parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004017 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004018 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004019 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004020 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004021 struct lysp_feature *feat;
4022
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004023 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024
4025 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004026 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004027 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004028
aPieceka24a2252021-05-07 10:52:31 +02004029 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004030 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004031 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004032 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004034 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004035 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004036 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004037 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004038 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004039 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004040 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004041 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004042 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004043 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004044 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004045 break;
4046 default:
David Sedlákb3077192019-06-19 10:55:37 +02004047 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004048 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004049 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004050 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004051 return ret;
4052}
4053
Michal Vaskoea5abea2018-09-18 13:10:54 +02004054/**
4055 * @brief Parse the identity statement.
4056 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004057 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004058 * @param[in,out] identities Identities to add to.
4059 *
4060 * @return LY_ERR values.
4061 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004062LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004063parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004064{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004065 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004066 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004067 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004068 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004069 struct lysp_ident *ident;
4070
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004071 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004072
4073 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004074 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004075 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004076
aPieceka24a2252021-05-07 10:52:31 +02004077 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004079 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004080 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004081 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004082 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004083 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004084 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004085 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004086 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004087 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004089 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004090 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004091 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004092 case LY_STMT_BASE:
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01004093 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004094 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 +01004095 return LY_EVALID;
4096 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004097 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 +02004098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004099 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004100 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004101 break;
4102 default:
David Sedlákb3077192019-06-19 10:55:37 +02004103 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004104 return LY_EVALID;
4105 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107 return ret;
4108}
4109
Michal Vaskoea5abea2018-09-18 13:10:54 +02004110/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004111 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004112 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004113 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004114 * @param[in,out] mod Module to write to.
4115 *
4116 * @return LY_ERR values.
4117 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004118LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004119parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120{
4121 LY_ERR ret = 0;
4122 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004123 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004124 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004125 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004126 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004127
Michal Vaskoc3781c32020-10-06 14:04:08 +02004128 mod->is_submod = 0;
4129
4130 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004131 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004132 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004133
aPieceka24a2252021-05-07 10:52:31 +02004134 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004135
Radek Krejcie3846472018-10-15 15:24:51 +02004136#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004137 if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004138
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139 switch (kw) {
4140 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004141 case LY_STMT_NAMESPACE:
4142 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004143 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4144 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004145 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004146 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004147 break;
4148 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004149 case LY_STMT_INCLUDE:
4150 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004151 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 break;
4153 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004154 case LY_STMT_ORGANIZATION:
4155 case LY_STMT_CONTACT:
4156 case LY_STMT_DESCRIPTION:
4157 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004158 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 break;
4160
4161 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004162 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004163 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004164 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_ANYDATA:
4167 case LY_STMT_ANYXML:
4168 case LY_STMT_AUGMENT:
4169 case LY_STMT_CHOICE:
4170 case LY_STMT_CONTAINER:
4171 case LY_STMT_DEVIATION:
4172 case LY_STMT_EXTENSION:
4173 case LY_STMT_FEATURE:
4174 case LY_STMT_GROUPING:
4175 case LY_STMT_IDENTITY:
4176 case LY_STMT_LEAF:
4177 case LY_STMT_LEAF_LIST:
4178 case LY_STMT_LIST:
4179 case LY_STMT_NOTIFICATION:
4180 case LY_STMT_RPC:
4181 case LY_STMT_TYPEDEF:
4182 case LY_STMT_USES:
4183 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 mod_stmt = Y_MOD_BODY;
4185 break;
4186 default:
4187 /* error handled in the next switch */
4188 break;
4189 }
Radek Krejcie3846472018-10-15 15:24:51 +02004190#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004191
Radek Krejcie3846472018-10-15 15:24:51 +02004192 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193 switch (kw) {
4194 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004195 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004196 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004198 case LY_STMT_NAMESPACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004199 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004200 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004201 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01004202 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004203 break;
4204
4205 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004206 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004207 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004209 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004210 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004211 break;
4212
4213 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004214 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004215 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004217 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004218 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004220 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004221 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004223 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004224 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004225 break;
4226
4227 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004228 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004229 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004230 break;
4231
4232 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004233 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004234 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004235 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004236 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004237 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004239 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004240 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004242 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004243 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004245 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004246 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004247 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004248 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004249 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004250 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004251 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004252 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004253 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004254 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004255 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004256 break;
4257
Radek Krejcid6b76452019-09-03 17:03:03 +02004258 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004259 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004261 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004262 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004263 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004264 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004265 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004267 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004268 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004269 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004270 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004271 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004272 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004273 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004274 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004275 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004276 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004277 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004278 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004279 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004280 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004281 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004282 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004283 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004284 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004285 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004286 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004287 break;
4288
4289 default:
David Sedlákb3077192019-06-19 10:55:37 +02004290 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004291 return LY_EVALID;
4292 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004293 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004294 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004295
Radek Krejci6d6556c2018-11-08 09:37:45 +01004296checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004297 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004298 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004299 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004300 return LY_EVALID;
4301 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004302 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004303 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004304 }
4305
Radek Krejcie9e987e2018-10-31 12:50:27 +01004306 /* submodules share the namespace with the module names, so there must not be
4307 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004308 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004309 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004310 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004311 return LY_EVALID;
4312 }
4313
4314 return ret;
4315}
4316
4317/**
4318 * @brief Parse submodule substatements.
4319 *
4320 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004321 * @param[out] submod Parsed submodule structure.
4322 *
4323 * @return LY_ERR values.
4324 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004325LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004326parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004327{
4328 LY_ERR ret = 0;
4329 char *buf, *word;
4330 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004331 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004332 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004333 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004334
Michal Vaskoc3781c32020-10-06 14:04:08 +02004335 submod->is_submod = 1;
4336
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004337 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004338 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004339 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004340
aPieceka24a2252021-05-07 10:52:31 +02004341 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004342
4343#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004344 if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004345
4346 switch (kw) {
4347 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004348 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004349 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4350 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004351 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004352 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4353 break;
4354 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004355 case LY_STMT_INCLUDE:
4356 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004357 CHECK_ORDER(Y_MOD_LINKAGE);
4358 break;
4359 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_ORGANIZATION:
4361 case LY_STMT_CONTACT:
4362 case LY_STMT_DESCRIPTION:
4363 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004364 CHECK_ORDER(Y_MOD_META);
4365 break;
4366
4367 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004368 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004369 CHECK_ORDER(Y_MOD_REVISION);
4370 break;
4371 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_ANYDATA:
4373 case LY_STMT_ANYXML:
4374 case LY_STMT_AUGMENT:
4375 case LY_STMT_CHOICE:
4376 case LY_STMT_CONTAINER:
4377 case LY_STMT_DEVIATION:
4378 case LY_STMT_EXTENSION:
4379 case LY_STMT_FEATURE:
4380 case LY_STMT_GROUPING:
4381 case LY_STMT_IDENTITY:
4382 case LY_STMT_LEAF:
4383 case LY_STMT_LEAF_LIST:
4384 case LY_STMT_LIST:
4385 case LY_STMT_NOTIFICATION:
4386 case LY_STMT_RPC:
4387 case LY_STMT_TYPEDEF:
4388 case LY_STMT_USES:
4389 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004390 mod_stmt = Y_MOD_BODY;
4391 break;
4392 default:
4393 /* error handled in the next switch */
4394 break;
4395 }
4396#undef CHECK_ORDER
4397
4398 prev_kw = kw;
4399 switch (kw) {
4400 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004401 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004402 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004404 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004405 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004406 break;
4407
4408 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004409 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004410 if (submod->version == LYS_VERSION_1_1) {
4411 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4412 submod->name);
4413 }
Radek Krejci33090f92020-12-17 20:12:46 +01004414 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004416 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004417 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004418 break;
4419
4420 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004421 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004422 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004423 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004424 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004425 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004426 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004427 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004428 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004429 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004430 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004431 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004432 break;
4433
4434 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004435 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004436 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004437 break;
4438
4439 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004440 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004441 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004442 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004443 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004444 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004446 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004447 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004449 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004450 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004452 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004453 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004455 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004456 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004458 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004459 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004461 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004462 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004463 break;
4464
Radek Krejcid6b76452019-09-03 17:03:03 +02004465 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004466 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004467 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004468 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004469 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004470 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004471 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004472 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004473 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004474 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004475 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004476 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004477 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004478 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004479 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004480 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004481 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004482 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004483 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004484 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004485 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004486 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004487 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004488 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004489 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004490 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004491 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004492 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004493 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004494 break;
4495
4496 default:
David Sedlákb3077192019-06-19 10:55:37 +02004497 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004498 return LY_EVALID;
4499 }
4500 }
4501 LY_CHECK_RET(ret);
4502
4503checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004504 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004505 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004506 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004507 return LY_EVALID;
4508 }
4509
4510 /* submodules share the namespace with the module names, so there must not be
4511 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004512 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004513 /* main modules may have different revisions */
4514 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004515 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004516 return LY_EVALID;
4517 }
4518
Michal Vasko7fbc8162018-09-17 10:35:16 +02004519 return ret;
4520}
4521
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004522/**
4523 * @brief Skip any redundant characters, namely whitespaces and comments.
4524 *
4525 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004526 * @return LY_SUCCESS on success.
4527 * @return LY_EVALID on invalid comment.
4528 */
4529static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004530skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004531{
4532 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004533 while (ctx->in->current[0]) {
4534 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004535 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004536 ly_in_skip(ctx->in, 2);
4537 LY_CHECK_RET(skip_comment(ctx, 1));
4538 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004539 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004540 ly_in_skip(ctx->in, 2);
4541 LY_CHECK_RET(skip_comment(ctx, 2));
4542 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004543 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004544 if (ctx->in->current[0] == '\n') {
4545 LY_IN_NEW_LINE(ctx->in);
4546 }
Radek Krejci33090f92020-12-17 20:12:46 +01004547 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004548 } else {
4549 break;
4550 }
4551 }
4552
4553 return LY_SUCCESS;
4554}
4555
Radek Krejcid4557c62018-09-17 11:42:09 +02004556LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004557yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004558 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004559{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004560 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004561 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004562 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004563 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004564 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004565
aPiecek56be92a2021-07-01 07:18:10 +02004566 assert(context && ly_ctx && main_ctx && in && submod);
4567
David Sedlák1b623122019-08-05 15:27:49 +02004568 /* create context */
4569 *context = calloc(1, sizeof **context);
4570 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004571 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004572 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004573 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004574
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004575 mod_p = calloc(1, sizeof *mod_p);
4576 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01004577 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004578 mod_p->parsing = 1;
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01004579
4580 /* use main context parsed mods adding the current one */
4581 (*context)->parsed_mods = main_ctx->parsed_mods;
4582 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004583
Radek Krejciddace2c2021-01-08 11:30:56 +01004584 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004585
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004586 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004587 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004588 LY_CHECK_GOTO(ret, cleanup);
4589
Michal Vasko7fbc8162018-09-17 10:35:16 +02004590 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004591 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004592 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004593
Radek Krejcid6b76452019-09-03 17:03:03 +02004594 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004595 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004596 ret = LY_EINVAL;
4597 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004598 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004599 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004600 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004601 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004602 }
4603
Michal Vasko7fbc8162018-09-17 10:35:16 +02004604 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004605 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004606 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004607
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004608 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004609 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004610 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004611 if (in->current[0]) {
4612 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004613 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004614 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004615 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004616
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004617 mod_p->parsing = 0;
4618 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004619
Radek Krejcibbe09a92018-11-08 09:36:54 +01004620cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004621 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004622 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004623 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004624 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004625 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004626 }
4627
4628 return ret;
4629}
4630
4631LY_ERR
aPiecekc3e26142021-06-22 14:25:49 +02004632yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004633{
4634 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004635 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004636 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004637 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004638 struct lysp_module *mod_p = NULL;
4639
David Sedlák1b623122019-08-05 15:27:49 +02004640 /* create context */
4641 *context = calloc(1, sizeof **context);
4642 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004643 (*context)->format = LYS_IN_YANG;
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01004644 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004645 (*context)->in = in;
aPiecek8d4e75d2021-06-24 14:47:06 +02004646 (*context)->main_ctx = (struct lys_parser_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004647
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004648 mod_p = calloc(1, sizeof *mod_p);
4649 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4650 mod_p->mod = mod;
Michal Vaskoa3afbfe2021-12-07 14:04:47 +01004651 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004652
Radek Krejciddace2c2021-01-08 11:30:56 +01004653 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004654
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004655 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004656 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004657 LY_CHECK_GOTO(ret, cleanup);
4658
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004659 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004660 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004661 LY_CHECK_GOTO(ret, cleanup);
4662
Radek Krejcid6b76452019-09-03 17:03:03 +02004663 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004664 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 +01004665 ret = LY_EINVAL;
4666 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004667 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004668 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004669 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004670 goto cleanup;
4671 }
4672
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004673 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004674 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004675 LY_CHECK_GOTO(ret, cleanup);
4676
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004677 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004678 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004679 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004680 if (in->current[0]) {
4681 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004682 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004683 goto cleanup;
4684 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004685
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004686 mod->parsed = mod_p;
4687
4688cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004689 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004690 if (ret) {
4691 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004692 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004693 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004694 }
4695
Michal Vasko7fbc8162018-09-17 10:35:16 +02004696 return ret;
4697}