blob: b0609c757b9fc520e3dfe3e9a208c5db23631cc5 [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/**
59 * @brief Loop through all substatements providing, return if there are none.
60 *
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.
66 *
67 * @return In case there are no substatements or a fatal error encountered.
68 */
Radek Krejci33090f92020-12-17 20:12:46 +010069#define YANG_READ_SUBSTMT_FOR(CTX, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
70 LY_CHECK_RET(get_keyword(CTX, &KW, &WORD, &WORD_LEN)); \
Radek Krejcid6b76452019-09-03 17:03:03 +020071 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010072 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010073 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020074 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020075 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020076 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020077 return LY_EVALID; \
78 } \
Radek Krejci33090f92020-12-17 20:12:46 +010079 for (ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020080 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Radek Krejci33090f92020-12-17 20:12:46 +010081 ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020082
Radek Krejci33090f92020-12-17 20:12:46 +010083LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020084 struct lysp_node **siblings);
Radek Krejci33090f92020-12-17 20:12:46 +010085LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
86LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
87LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
88LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +010089LY_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 +020090
Michal Vaskoea5abea2018-09-18 13:10:54 +020091/**
92 * @brief Add another character to dynamic buffer, a low-level function.
93 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020094 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020095 *
Radek Krejci404251e2018-10-09 12:06:44 +020096 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +020097 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +020098 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020099 * @param[in,out] buf Buffer to use, can be moved by realloc().
100 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200101 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200102 *
103 * @return LY_ERR values.
104 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200105LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200106buf_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 +0200107{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100108#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200109 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100110 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200111 *buf = ly_realloc(*buf, *buf_len);
112 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
113 }
Radek Krejcic0917392019-04-10 13:04:04 +0200114 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200116 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200117 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200118 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200119
Radek Krejci44ceedc2018-10-02 15:54:31 +0200120 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200121 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100122#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200123}
124
Michal Vaskoea5abea2018-09-18 13:10:54 +0200125/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
127 *
128 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200129 * @param[in] arg Type of the input string to select method of checking character validity.
130 * @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 +0200131 * 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 +0200132 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
133 * @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 +0200134 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200135 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200136 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
137 * 0 - colon not yet found (no prefix)
138 * 1 - \p c is the colon character
139 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200140 *
141 * @return LY_ERR values.
142 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200143LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100144buf_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 +0200145 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200146{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200147 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200148 size_t len;
149
Radek Krejcif29b7c32019-04-09 16:17:49 +0200150 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
151 assert(!need_buf || (need_buf && word_b));
152
Radek Krejci44ceedc2018-10-02 15:54:31 +0200153 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100154 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
155 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
156 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200157 if (c == '\n') {
158 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100159 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200160 } else {
161 /* note - even the multibyte character is count as 1 */
162 ++ctx->indent;
163 }
164
Radek Krejci44ceedc2018-10-02 15:54:31 +0200165 /* check character validity */
166 switch (arg) {
167 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100168 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200169 break;
170 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100171 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200172 break;
173 case Y_STR_ARG:
174 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100175 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176 break;
177 }
178
Michal Vasko7fbc8162018-09-17 10:35:16 +0200179 if (word_b && *word_b) {
180 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100181 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200182 return LY_EMEM;
183 }
184
185 /* in case of realloc */
186 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200187 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200188 /* first time we need a buffer, copy everything read up to now */
189 if (*word_len) {
190 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200191 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200192 *buf_len = *word_len;
193 memcpy(*word_b, *word_p, *word_len);
194 }
195
196 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100197 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200198 return LY_EMEM;
199 }
200
201 /* in case of realloc */
202 *word_p = *word_b;
203 } else {
204 /* just remember the first character pointer */
205 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100206 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200207 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200208 /* ... and update the word's length */
209 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100210 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200211 }
212
213 return LY_SUCCESS;
214}
215
Michal Vaskoea5abea2018-09-18 13:10:54 +0200216/**
217 * @brief Skip YANG comment in data.
218 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200219 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200220 * @param[in] comment Type of the comment to process:
221 * 1 for a one-line comment,
222 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200223 * @return LY_ERR values.
224 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200225LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100226skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200227{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100228 /* internal statuses: */
229#define COMMENT_NO 0 /* comment ended */
230#define COMMENT_LINE 1 /* in line comment */
231#define COMMENT_BLOCK 2 /* in block comment */
232#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
233
Radek Krejci33090f92020-12-17 20:12:46 +0100234 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200235 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100236 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100237 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100238 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100239 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200240 }
241 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100242 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100243 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100244 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100245 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100246 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200247 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200248 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100249 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100250 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100251 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100252 } else if (ctx->in->current[0] != '*') {
253 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100254 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200255 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100256 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200257 }
258 break;
259 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200260 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200261 }
262
Radek Krejci33090f92020-12-17 20:12:46 +0100263 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200264 ctx->indent = 0;
265 } else {
266 ++ctx->indent;
267 }
Radek Krejci33090f92020-12-17 20:12:46 +0100268 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200269 }
270
Radek Krejci33090f92020-12-17 20:12:46 +0100271 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200272 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200273 return LY_EVALID;
274 }
275
276 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100277
278#undef COMMENT_NO
279#undef COMMENT_LINE
280#undef COMMENT_BLOCK
281#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200282}
283
Michal Vaskoea5abea2018-09-18 13:10:54 +0200284/**
285 * @brief Read a quoted string from data.
286 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200287 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200288 * @param[in] arg Type of YANG keyword argument expected.
289 * @param[out] word_p Pointer to the read quoted string.
290 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
291 * set to NULL. Otherwise equal to \p word_p.
292 * @param[out] word_len Length of the read quoted string.
293 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
294 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
295 * indenation in the final quoted string.
296 *
297 * @return LY_ERR values.
298 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200299static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100300read_qstring(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200301 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200302{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100303 /* string parsing status: */
304#define STRING_ENDED 0 /* string ended */
305#define STRING_SINGLE_QUOTED 1 /* string with ' */
306#define STRING_DOUBLE_QUOTED 2 /* string with " */
307#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
308#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
309#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
310
Radek Krejci1deb5be2020-08-26 16:43:36 +0200311 uint8_t string;
312 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200313 ly_bool need_buf = 0;
314 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200315 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200316 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200317
Radek Krejci33090f92020-12-17 20:12:46 +0100318 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100319 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200320 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100322 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100323 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200324 }
Radek Krejci33090f92020-12-17 20:12:46 +0100325 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200326
Radek Krejci33090f92020-12-17 20:12:46 +0100327 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200328 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100329 case STRING_SINGLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100330 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200331 case '\'':
332 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100333 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100334 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200335 break;
336 default:
337 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100338 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 +0200339 break;
340 }
341 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100342 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100343 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200344 case '\"':
345 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100346 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100347 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200348 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200349 break;
350 case '\\':
351 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100352 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200353
354 /* the backslash sequence is substituted, so we will need a buffer to store the result */
355 need_buf = 1;
356
357 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100358 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200359
360 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
361 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
362 trailing_ws = 0;
363
364 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
365 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200366 break;
367 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200368 if (current_indent < block_indent) {
369 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100370 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200371 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100372 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100373 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 +0100374 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200375 }
376 break;
377 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200378 if (current_indent < block_indent) {
379 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100380 current_indent += Y_TAB_SPACES;
381 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200382 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200383 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100384 c = ctx->in->current;
385 ctx->in->current = " ";
386 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
387 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100388 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200389 }
Radek Krejci33090f92020-12-17 20:12:46 +0100390 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200391 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100392 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100393 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 +0100394 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200395 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100396 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200397 }
398 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200399 case '\r':
400 if (ctx->in->current[1] != '\n') {
401 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
402 return LY_EVALID;
403 }
404 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200405 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200406 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200407 /* we will be removing the indents so we need our own buffer */
408 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200409
410 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200411 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200412
Radek Krejciff13cd12019-10-25 15:34:24 +0200413 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200414 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200415 }
416
417 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100418 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 +0200419
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200420 /* reset context indentation counter for possible string after this one */
421 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200422 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200423 break;
424 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200425 /* first non-whitespace character, stop eating indentation */
426 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200427
428 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100429 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 +0200430 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200431 break;
432 }
433 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100434 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200435 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100436 c = ctx->in->current;
437 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100439 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100440 /* fix false newline count in buf_store_char() */
441 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200442 break;
443 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100444 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200445 break;
446 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200447 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200448 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200449 break;
450 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200451 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100452 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200453 return LY_EVALID;
454 }
455
456 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100457 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 +0200458
Radek Krejcif13b87b2020-12-01 22:02:17 +0100459 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100460 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200461 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100462 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100463 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200464 case '+':
465 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100466 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200467 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200468 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200469 case '\r':
470 if (ctx->in->current[1] != '\n') {
471 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
472 return LY_EVALID;
473 }
474 MOVE_INPUT(ctx, 1);
475 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200476 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100477 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100478 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200479 case ' ':
480 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200481 /* just skip */
482 break;
483 default:
484 /* string is finished */
485 goto string_end;
486 }
Radek Krejci33090f92020-12-17 20:12:46 +0100487 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200488 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100489 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100490 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200491 case '\r':
492 if (ctx->in->current[1] != '\n') {
493 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
494 return LY_EVALID;
495 }
496 MOVE_INPUT(ctx, 1);
497 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200498 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100499 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100500 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200501 case ' ':
502 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200503 /* skip */
504 break;
505 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100506 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200507 break;
508 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100509 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200510 break;
511 default:
512 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200513 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200514 return LY_EVALID;
515 }
Radek Krejci33090f92020-12-17 20:12:46 +0100516 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200517 break;
518 default:
519 return LY_EINT;
520 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200521 }
522
523string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200524 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200525 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200526 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200527 return LY_EVALID;
528 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200529 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100530
531#undef STRING_ENDED
532#undef STRING_SINGLE_QUOTED
533#undef STRING_DOUBLE_QUOTED
534#undef STRING_DOUBLE_QUOTED_ESCAPED
535#undef STRING_PAUSED_NEXTSTRING
536#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537}
538
Michal Vaskoea5abea2018-09-18 13:10:54 +0200539/**
540 * @brief Get another YANG string from the raw data.
541 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200542 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200543 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200544 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
545 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200546 * @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 +0200547 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
548 * set to NULL. Otherwise equal to \p word_p.
549 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200550 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200551 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200552LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100553get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200554 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200555{
Michal Vaskob68ea142021-04-26 13:46:00 +0200556 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200557 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200558 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200559
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560 /* word buffer - dynamically allocated */
561 *word_b = NULL;
562
563 /* word pointer - just a pointer to data */
564 *word_p = NULL;
565
566 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100567 while (ctx->in->current[0]) {
568 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200569 case '\'':
570 case '\"':
571 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200572 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100573 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200574 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200575 ret = LY_EVALID;
576 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200577 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200578 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100579 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200580 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200581 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200582 goto str_end;
583 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100584 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200585 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100586 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200587 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100588 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200589 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100590 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200591 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200592 } else {
593 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200594 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 +0200595 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200596 break;
597 case ' ':
598 if (*word_len) {
599 /* word is finished */
600 goto str_end;
601 }
Radek Krejci33090f92020-12-17 20:12:46 +0100602 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200603 break;
604 case '\t':
605 if (*word_len) {
606 /* word is finished */
607 goto str_end;
608 }
609 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100610 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200611
Radek Krejci33090f92020-12-17 20:12:46 +0100612 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200613 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200614 case '\r':
615 if (ctx->in->current[1] != '\n') {
616 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200617 ret = LY_EVALID;
618 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200619 }
620 MOVE_INPUT(ctx, 1);
621 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200622 case '\n':
623 if (*word_len) {
624 /* word is finished */
625 goto str_end;
626 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100627 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100628 MOVE_INPUT(ctx, 1);
629
Michal Vasko7fbc8162018-09-17 10:35:16 +0200630 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200631 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200632 break;
633 case ';':
634 case '{':
635 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
636 /* word is finished */
637 goto str_end;
638 }
639
Radek Krejci33090f92020-12-17 20:12:46 +0100640 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200641 ret = LY_EVALID;
642 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200643 case '}':
644 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100645 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200646 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200647 ret = LY_EVALID;
648 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200649 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200650 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 +0200651 break;
652 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200653 }
654
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200655 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200656 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200657 ret = LY_EVALID;
658 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200659
Michal Vasko7fbc8162018-09-17 10:35:16 +0200660str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200661 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200662 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200663 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200664 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200665 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200666 *word_p = *word_b;
667 }
668
669 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200670
671error:
672 free(*word_b);
673 *word_b = NULL;
674 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200675}
676
Michal Vaskoea5abea2018-09-18 13:10:54 +0200677/**
678 * @brief Get another YANG keyword from the raw data.
679 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200680 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200681 * @param[out] kw YANG keyword read.
682 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
683 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
684 *
685 * @return LY_ERR values.
686 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200687LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100688get_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 +0200689{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200690 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200691 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200692 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200693
694 if (word_p) {
695 *word_p = NULL;
696 *word_len = 0;
697 }
698
699 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100700 while (ctx->in->current[0]) {
701 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200702 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100703 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200704 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100705 MOVE_INPUT(ctx, 2);
706 LY_CHECK_RET(skip_comment(ctx, 1));
707 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200708 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100709 MOVE_INPUT(ctx, 2);
710 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200711 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200712 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200713 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200714 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 }
Radek Krejci13028282019-06-11 14:56:48 +0200716 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200717 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200718 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100719 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200720 ctx->indent = 0;
721 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200722 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200723 /* skip whitespaces (optsep) */
724 ++ctx->indent;
725 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200726 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200727 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100728 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200729 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100730 case '\r':
731 /* possible CRLF endline */
732 if (ctx->in->current[1] == '\n') {
733 break;
734 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100735 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 default:
737 /* either a keyword start or an invalid character */
738 goto keyword_start;
739 }
740
Radek Krejci33090f92020-12-17 20:12:46 +0100741 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200742 }
743
744keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100745 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100746 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200747
Michal Vasko69730152020-10-09 16:30:07 +0200748 if ((*kw == LY_STMT_SYNTAX_SEMICOLON) || (*kw == LY_STMT_SYNTAX_LEFT_BRACE) || (*kw == LY_STMT_SYNTAX_RIGHT_BRACE)) {
Radek Krejci626df482018-10-11 15:06:31 +0200749 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200750 }
751
Radek Krejcid6b76452019-09-03 17:03:03 +0200752 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200753 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100754 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200755 case '\r':
756 if (ctx->in->current[1] != '\n') {
757 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
758 return LY_EVALID;
759 }
760 MOVE_INPUT(ctx, 1);
761 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200762 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200763 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200764 case ' ':
765 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200766 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200767 case ':':
768 /* keyword is not actually a keyword, but prefix of an extension.
769 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
770 * and we will be checking the keyword (extension instance) itself */
771 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100772 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200773 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200774 case '{':
775 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200776 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200777 break;
778 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100779 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200780 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100781 MOVE_INPUT(ctx, 1);
782 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200783 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200784 return LY_EVALID;
785 }
786 } else {
787 /* still can be an extension */
788 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200789
Radek Krejci156ccaf2018-10-15 15:49:17 +0200790extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100791 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200792 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
793 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200794 uint32_t c = 0;
795
Radek Krejci33090f92020-12-17 20:12:46 +0100796 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
797 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200798 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200799 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200800 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100801 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200802 }
Radek Krejci33090f92020-12-17 20:12:46 +0100803 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200804 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200805 return LY_EVALID;
806 }
807
808 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200809 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100810 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200811 return LY_EVALID;
812 }
813
Radek Krejcid6b76452019-09-03 17:03:03 +0200814 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200815 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200816
Radek Krejci626df482018-10-11 15:06:31 +0200817success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818 if (word_p) {
819 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100820 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821 }
822
823 return LY_SUCCESS;
824}
825
Michal Vaskoea5abea2018-09-18 13:10:54 +0200826/**
827 * @brief Parse extension instance substatements.
828 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200829 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200830 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200831 * @param[in] word Extension instance substatement name (keyword).
832 * @param[in] word_len Extension instance substatement name length.
833 * @param[in,out] child Children of this extension instance to add to.
834 *
835 * @return LY_ERR values.
836 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100838parse_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 +0200839 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200840{
841 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100842 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200843 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200844 struct lysp_stmt *stmt, *par_child;
845
846 stmt = calloc(1, sizeof *stmt);
847 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
848
Radek Krejcibb9b1982019-04-08 14:24:59 +0200849 /* insert into parent statements */
850 if (!*child) {
851 *child = stmt;
852 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200853 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200854 par_child->next = stmt;
855 }
856
Michal Vaskofc2cd072021-02-24 13:17:17 +0100857 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200858 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200859
860 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100861 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200862 if (word) {
863 if (buf) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200864 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200865 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200866 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200867 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200868 }
869
Radek Krejci8df109d2021-04-23 12:19:08 +0200870 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100871 stmt->prefix_data = ctx->parsed_mod;
872 stmt->kw = kw;
873
Radek Krejci33090f92020-12-17 20:12:46 +0100874 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, ) {
875 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200877 return ret;
878}
879
Michal Vaskoea5abea2018-09-18 13:10:54 +0200880/**
881 * @brief Parse extension instance.
882 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200883 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200884 * @param[in] ext_name Extension instance substatement name (keyword).
885 * @param[in] ext_name_len Extension instance substatement name length.
Radek Krejcifc596f92021-02-26 22:40:26 +0100886 * @param[in] insubstmt The statement this extension instance is a substatement of.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200887 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
888 * @param[in,out] exts Extension instances to add to.
889 *
890 * @return LY_ERR values.
891 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100893parse_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 +0200894 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100896 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200897 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200898 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200899 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200900 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200901
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200902 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200903
Michal Vaskofc2cd072021-02-24 13:17:17 +0100904 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
905 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
906 return LY_EVALID;
907 }
908
909 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200910 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200911
912 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100913 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200914 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200915 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916 }
917
Michal Vaskofc2cd072021-02-24 13:17:17 +0100918 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200919 e->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100920 e->prefix_data = ctx->parsed_mod;
Radek Krejciab430862021-03-02 20:13:40 +0100921 e->parent_stmt = insubstmt;
922 e->parent_stmt_index = insubstmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100923
Radek Krejci33090f92020-12-17 20:12:46 +0100924 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
925 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200926 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927 return ret;
928}
929
Michal Vaskoea5abea2018-09-18 13:10:54 +0200930/**
931 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
932 * description, etc...
933 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200934 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200935 * @param[in] substmt Type of this substatement.
936 * @param[in] substmt_index Index of this substatement.
937 * @param[in,out] value Place to store the parsed value.
938 * @param[in] arg Type of the YANG keyword argument (of the value).
939 * @param[in,out] exts Extension instances to add to.
940 *
941 * @return LY_ERR values.
942 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200943static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100944parse_text_field(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200945 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200946{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100947 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200948 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200949 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200950 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951
952 if (*value) {
Radek Krejci3972b332021-03-02 16:34:31 +0100953 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200954 return LY_EVALID;
955 }
956
957 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100958 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200959
960 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200961 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200962
Radek Krejci33090f92020-12-17 20:12:46 +0100963 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200965 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100966 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200967 break;
968 default:
Radek Krejci3972b332021-03-02 16:34:31 +0100969 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200970 return LY_EVALID;
971 }
972 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973 return ret;
974}
975
Michal Vaskoea5abea2018-09-18 13:10:54 +0200976/**
977 * @brief Parse the yang-version statement.
978 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200979 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100980 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200981 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200982 *
983 * @return LY_ERR values.
984 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200985static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100986parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100988 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200990 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200991 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100993 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200994 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995 return LY_EVALID;
996 }
997
998 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100999 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001000
Radek Krejci96e48da2020-09-04 13:18:06 +02001001 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001002 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001003 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001004 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001005 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001006 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007 free(buf);
1008 return LY_EVALID;
1009 }
1010 free(buf);
1011
Radek Krejci33090f92020-12-17 20:12:46 +01001012 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001013 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001014 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001015 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001016 break;
1017 default:
David Sedlákb3077192019-06-19 10:55:37 +02001018 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001019 return LY_EVALID;
1020 }
1021 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001022 return ret;
1023}
1024
Michal Vaskoea5abea2018-09-18 13:10:54 +02001025/**
1026 * @brief Parse the belongs-to statement.
1027 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001028 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001029 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1030 * @param[in,out] exts Extension instances to add to.
1031 *
1032 * @return LY_ERR values.
1033 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001034static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001035parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001037 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001038 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001039 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001040 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001041
Michal Vaskoc3781c32020-10-06 14:04:08 +02001042 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001043 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001044 return LY_EVALID;
1045 }
1046
Michal Vaskoc3781c32020-10-06 14:04:08 +02001047 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001048 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001049 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001050 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Submodule \"belongs-to\" value \"%.*s\" does not match its module name \"%s\".",
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001051 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001052 free(buf);
1053 return LY_EVALID;
1054 }
1055 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001056
Radek Krejci33090f92020-12-17 20:12:46 +01001057 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001059 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001060 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001062 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001063 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_BELONGS_TO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064 break;
1065 default:
David Sedlákb3077192019-06-19 10:55:37 +02001066 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067 return LY_EVALID;
1068 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001070 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001071checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001072 /* mandatory substatements */
1073 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001074 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075 return LY_EVALID;
1076 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001077 return ret;
1078}
1079
Michal Vaskoea5abea2018-09-18 13:10:54 +02001080/**
1081 * @brief Parse the revision-date statement.
1082 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001083 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001084 * @param[in,out] rev Array to store the parsed value in.
1085 * @param[in,out] exts Extension instances to add to.
1086 *
1087 * @return LY_ERR values.
1088 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001090parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001091{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001092 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001093 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001094 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001095 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001096
1097 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001098 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001099 return LY_EVALID;
1100 }
1101
1102 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001103 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104
1105 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001106 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 free(buf);
1108 return LY_EVALID;
1109 }
1110
1111 /* store value and spend buf if allocated */
1112 strncpy(rev, word, word_len);
1113 free(buf);
1114
Radek Krejci33090f92020-12-17 20:12:46 +01001115 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001117 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001118 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001119 break;
1120 default:
David Sedlákb3077192019-06-19 10:55:37 +02001121 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001122 return LY_EVALID;
1123 }
1124 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001125 return ret;
1126}
1127
Michal Vaskoea5abea2018-09-18 13:10:54 +02001128/**
1129 * @brief Parse the include statement.
1130 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001131 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001132 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001133 * @param[in,out] includes Parsed includes to add to.
1134 *
1135 * @return LY_ERR values.
1136 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001138parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139{
Radek Krejcid33273d2018-10-25 14:55:52 +02001140 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001142 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001143 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144 struct lysp_include *inc;
1145
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001146 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147
1148 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001149 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150
Radek Krejci011e4aa2020-09-04 15:22:31 +02001151 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001152
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001153 /* submodules share the namespace with the module names, so there must not be
1154 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001155 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001156 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001157 return LY_EVALID;
1158 }
1159
Radek Krejci33090f92020-12-17 20:12:46 +01001160 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001162 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001163 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001164 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 +02001165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001166 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001167 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001168 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 +02001169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001170 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001171 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001173 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001174 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175 break;
1176 default:
David Sedlákb3077192019-06-19 10:55:37 +02001177 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178 return LY_EVALID;
1179 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181 return ret;
1182}
1183
Michal Vaskoea5abea2018-09-18 13:10:54 +02001184/**
1185 * @brief Parse the import statement.
1186 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001187 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001188 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001189 * @param[in,out] imports Parsed imports to add to.
1190 *
1191 * @return LY_ERR values.
1192 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001193static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001194parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001196 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001198 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001199 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200 struct lysp_import *imp;
1201
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001202 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001203
1204 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001205 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001206 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001207
Radek Krejci33090f92020-12-17 20:12:46 +01001208 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001209 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001210 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001211 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 +01001212 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 +02001213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001214 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001215 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001216 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 +02001217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001218 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001219 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001220 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 +02001221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001222 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001223 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001225 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001226 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227 break;
1228 default:
David Sedlákb3077192019-06-19 10:55:37 +02001229 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230 return LY_EVALID;
1231 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001232 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001233 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001234checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001235 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001236 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237
1238 return ret;
1239}
1240
Michal Vaskoea5abea2018-09-18 13:10:54 +02001241/**
1242 * @brief Parse the revision statement.
1243 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001244 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001245 * @param[in,out] revs Parsed revisions to add to.
1246 *
1247 * @return LY_ERR values.
1248 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001250parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001252 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001253 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001254 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001255 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001256 struct lysp_revision *rev;
1257
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001258 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001259
1260 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001261 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001262
1263 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001264 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001265 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266 return LY_EVALID;
1267 }
1268
Radek Krejcib7db73a2018-10-24 14:18:40 +02001269 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001270 free(buf);
1271
Radek Krejci33090f92020-12-17 20:12:46 +01001272 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001273 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001274 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001275 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 +02001276 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001277 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001278 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 +02001279 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001280 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001281 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001282 break;
1283 default:
David Sedlákb3077192019-06-19 10:55:37 +02001284 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001285 return LY_EVALID;
1286 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001287 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001288 return ret;
1289}
1290
Michal Vaskoea5abea2018-09-18 13:10:54 +02001291/**
1292 * @brief Parse a generic text field that can have more instances such as base.
1293 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001294 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001295 * @param[in] substmt Type of this substatement.
1296 * @param[in,out] texts Parsed values to add to.
1297 * @param[in] arg Type of the expected argument.
1298 * @param[in,out] exts Extension instances to add to.
1299 *
1300 * @return LY_ERR values.
1301 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001302static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001303parse_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 +02001304 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001305{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001306 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001307 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001308 const char **item;
1309 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001310 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001311
1312 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001313 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314
1315 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001316 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001317
Radek Krejci011e4aa2020-09-04 15:22:31 +02001318 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Radek Krejci33090f92020-12-17 20:12:46 +01001319 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001320 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001321 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001322 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001323 break;
1324 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001325 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001326 return LY_EVALID;
1327 }
1328 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329 return ret;
1330}
1331
Michal Vaskoea5abea2018-09-18 13:10:54 +02001332/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001333 * @brief Parse a generic text field that can have more instances such as base.
1334 *
1335 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001336 * @param[in] substmt Type of this substatement.
1337 * @param[in,out] qnames Parsed qnames to add to.
1338 * @param[in] arg Type of the expected argument.
1339 * @param[in,out] exts Extension instances to add to.
1340 *
1341 * @return LY_ERR values.
1342 */
1343static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001344parse_qnames(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001345 enum yang_arg arg, struct lysp_ext_instance **exts)
1346{
1347 LY_ERR ret = LY_SUCCESS;
1348 char *buf, *word;
1349 struct lysp_qname *item;
1350 size_t word_len;
1351 enum ly_stmt kw;
1352
1353 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001354 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001355
1356 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001357 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001358
1359 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001360 item->mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001361 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001362 switch (kw) {
1363 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001364 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001365 break;
1366 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001367 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001368 return LY_EVALID;
1369 }
1370 }
1371 return ret;
1372}
1373
1374/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001375 * @brief Parse the config statement.
1376 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001377 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001378 * @param[in,out] flags Flags to add to.
1379 * @param[in,out] exts Extension instances to add to.
1380 *
1381 * @return LY_ERR values.
1382 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001384parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001385{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001386 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001387 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001388 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001389 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001390
1391 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001392 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001393 return LY_EVALID;
1394 }
1395
1396 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001397 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001398
Radek Krejcif13b87b2020-12-01 22:02:17 +01001399 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001400 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001401 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001402 *flags |= LYS_CONFIG_R;
1403 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001404 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001405 free(buf);
1406 return LY_EVALID;
1407 }
1408 free(buf);
1409
Radek Krejci33090f92020-12-17 20:12:46 +01001410 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001411 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001412 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001413 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001414 break;
1415 default:
David Sedlákb3077192019-06-19 10:55:37 +02001416 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417 return LY_EVALID;
1418 }
1419 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420 return ret;
1421}
1422
Michal Vaskoea5abea2018-09-18 13:10:54 +02001423/**
1424 * @brief Parse the mandatory statement.
1425 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001426 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001427 * @param[in,out] flags Flags to add to.
1428 * @param[in,out] exts Extension instances to add to.
1429 *
1430 * @return LY_ERR values.
1431 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001432static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001433parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001435 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001436 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001437 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001438 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001439
1440 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001441 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001442 return LY_EVALID;
1443 }
1444
1445 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001446 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001447
Radek Krejcif13b87b2020-12-01 22:02:17 +01001448 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001450 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001451 *flags |= LYS_MAND_FALSE;
1452 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001453 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001454 free(buf);
1455 return LY_EVALID;
1456 }
1457 free(buf);
1458
Radek Krejci33090f92020-12-17 20:12:46 +01001459 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001460 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001461 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001462 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463 break;
1464 default:
David Sedlákb3077192019-06-19 10:55:37 +02001465 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466 return LY_EVALID;
1467 }
1468 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001469 return ret;
1470}
1471
Michal Vaskoea5abea2018-09-18 13:10:54 +02001472/**
1473 * @brief Parse a restriction such as range or length.
1474 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001475 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001476 * @param[in] restr_kw Type of this particular restriction.
1477 * @param[in,out] exts Extension instances to add to.
1478 *
1479 * @return LY_ERR values.
1480 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001482parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001484 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001485 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001486 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001487 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488
1489 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001490 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491
Michal Vaskob36053d2020-03-26 15:49:30 +01001492 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001493 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001494 restr->arg.mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001495 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001497 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001498 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 +02001499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001500 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001501 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 +02001502 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001503 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01001504 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 +02001505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001506 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001507 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 +02001508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001509 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001510 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001511 break;
1512 default:
David Sedlákb3077192019-06-19 10:55:37 +02001513 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 return LY_EVALID;
1515 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 return ret;
1518}
1519
Michal Vaskoea5abea2018-09-18 13:10:54 +02001520/**
1521 * @brief Parse a restriction that can have more instances such as must.
1522 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001523 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001524 * @param[in] restr_kw Type of this particular restriction.
1525 * @param[in,out] restrs Restrictions to add to.
1526 *
1527 * @return LY_ERR values.
1528 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001529static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001530parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531{
1532 struct lysp_restr *restr;
1533
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001534 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001535 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536}
1537
Michal Vaskoea5abea2018-09-18 13:10:54 +02001538/**
1539 * @brief Parse the status statement.
1540 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001541 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001542 * @param[in,out] flags Flags to add to.
1543 * @param[in,out] exts Extension instances to add to.
1544 *
1545 * @return LY_ERR values.
1546 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001548parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001549{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001550 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001552 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001553 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554
1555 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001556 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001557 return LY_EVALID;
1558 }
1559
1560 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001561 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001562
Radek Krejcif13b87b2020-12-01 22:02:17 +01001563 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001565 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001566 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001567 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 *flags |= LYS_STATUS_OBSLT;
1569 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001570 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 free(buf);
1572 return LY_EVALID;
1573 }
1574 free(buf);
1575
Radek Krejci33090f92020-12-17 20:12:46 +01001576 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001578 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001579 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 break;
1581 default:
David Sedlákb3077192019-06-19 10:55:37 +02001582 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001583 return LY_EVALID;
1584 }
1585 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 return ret;
1587}
1588
Michal Vaskoea5abea2018-09-18 13:10:54 +02001589/**
1590 * @brief Parse the when statement.
1591 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001592 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001593 * @param[in,out] when_p When pointer to parse to.
1594 *
1595 * @return LY_ERR values.
1596 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001597LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001598parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001599{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001600 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001601 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001602 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001603 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604 struct lysp_when *when;
1605
1606 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001607 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001608 return LY_EVALID;
1609 }
1610
1611 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001612 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001613 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001614
1615 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001616 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001617 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001618 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001619
Radek Krejci33090f92020-12-17 20:12:46 +01001620 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001622 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001623 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 +02001624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001626 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 +02001627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001628 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001629 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_WHEN, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001630 break;
1631 default:
David Sedlákb3077192019-06-19 10:55:37 +02001632 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 return LY_EVALID;
1634 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001635 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 return ret;
1637}
1638
Michal Vaskoea5abea2018-09-18 13:10:54 +02001639/**
1640 * @brief Parse the anydata or anyxml statement.
1641 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001642 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001643 * @param[in] any_kw Type of this particular keyword.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001644 * @param[in,out] siblings Siblings to add to.
1645 *
1646 * @return LY_ERR values.
1647 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001648LY_ERR
Radek Krejci39b7fc22021-02-26 23:29:18 +01001649parse_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 +02001650{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001651 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001652 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001653 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001654 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001655 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001656
David Sedlák60adc092019-08-06 15:57:02 +02001657 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001658 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001659
Radek Krejci39b7fc22021-02-26 23:29:18 +01001660 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001661 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001662
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001664 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001665 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666
1667 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01001668 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001669 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001670 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001671 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001673 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001674 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 +02001675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001676 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001677 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001679 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001680 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001682 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001683 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001684 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001685 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001686 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 +02001687 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001688 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001689 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001691 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001692 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001694 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001695 LY_CHECK_RET(parse_ext(ctx, word, word_len, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696 break;
1697 default:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001698 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 return LY_EVALID;
1700 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001701 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001702 return ret;
1703}
1704
Michal Vaskoea5abea2018-09-18 13:10:54 +02001705/**
1706 * @brief Parse the value or position statement. Substatement of type enum statement.
1707 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001708 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001709 * @param[in] val_kw Type of this particular keyword.
1710 * @param[in,out] value Value to write to.
1711 * @param[in,out] flags Flags to write to.
1712 * @param[in,out] exts Extension instances to add to.
1713 *
1714 * @return LY_ERR values.
1715 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001716LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001717parse_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 +02001718 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001719{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001720 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001721 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001722 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001723 long int num = 0;
1724 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001725 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001726
1727 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001728 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001729 return LY_EVALID;
1730 }
1731 *flags |= LYS_SET_VALUE;
1732
1733 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001734 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735
Radek Krejcid6b76452019-09-03 17:03:03 +02001736 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 +02001737 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001738 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739 }
1740
1741 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001742 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001743 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001744 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001745 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001746 goto error;
1747 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001748 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001749 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001750 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001751 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001752 goto error;
1753 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754 }
1755 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001756 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001757 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001758 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759 }
1760 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001761 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001762 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001763 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001764 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 *value = num;
1766 } else {
1767 *value = unum;
1768 }
1769 free(buf);
1770
Radek Krejci33090f92020-12-17 20:12:46 +01001771 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001773 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001774 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 +02001775 break;
1776 default:
David Sedlákb3077192019-06-19 10:55:37 +02001777 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001778 return LY_EVALID;
1779 }
1780 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001781 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001782
1783error:
1784 free(buf);
1785 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001786}
1787
Michal Vaskoea5abea2018-09-18 13:10:54 +02001788/**
1789 * @brief Parse the enum or bit statement. Substatement of type statement.
1790 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001791 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001792 * @param[in] enum_kw Type of this particular keyword.
1793 * @param[in,out] enums Enums or bits to add to.
1794 *
1795 * @return LY_ERR values.
1796 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001798parse_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 +02001799{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001800 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001801 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001802 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001803 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001804 struct lysp_type_enum *enm;
1805
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001806 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001807
1808 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001809 LY_CHECK_RET(get_argument(ctx, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejcid6b76452019-09-03 17:03:03 +02001810 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001811 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001812 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001813 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001814
Radek Krejci011e4aa2020-09-04 15:22:31 +02001815 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001816 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1817
Radek Krejci33090f92020-12-17 20:12:46 +01001818 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001820 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001821 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 +02001822 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001823 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001824 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001825 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001826 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001827 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001828 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 +02001829 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001830 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001831 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001832 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001833 case LY_STMT_VALUE:
1834 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 +02001835 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001836 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001837 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001838 case LY_STMT_POSITION:
1839 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 +02001840 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001841 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001842 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001843 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001844 LY_CHECK_RET(parse_ext(ctx, word, word_len, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001845 break;
1846 default:
David Sedlákb3077192019-06-19 10:55:37 +02001847 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 return LY_EVALID;
1849 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001850 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001851 return ret;
1852}
1853
Michal Vaskoea5abea2018-09-18 13:10:54 +02001854/**
1855 * @brief Parse the fraction-digits statement. Substatement of type statement.
1856 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001857 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001858 * @param[in,out] fracdig Value to write to.
1859 * @param[in,out] exts Extension instances to add to.
1860 *
1861 * @return LY_ERR values.
1862 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001863static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001864parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001865{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001866 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001868 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001869 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001870 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871
1872 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001873 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874 return LY_EVALID;
1875 }
1876
1877 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001878 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879
1880 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001881 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001882 free(buf);
1883 return LY_EVALID;
1884 }
1885
1886 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001887 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001889 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001890 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891 free(buf);
1892 return LY_EVALID;
1893 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001894 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001895 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 free(buf);
1897 return LY_EVALID;
1898 }
1899 *fracdig = num;
1900 free(buf);
1901
Radek Krejci33090f92020-12-17 20:12:46 +01001902 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001903 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001904 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001905 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FRACTION_DIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906 break;
1907 default:
David Sedlákb3077192019-06-19 10:55:37 +02001908 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001909 return LY_EVALID;
1910 }
1911 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001912 return ret;
1913}
1914
Michal Vaskoea5abea2018-09-18 13:10:54 +02001915/**
1916 * @brief Parse the require-instance statement. Substatement of type statement.
1917 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001918 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001919 * @param[in,out] reqinst Value to write to.
1920 * @param[in,out] flags Flags to write to.
1921 * @param[in,out] exts Extension instances to add to.
1922 *
1923 * @return LY_ERR values.
1924 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001926parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001927 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001929 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001930 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001931 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001932 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001933
1934 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001935 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001936 return LY_EVALID;
1937 }
1938 *flags |= LYS_SET_REQINST;
1939
1940 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001941 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001942
Radek Krejcif13b87b2020-12-01 22:02:17 +01001943 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001944 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001945 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001946 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001947 free(buf);
1948 return LY_EVALID;
1949 }
1950 free(buf);
1951
Radek Krejci33090f92020-12-17 20:12:46 +01001952 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001954 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001955 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REQUIRE_INSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956 break;
1957 default:
David Sedlákb3077192019-06-19 10:55:37 +02001958 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001959 return LY_EVALID;
1960 }
1961 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962 return ret;
1963}
1964
Michal Vaskoea5abea2018-09-18 13:10:54 +02001965/**
1966 * @brief Parse the modifier statement. Substatement of type pattern statement.
1967 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001968 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001969 * @param[in,out] pat Value to write to.
1970 * @param[in,out] exts Extension instances to add to.
1971 *
1972 * @return LY_ERR values.
1973 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001974static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001975parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001977 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001979 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001980 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981
Radek Krejcif13b87b2020-12-01 22:02:17 +01001982 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001983 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984 return LY_EVALID;
1985 }
1986
1987 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001988 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989
Radek Krejcif13b87b2020-12-01 22:02:17 +01001990 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001991 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 free(buf);
1993 return LY_EVALID;
1994 }
1995 free(buf);
1996
1997 /* replace the value in the dictionary */
1998 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001999 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002000 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002001 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002002
Radek Krejcif13b87b2020-12-01 22:02:17 +01002003 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2004 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002005 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006
Radek Krejci33090f92020-12-17 20:12:46 +01002007 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002009 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002010 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 break;
2012 default:
David Sedlákb3077192019-06-19 10:55:37 +02002013 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 return LY_EVALID;
2015 }
2016 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017 return ret;
2018}
2019
Michal Vaskoea5abea2018-09-18 13:10:54 +02002020/**
2021 * @brief Parse the pattern statement. Substatement of type statement.
2022 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002023 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002024 * @param[in,out] patterns Restrictions to add to.
2025 *
2026 * @return LY_ERR values.
2027 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002029parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002031 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002032 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002033 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002034 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002035 struct lysp_restr *restr;
2036
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002037 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038
2039 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002040 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041
2042 /* add special meaning first byte */
2043 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002044 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045 word = buf;
2046 } else {
2047 buf = malloc(word_len + 2);
2048 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002049 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002050 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002051 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002052 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002053 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
2054 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055
Radek Krejci33090f92020-12-17 20:12:46 +01002056 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002057 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002058 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002059 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 +02002060 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002061 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002062 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 +02002063 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002064 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01002065 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 +02002066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002067 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002068 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 +02002069 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002070 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002071 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002072 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002073 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002074 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002075 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076 break;
2077 default:
David Sedlákb3077192019-06-19 10:55:37 +02002078 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002079 return LY_EVALID;
2080 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 return ret;
2083}
2084
Michal Vaskoea5abea2018-09-18 13:10:54 +02002085/**
2086 * @brief Parse the type statement.
2087 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002088 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002089 * @param[in,out] type Type to wrote to.
2090 *
2091 * @return LY_ERR values.
2092 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002093static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002094parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002096 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002097 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002098 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002099 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002100 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002101 struct lysp_type *nest_type;
2102
2103 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002104 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105 return LY_EVALID;
2106 }
2107
2108 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002109 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002110 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002111
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002112 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002113 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002114
Radek Krejci33090f92020-12-17 20:12:46 +01002115 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002117 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002118 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 +01002119 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002121 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002122 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002123 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002125 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002126 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002127 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002128 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002129 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002130 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002131 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002133 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002135 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002136 return LY_EVALID;
2137 }
2138 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002139 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002140
Radek Krejci33090f92020-12-17 20:12:46 +01002141 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002142 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002144 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002145 if (type->path) {
Radek Krejci3972b332021-03-02 16:34:31 +01002146 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002147 return LY_EVALID;
2148 }
2149
Radek Krejcifc596f92021-02-26 22:40:26 +01002150 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002151 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
Michal Vasko69730152020-10-09 16:30:07 +02002152 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002153 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002154 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002155 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002157 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002158 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002159 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002161 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002163 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002164 return LY_EVALID;
2165 }
2166 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002167 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168
Radek Krejci33090f92020-12-17 20:12:46 +01002169 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002170 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002172 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002173 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002174 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002176 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002177 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002178 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002179 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002181 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002182 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 break;
2184 default:
David Sedlákb3077192019-06-19 10:55:37 +02002185 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 return LY_EVALID;
2187 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 return ret;
2190}
2191
Michal Vaskoea5abea2018-09-18 13:10:54 +02002192/**
2193 * @brief Parse the leaf statement.
2194 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002195 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002196 * @param[in,out] siblings Siblings to add to.
2197 *
2198 * @return LY_ERR values.
2199 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002200LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002201parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002203 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002204 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002205 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002206 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002207 struct lysp_node_leaf *leaf;
2208
David Sedlák60adc092019-08-06 15:57:02 +02002209 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002210 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002212 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002213
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002215 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002216 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002217
2218 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002219 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002221 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002222 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002223 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002224 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002225 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002226 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002228 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002229 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 +02002230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002231 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002232 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002234 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002235 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002237 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002238 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002240 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002241 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 +02002242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002243 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002244 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002246 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002247 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002249 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002250 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 +02002251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002252 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002253 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002255 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002256 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002257 break;
2258 default:
David Sedlákb3077192019-06-19 10:55:37 +02002259 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 return LY_EVALID;
2261 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002262 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002263 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002264checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002265 /* mandatory substatements */
2266 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002267 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002268 return LY_EVALID;
2269 }
2270
2271 return ret;
2272}
2273
Michal Vaskoea5abea2018-09-18 13:10:54 +02002274/**
2275 * @brief Parse the max-elements statement.
2276 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002277 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002278 * @param[in,out] max Value to write to.
2279 * @param[in,out] flags Flags to write to.
2280 * @param[in,out] exts Extension instances to add to.
2281 *
2282 * @return LY_ERR values.
2283 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002284LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002285parse_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 +02002286{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002287 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002289 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002291 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002292
2293 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002294 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002295 return LY_EVALID;
2296 }
2297 *flags |= LYS_SET_MAX;
2298
2299 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002300 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301
2302 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002303 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304 free(buf);
2305 return LY_EVALID;
2306 }
2307
Radek Krejci7f9b6512019-09-18 13:11:09 +02002308 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002310 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002312 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002313 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314 free(buf);
2315 return LY_EVALID;
2316 }
2317 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002318 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319 free(buf);
2320 return LY_EVALID;
2321 }
2322
2323 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002324 } else {
2325 /* unbounded */
2326 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002327 }
2328 free(buf);
2329
Radek Krejci33090f92020-12-17 20:12:46 +01002330 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002332 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002333 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MAX_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 break;
2335 default:
David Sedlákb3077192019-06-19 10:55:37 +02002336 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337 return LY_EVALID;
2338 }
2339 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 return ret;
2341}
2342
Michal Vaskoea5abea2018-09-18 13:10:54 +02002343/**
2344 * @brief Parse the min-elements statement.
2345 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002346 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002347 * @param[in,out] min Value to write to.
2348 * @param[in,out] flags Flags to write to.
2349 * @param[in,out] exts Extension instances to add to.
2350 *
2351 * @return LY_ERR values.
2352 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002353LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002354parse_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 +02002355{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002356 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002358 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002360 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361
2362 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002363 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 return LY_EVALID;
2365 }
2366 *flags |= LYS_SET_MIN;
2367
2368 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002369 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370
2371 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002372 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002373 free(buf);
2374 return LY_EVALID;
2375 }
2376
2377 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002378 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002379 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002380 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002381 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382 free(buf);
2383 return LY_EVALID;
2384 }
2385 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002386 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002387 free(buf);
2388 return LY_EVALID;
2389 }
2390 *min = num;
2391 free(buf);
2392
Radek Krejci33090f92020-12-17 20:12:46 +01002393 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002395 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002396 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MIN_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 break;
2398 default:
David Sedlákb3077192019-06-19 10:55:37 +02002399 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 return LY_EVALID;
2401 }
2402 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 return ret;
2404}
2405
Michal Vaskoea5abea2018-09-18 13:10:54 +02002406/**
2407 * @brief Parse the ordered-by statement.
2408 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002409 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002410 * @param[in,out] flags Flags to write to.
2411 * @param[in,out] exts Extension instances to add to.
2412 *
2413 * @return LY_ERR values.
2414 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002416parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002417{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002418 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002420 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002421 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422
2423 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002424 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002425 return LY_EVALID;
2426 }
2427
2428 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002429 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002430
Radek Krejcif13b87b2020-12-01 22:02:17 +01002431 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002433 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002434 *flags |= LYS_ORDBY_USER;
2435 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002436 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437 free(buf);
2438 return LY_EVALID;
2439 }
2440 free(buf);
2441
Radek Krejci33090f92020-12-17 20:12:46 +01002442 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002444 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002445 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ORDERED_BY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 break;
2447 default:
David Sedlákb3077192019-06-19 10:55:37 +02002448 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 return LY_EVALID;
2450 }
2451 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 return ret;
2453}
2454
Michal Vaskoea5abea2018-09-18 13:10:54 +02002455/**
2456 * @brief Parse the leaf-list statement.
2457 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002458 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002459 * @param[in,out] siblings Siblings to add to.
2460 *
2461 * @return LY_ERR values.
2462 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002463LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002464parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002466 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002467 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002468 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002469 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002470 struct lysp_node_leaflist *llist;
2471
David Sedlák60adc092019-08-06 15:57:02 +02002472 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002473 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002474 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002475 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002476
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002478 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002479 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480
2481 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002482 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002484 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002485 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002486 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002487 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002488 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002489 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002491 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002492 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 +02002493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002494 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002495 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002496 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002497 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002498 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002500 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002501 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002503 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002504 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002506 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002507 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002509 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002510 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 +02002511 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002512 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002513 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002514 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002515 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002516 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002518 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002519 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 +02002520 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002521 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002522 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002524 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002525 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002526 break;
2527 default:
David Sedlákb3077192019-06-19 10:55:37 +02002528 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002529 return LY_EVALID;
2530 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002532 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002533checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 /* mandatory substatements */
2535 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002536 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 return LY_EVALID;
2538 }
2539
2540 return ret;
2541}
2542
Michal Vaskoea5abea2018-09-18 13:10:54 +02002543/**
2544 * @brief Parse the refine statement.
2545 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002546 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002547 * @param[in,out] refines Refines to add to.
2548 *
2549 * @return LY_ERR values.
2550 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002551static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002552parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002553{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002554 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002556 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002557 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 struct lysp_refine *rf;
2559
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002560 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002561
2562 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002563 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002564 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002565 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002566
Radek Krejci33090f92020-12-17 20:12:46 +01002567 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002568 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002569 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002570 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002572 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002573 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002575 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002576 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 +02002577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002578 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002579 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002580 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002582 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002583 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002585 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002586 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002588 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002589 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002591 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002592 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002594 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002595 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 +02002596 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002597 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002598 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 +02002599 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002600 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002601 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002602 break;
2603 default:
David Sedlákb3077192019-06-19 10:55:37 +02002604 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002605 return LY_EVALID;
2606 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608 return ret;
2609}
2610
Michal Vaskoea5abea2018-09-18 13:10:54 +02002611/**
2612 * @brief Parse the typedef statement.
2613 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002614 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002615 * @param[in,out] typedefs Typedefs to add to.
2616 *
2617 * @return LY_ERR values.
2618 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002619static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002620parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002622 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002624 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 struct lysp_tpdf *tpdf;
2627
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002628 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629
2630 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002631 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002632 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633
2634 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002635 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002637 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002638 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002639 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002641 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002642 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 +02002643 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002644 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002645 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 +02002646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002647 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002648 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002650 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002651 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002653 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002654 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 +02002655 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002656 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002657 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658 break;
2659 default:
David Sedlákb3077192019-06-19 10:55:37 +02002660 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 return LY_EVALID;
2662 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002664 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002665checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 /* mandatory substatements */
2667 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002668 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 return LY_EVALID;
2670 }
2671
Radek Krejcibbe09a92018-11-08 09:36:54 +01002672 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002673 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002674 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002675 }
2676
Michal Vasko7fbc8162018-09-17 10:35:16 +02002677 return ret;
2678}
2679
Michal Vaskoea5abea2018-09-18 13:10:54 +02002680/**
2681 * @brief Parse the input or output statement.
2682 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002683 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002684 * @param[in] kw Type of this particular keyword
2685 * @param[in,out] inout_p Input/output pointer to write to.
2686 *
2687 * @return LY_ERR values.
2688 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002690parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002691 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002693 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002695 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002696 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002697 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002698
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002699 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002700 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002701 return LY_EVALID;
2702 }
2703
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002704 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002705 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2706 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002707 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002708
2709 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002710 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002711 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002712 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002713 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002714 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002715 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002716 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002717 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002718 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002719 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002721 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002722 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002723 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002724 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002725 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002726 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002727 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002728 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002730 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002731 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002733 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002734 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002736 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002737 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002739 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002740 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002741 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002742 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002743 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002744 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002746 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002747 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002748 break;
2749 default:
David Sedlákb3077192019-06-19 10:55:37 +02002750 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751 return LY_EVALID;
2752 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002753 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002754 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002755
Radek Krejci7fc68292019-06-12 13:51:09 +02002756checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002757 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002758 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2759 return LY_EVALID;
2760 }
2761
Michal Vasko7fbc8162018-09-17 10:35:16 +02002762 return ret;
2763}
2764
Michal Vaskoea5abea2018-09-18 13:10:54 +02002765/**
2766 * @brief Parse the action statement.
2767 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002768 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002769 * @param[in,out] actions Actions to add to.
2770 *
2771 * @return LY_ERR values.
2772 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002773LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002774parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002776 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002777 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002778 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002779 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002780 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002781
Radek Krejci2a9fc652021-01-22 17:44:34 +01002782 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002783
2784 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002785 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002786 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002787 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002788 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002789
Radek Krejci33090f92020-12-17 20:12:46 +01002790 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002791 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002792 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002793 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 +02002794 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002795 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002796 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002798 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002799 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 +02002800 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002801 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002802 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002803 break;
2804
Radek Krejcid6b76452019-09-03 17:03:03 +02002805 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002806 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002807 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002808 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002809 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002810 break;
2811
Radek Krejcid6b76452019-09-03 17:03:03 +02002812 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002813 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002815 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002816 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002818 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002819 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 +02002820 break;
2821 default:
David Sedlákb3077192019-06-19 10:55:37 +02002822 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823 return LY_EVALID;
2824 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002825 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002826 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002827
Radek Krejci2a9fc652021-01-22 17:44:34 +01002828checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002829 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2830 if (!act->input.nodetype) {
2831 act->input.nodetype = LYS_INPUT;
2832 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002833 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002834 }
2835 if (!act->output.nodetype) {
2836 act->output.nodetype = LYS_OUTPUT;
2837 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002838 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002839 }
2840
Michal Vasko7fbc8162018-09-17 10:35:16 +02002841 return ret;
2842}
2843
Michal Vaskoea5abea2018-09-18 13:10:54 +02002844/**
2845 * @brief Parse the notification statement.
2846 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002847 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002848 * @param[in,out] notifs Notifications to add to.
2849 *
2850 * @return LY_ERR values.
2851 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002852LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002853parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002855 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002857 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002858 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002859 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002860
Radek Krejci2a9fc652021-01-22 17:44:34 +01002861 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862
2863 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002864 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002865 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002866 notif->nodetype = LYS_NOTIF;
2867 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002868
Radek Krejci2a9fc652021-01-22 17:44:34 +01002869 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002871 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002872 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 +02002873 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002874 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002875 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002877 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002878 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 +02002879 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002880 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002881 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002882 break;
2883
Radek Krejcid6b76452019-09-03 17:03:03 +02002884 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002885 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002886 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002887 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002888 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002890 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002891 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002893 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002894 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002896 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002897 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002899 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002900 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002902 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002903 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002905 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002906 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 break;
2908
Radek Krejcid6b76452019-09-03 17:03:03 +02002909 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002910 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002911 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002912 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002913 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002914 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002916 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002917 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002918 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002919 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002920 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 break;
2922 default:
David Sedlákb3077192019-06-19 10:55:37 +02002923 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002924 return LY_EVALID;
2925 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002926 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002927
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 return ret;
2929}
2930
Michal Vaskoea5abea2018-09-18 13:10:54 +02002931/**
2932 * @brief Parse the grouping statement.
2933 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002934 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002935 * @param[in,out] groupings Groupings to add to.
2936 *
2937 * @return LY_ERR values.
2938 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002939LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002940parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002942 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002944 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002945 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002946 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002947
Radek Krejci2a9fc652021-01-22 17:44:34 +01002948 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949
2950 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002951 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002952 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002953 grp->nodetype = LYS_GROUPING;
2954 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955
Radek Krejci2a9fc652021-01-22 17:44:34 +01002956 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002958 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002959 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 +02002960 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002961 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002962 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 +02002963 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002964 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002965 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002966 break;
2967
Radek Krejcid6b76452019-09-03 17:03:03 +02002968 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002969 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002970 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002971 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002972 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002974 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002975 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002977 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002978 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002979 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002980 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002981 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002983 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002984 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002986 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002987 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002988 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002989 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002990 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 break;
2992
Radek Krejcid6b76452019-09-03 17:03:03 +02002993 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002994 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002996 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002997 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002998 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002999 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003000 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003001 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003002 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003003 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003004 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003005 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003007 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003008 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 break;
3010 default:
David Sedlákb3077192019-06-19 10:55:37 +02003011 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 return LY_EVALID;
3013 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003015
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 return ret;
3017}
3018
Michal Vaskoea5abea2018-09-18 13:10:54 +02003019/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003020 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003021 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003022 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003023 * @param[in,out] augments Augments to add to.
3024 *
3025 * @return LY_ERR values.
3026 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003027LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003028parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003029{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003030 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003031 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003032 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003033 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003034 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035
Radek Krejci2a9fc652021-01-22 17:44:34 +01003036 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003037
3038 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003039 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003040 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003041 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003042 aug->nodetype = LYS_AUGMENT;
3043 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003044
Radek Krejci2a9fc652021-01-22 17:44:34 +01003045 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003046 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003047 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003048 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 +02003049 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003050 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003051 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003053 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003054 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 +02003055 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003056 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003057 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003059 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003060 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061 break;
3062
Radek Krejcid6b76452019-09-03 17:03:03 +02003063 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003064 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003065 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003066 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003067 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003068 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003069 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003070 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003072 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003073 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003074 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003075 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003076 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003078 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003079 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003080 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003081 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003082 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003084 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003085 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003087 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003088 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 break;
3090
Radek Krejcid6b76452019-09-03 17:03:03 +02003091 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003092 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003093 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003094 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003095 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003096 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003097 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003099 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003100 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 break;
3102 default:
David Sedlákb3077192019-06-19 10:55:37 +02003103 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104 return LY_EVALID;
3105 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003106 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003107
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108 return ret;
3109}
3110
Michal Vaskoea5abea2018-09-18 13:10:54 +02003111/**
3112 * @brief Parse the uses statement.
3113 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003114 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003115 * @param[in,out] siblings Siblings to add to.
3116 *
3117 * @return LY_ERR values.
3118 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003119LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003120parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003122 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003124 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003125 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 struct lysp_node_uses *uses;
3127
David Sedlák60adc092019-08-06 15:57:02 +02003128 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003129 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003131 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003134 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003135 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003136
3137 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003138 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003140 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003141 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 +02003142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003143 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003144 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003146 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003147 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 +02003148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003149 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003150 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003152 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003153 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 break;
3155
Radek Krejcid6b76452019-09-03 17:03:03 +02003156 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003157 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003159 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003160 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003162 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003163 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164 break;
3165 default:
David Sedlákb3077192019-06-19 10:55:37 +02003166 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167 return LY_EVALID;
3168 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003169 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003170
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 return ret;
3172}
3173
Michal Vaskoea5abea2018-09-18 13:10:54 +02003174/**
3175 * @brief Parse the case statement.
3176 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003177 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003178 * @param[in,out] siblings Siblings to add to.
3179 *
3180 * @return LY_ERR values.
3181 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003182LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003183parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003184{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003185 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003186 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003187 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003188 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003189 struct lysp_node_case *cas;
3190
David Sedlák60adc092019-08-06 15:57:02 +02003191 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003192 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003193 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003194 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003197 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003198 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199
3200 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003201 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003203 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003204 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 +02003205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003206 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003207 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003209 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003210 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 +02003211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003212 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003213 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003216 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 break;
3218
Radek Krejcid6b76452019-09-03 17:03:03 +02003219 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003220 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003221 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003222 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003223 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003225 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003226 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003228 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003229 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003231 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003232 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003234 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003235 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003237 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003238 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003240 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003241 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003243 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003244 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 break;
3246 default:
David Sedlákb3077192019-06-19 10:55:37 +02003247 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 return LY_EVALID;
3249 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003250 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 return ret;
3252}
3253
Michal Vaskoea5abea2018-09-18 13:10:54 +02003254/**
3255 * @brief Parse the choice statement.
3256 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003257 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003258 * @param[in,out] siblings Siblings to add to.
3259 *
3260 * @return LY_ERR values.
3261 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003262LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003263parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003265 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003267 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003268 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003269 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270
David Sedlák60adc092019-08-06 15:57:02 +02003271 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003272 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003273 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003274 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003277 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003278 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279
3280 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003281 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003282 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003283 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003284 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003286 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003287 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 +02003288 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003289 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003290 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003291 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003292 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003293 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003295 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003296 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 +02003297 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003298 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003299 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003301 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003302 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003303 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003304 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01003305 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 +02003306 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003307 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308 break;
3309
Radek Krejcid6b76452019-09-03 17:03:03 +02003310 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003311 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003312 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003313 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003314 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003316 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003317 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003319 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003320 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003321 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003323 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003324 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003326 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003327 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003328 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003329 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003330 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003332 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003333 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003335 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003336 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 break;
3338 default:
David Sedlákb3077192019-06-19 10:55:37 +02003339 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340 return LY_EVALID;
3341 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 return ret;
3344}
3345
Michal Vaskoea5abea2018-09-18 13:10:54 +02003346/**
3347 * @brief Parse the container statement.
3348 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003349 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003350 * @param[in,out] siblings Siblings to add to.
3351 *
3352 * @return LY_ERR values.
3353 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003354LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003355parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356{
3357 LY_ERR ret = 0;
3358 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003359 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003360 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 struct lysp_node_container *cont;
3362
David Sedlák60adc092019-08-06 15:57:02 +02003363 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003364 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003366 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003369 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003370 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371
3372 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003373 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003374 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003375 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003376 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003378 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003379 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 +02003380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003381 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003382 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003384 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003385 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 +02003386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003387 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003388 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003390 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003391 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003393 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003394 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 +02003395 break;
3396
Radek Krejcid6b76452019-09-03 17:03:03 +02003397 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003398 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003399 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003400 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003401 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003403 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003404 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003406 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003407 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003409 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003410 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003412 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003413 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003415 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003416 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003418 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003419 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
3421
Radek Krejcid6b76452019-09-03 17:03:03 +02003422 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003423 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003425 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003426 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003429 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003430 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003432 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003433 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003435 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003436 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003437 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003439 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003440 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 break;
3442 default:
David Sedlákb3077192019-06-19 10:55:37 +02003443 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 return LY_EVALID;
3445 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003447
Michal Vasko7fbc8162018-09-17 10:35:16 +02003448 return ret;
3449}
3450
Michal Vaskoea5abea2018-09-18 13:10:54 +02003451/**
3452 * @brief Parse the list statement.
3453 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003454 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003455 * @param[in,out] siblings Siblings to add to.
3456 *
3457 * @return LY_ERR values.
3458 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003459LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003460parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003462 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003463 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003464 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003465 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003466 struct lysp_node_list *list;
3467
David Sedlák60adc092019-08-06 15:57:02 +02003468 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003469 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003470 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003471 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003474 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003475 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476
3477 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003478 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003479 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003480 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003481 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003482 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003483 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003484 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 +02003485 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003486 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003487 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003488 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003489 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003490 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 +02003491 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003492 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003493 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003495 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003496 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003498 case LY_STMT_KEY:
Radek Krejcifc596f92021-02-26 22:40:26 +01003499 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 +02003500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003501 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003502 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003504 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003505 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003507 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003508 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003510 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003511 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512 break;
3513
Radek Krejcid6b76452019-09-03 17:03:03 +02003514 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003515 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003516 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003517 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003518 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003519 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003520 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003521 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003523 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003524 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003527 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003529 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003530 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003532 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003533 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003535 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003536 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 break;
3538
Radek Krejcid6b76452019-09-03 17:03:03 +02003539 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003540 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003542 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003543 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003545 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003546 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003547 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003549 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003550 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003552 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003553 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003554 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003556 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003557 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 break;
3559 default:
David Sedlákb3077192019-06-19 10:55:37 +02003560 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003561 return LY_EVALID;
3562 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003564
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565 return ret;
3566}
3567
Michal Vaskoea5abea2018-09-18 13:10:54 +02003568/**
3569 * @brief Parse the yin-element statement.
3570 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003571 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003572 * @param[in,out] flags Flags to write to.
3573 * @param[in,out] exts Extension instances to add to.
3574 *
3575 * @return LY_ERR values.
3576 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003578parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003580 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003582 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003583 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584
3585 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003586 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587 return LY_EVALID;
3588 }
3589
3590 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003591 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592
Radek Krejcif13b87b2020-12-01 22:02:17 +01003593 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003595 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596 *flags |= LYS_YINELEM_FALSE;
3597 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003598 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 free(buf);
3600 return LY_EVALID;
3601 }
3602 free(buf);
3603
Radek Krejci33090f92020-12-17 20:12:46 +01003604 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003605 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003606 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003607 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003608 LY_CHECK_RET(ret);
3609 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003610 default:
David Sedlákb3077192019-06-19 10:55:37 +02003611 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003612 return LY_EVALID;
3613 }
3614 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615 return ret;
3616}
3617
Michal Vaskoea5abea2018-09-18 13:10:54 +02003618/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003619 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003620 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003621 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003622 * @param[in,out] argument Value to write to.
3623 * @param[in,out] flags Flags to write to.
3624 * @param[in,out] exts Extension instances to add to.
3625 *
3626 * @return LY_ERR values.
3627 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003629parse_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 +02003630{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003631 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003633 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003634 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635
3636 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003637 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 return LY_EVALID;
3639 }
3640
3641 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003642 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003643 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644
Radek Krejci33090f92020-12-17 20:12:46 +01003645 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003647 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003648 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003650 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003651 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652 break;
3653 default:
David Sedlákb3077192019-06-19 10:55:37 +02003654 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003655 return LY_EVALID;
3656 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658 return ret;
3659}
3660
Michal Vaskoea5abea2018-09-18 13:10:54 +02003661/**
3662 * @brief Parse the extension statement.
3663 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003664 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003665 * @param[in,out] extensions Extensions to add to.
3666 *
3667 * @return LY_ERR values.
3668 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003670parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003672 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003674 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003675 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676 struct lysp_ext *ex;
3677
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003678 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679
3680 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003681 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003682 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003683
Radek Krejci33090f92020-12-17 20:12:46 +01003684 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003686 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003687 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 +02003688 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003689 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003690 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 +02003691 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003692 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003693 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003695 case LY_STMT_ARGUMENT:
Radek Krejci9f87b0c2021-03-05 14:45:26 +01003696 LY_CHECK_RET(parse_argument(ctx, &ex->argname, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003698 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003699 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003700 break;
3701 default:
David Sedlákb3077192019-06-19 10:55:37 +02003702 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003703 return LY_EVALID;
3704 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003705 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003706 return ret;
3707}
3708
Michal Vaskoea5abea2018-09-18 13:10:54 +02003709/**
3710 * @brief Parse the deviate statement.
3711 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003712 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003713 * @param[in,out] deviates Deviates to add to.
3714 *
3715 * @return LY_ERR values.
3716 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003717LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003718parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003720 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003721 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003722 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003723 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003724 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003725 struct lysp_deviate_add *d_add = NULL;
3726 struct lysp_deviate_rpl *d_rpl = NULL;
3727 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003728 const char **d_units = NULL;
3729 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003730 struct lysp_restr **d_musts = NULL;
3731 uint16_t *d_flags = 0;
3732 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733
3734 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003735 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736
Radek Krejcif13b87b2020-12-01 22:02:17 +01003737 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003738 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003739 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003740 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003741 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003743 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003744 dev_mod = LYS_DEV_DELETE;
3745 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003746 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747 free(buf);
3748 return LY_EVALID;
3749 }
3750 free(buf);
3751
3752 /* create structure */
3753 switch (dev_mod) {
3754 case LYS_DEV_NOT_SUPPORTED:
3755 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003756 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 break;
3758 case LYS_DEV_ADD:
3759 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003760 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 d = (struct lysp_deviate *)d_add;
3762 d_units = &d_add->units;
3763 d_uniques = &d_add->uniques;
3764 d_dflts = &d_add->dflts;
3765 d_musts = &d_add->musts;
3766 d_flags = &d_add->flags;
3767 d_min = &d_add->min;
3768 d_max = &d_add->max;
3769 break;
3770 case LYS_DEV_REPLACE:
3771 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003772 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003773 d = (struct lysp_deviate *)d_rpl;
3774 d_units = &d_rpl->units;
3775 d_flags = &d_rpl->flags;
3776 d_min = &d_rpl->min;
3777 d_max = &d_rpl->max;
3778 break;
3779 case LYS_DEV_DELETE:
3780 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003781 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003782 d = (struct lysp_deviate *)d_del;
3783 d_units = &d_del->units;
3784 d_uniques = &d_del->uniques;
3785 d_dflts = &d_del->dflts;
3786 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003787 break;
3788 default:
3789 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003790 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791 }
3792 d->mod = dev_mod;
3793
3794 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003795 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003796
Radek Krejci33090f92020-12-17 20:12:46 +01003797 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003799 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003800 switch (dev_mod) {
3801 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003802 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003803 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003804 return LY_EVALID;
3805 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003806 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003807 break;
3808 }
3809 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003810 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003811 switch (dev_mod) {
3812 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003813 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 return LY_EVALID;
3815 case LYS_DEV_REPLACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003816 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003817 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003818 break;
3819 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003820 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003821 break;
3822 }
3823 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003824 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003825 switch (dev_mod) {
3826 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003827 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003828 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 return LY_EVALID;
3830 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003831 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 break;
3833 }
3834 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003835 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003836 switch (dev_mod) {
3837 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003838 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003839 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003840 return LY_EVALID;
3841 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003842 LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003843 break;
3844 }
3845 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003846 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847 switch (dev_mod) {
3848 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003849 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003850 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003851 return LY_EVALID;
3852 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003853 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003854 break;
3855 }
3856 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003857 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858 switch (dev_mod) {
3859 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003860 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003861 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862 return LY_EVALID;
3863 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003864 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865 break;
3866 }
3867 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003868 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003869 switch (dev_mod) {
3870 case LYS_DEV_NOT_SUPPORTED:
3871 case LYS_DEV_ADD:
3872 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 Krejci2c02f3e2018-10-16 10:54:38 +02003876 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003877 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003878 return LY_EVALID;
3879 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003881 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003882 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003883 break;
3884 }
3885 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003886 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 switch (dev_mod) {
3888 case LYS_DEV_NOT_SUPPORTED:
3889 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003890 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003891 return LY_EVALID;
3892 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003893 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894 break;
3895 }
3896 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003897 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 switch (dev_mod) {
3899 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003900 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 return LY_EVALID;
3902 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003903 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 +02003904 break;
3905 }
3906 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003907 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003908 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATE, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003909 break;
3910 default:
David Sedlákb3077192019-06-19 10:55:37 +02003911 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 return LY_EVALID;
3913 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003915 return ret;
3916}
3917
Michal Vaskoea5abea2018-09-18 13:10:54 +02003918/**
3919 * @brief Parse the deviation statement.
3920 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003921 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003922 * @param[in,out] deviations Deviations to add to.
3923 *
3924 * @return LY_ERR values.
3925 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003926LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003927parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003929 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003931 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003932 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933 struct lysp_deviation *dev;
3934
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003935 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936
3937 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003938 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003939 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003940 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003941
Radek Krejci33090f92020-12-17 20:12:46 +01003942 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003943 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003944 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003945 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 +02003946 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003947 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003948 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003950 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003951 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 +02003952 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003953 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003954 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATION, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955 break;
3956 default:
David Sedlákb3077192019-06-19 10:55:37 +02003957 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003958 return LY_EVALID;
3959 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003961 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003962checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003963 /* mandatory substatements */
3964 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003965 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003966 return LY_EVALID;
3967 }
3968
3969 return ret;
3970}
3971
Michal Vaskoea5abea2018-09-18 13:10:54 +02003972/**
3973 * @brief Parse the feature statement.
3974 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003975 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003976 * @param[in,out] features Features to add to.
3977 *
3978 * @return LY_ERR values.
3979 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003980LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003981parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003982{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003983 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003985 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003986 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 struct lysp_feature *feat;
3988
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003989 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990
3991 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003992 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003993 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003994
Radek Krejci33090f92020-12-17 20:12:46 +01003995 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003997 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003998 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 +02003999 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004000 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004001 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004002 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004003 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004004 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 +02004005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004006 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004007 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004008 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004009 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004010 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011 break;
4012 default:
David Sedlákb3077192019-06-19 10:55:37 +02004013 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004014 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004015 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017 return ret;
4018}
4019
Michal Vaskoea5abea2018-09-18 13:10:54 +02004020/**
4021 * @brief Parse the identity statement.
4022 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004023 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004024 * @param[in,out] identities Identities to add to.
4025 *
4026 * @return LY_ERR values.
4027 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004028LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004029parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004030{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004031 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004032 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004033 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004034 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004035 struct lysp_ident *ident;
4036
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004037 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004038
4039 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004040 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004041 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004042
Radek Krejci33090f92020-12-17 20:12:46 +01004043 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004044 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004045 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004046 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 +02004047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004048 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004049 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004050 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004051 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004052 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004053 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 +02004054 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004055 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004056 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004058 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004059 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004060 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 +01004061 return LY_EVALID;
4062 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004063 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 +02004064 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004065 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004066 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004067 break;
4068 default:
David Sedlákb3077192019-06-19 10:55:37 +02004069 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004070 return LY_EVALID;
4071 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004072 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073 return ret;
4074}
4075
Michal Vaskoea5abea2018-09-18 13:10:54 +02004076/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004077 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004078 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004079 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004080 * @param[in,out] mod Module to write to.
4081 *
4082 * @return LY_ERR values.
4083 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004084LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004085parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004086{
4087 LY_ERR ret = 0;
4088 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004089 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004090 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004091 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004092 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093
Michal Vaskoc3781c32020-10-06 14:04:08 +02004094 mod->is_submod = 0;
4095
4096 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004097 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004098 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004099
Radek Krejci33090f92020-12-17 20:12:46 +01004100 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004101
Radek Krejcie3846472018-10-15 15:24:51 +02004102#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004103 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 +02004104
Michal Vasko7fbc8162018-09-17 10:35:16 +02004105 switch (kw) {
4106 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004107 case LY_STMT_NAMESPACE:
4108 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004109 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4110 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004111 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004112 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004113 break;
4114 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004115 case LY_STMT_INCLUDE:
4116 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004117 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004118 break;
4119 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004120 case LY_STMT_ORGANIZATION:
4121 case LY_STMT_CONTACT:
4122 case LY_STMT_DESCRIPTION:
4123 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004124 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004125 break;
4126
4127 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004128 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004129 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004130 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004132 case LY_STMT_ANYDATA:
4133 case LY_STMT_ANYXML:
4134 case LY_STMT_AUGMENT:
4135 case LY_STMT_CHOICE:
4136 case LY_STMT_CONTAINER:
4137 case LY_STMT_DEVIATION:
4138 case LY_STMT_EXTENSION:
4139 case LY_STMT_FEATURE:
4140 case LY_STMT_GROUPING:
4141 case LY_STMT_IDENTITY:
4142 case LY_STMT_LEAF:
4143 case LY_STMT_LEAF_LIST:
4144 case LY_STMT_LIST:
4145 case LY_STMT_NOTIFICATION:
4146 case LY_STMT_RPC:
4147 case LY_STMT_TYPEDEF:
4148 case LY_STMT_USES:
4149 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004150 mod_stmt = Y_MOD_BODY;
4151 break;
4152 default:
4153 /* error handled in the next switch */
4154 break;
4155 }
Radek Krejcie3846472018-10-15 15:24:51 +02004156#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004157
Radek Krejcie3846472018-10-15 15:24:51 +02004158 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 switch (kw) {
4160 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004161 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004162 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004164 case LY_STMT_NAMESPACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004165 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 +02004166 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004167 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01004168 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 +02004169 break;
4170
4171 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004172 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004173 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004175 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004176 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004177 break;
4178
4179 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004180 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004181 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 +02004182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004183 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004184 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 +02004185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004186 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004187 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 +02004188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004189 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004190 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 +02004191 break;
4192
4193 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004194 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004195 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 break;
4197
4198 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004199 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004200 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004201 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004202 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004203 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004205 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004206 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004208 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004209 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004211 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004212 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004214 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004215 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004217 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004218 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004220 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004221 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004222 break;
4223
Radek Krejcid6b76452019-09-03 17:03:03 +02004224 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004225 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004226 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004227 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004228 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004230 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004231 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004232 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004233 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004234 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004236 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004237 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004239 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004240 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004242 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004243 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004245 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004246 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004247 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004248 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004249 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004250 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004251 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004252 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004253 break;
4254
4255 default:
David Sedlákb3077192019-06-19 10:55:37 +02004256 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004257 return LY_EVALID;
4258 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004259 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004260 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004261
Radek Krejci6d6556c2018-11-08 09:37:45 +01004262checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004263 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004264 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004265 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004266 return LY_EVALID;
4267 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004268 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004269 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004270 }
4271
Radek Krejcie9e987e2018-10-31 12:50:27 +01004272 /* submodules share the namespace with the module names, so there must not be
4273 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004274 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004275 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004276 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004277 return LY_EVALID;
4278 }
4279
4280 return ret;
4281}
4282
4283/**
4284 * @brief Parse submodule substatements.
4285 *
4286 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004287 * @param[out] submod Parsed submodule structure.
4288 *
4289 * @return LY_ERR values.
4290 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004291LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004292parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004293{
4294 LY_ERR ret = 0;
4295 char *buf, *word;
4296 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004297 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004298 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004299 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004300
Michal Vaskoc3781c32020-10-06 14:04:08 +02004301 submod->is_submod = 1;
4302
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004303 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004304 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004305 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004306
Radek Krejci33090f92020-12-17 20:12:46 +01004307 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004308
4309#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004310 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 +01004311
4312 switch (kw) {
4313 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004314 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004315 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4316 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004317 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004318 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4319 break;
4320 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004321 case LY_STMT_INCLUDE:
4322 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004323 CHECK_ORDER(Y_MOD_LINKAGE);
4324 break;
4325 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004326 case LY_STMT_ORGANIZATION:
4327 case LY_STMT_CONTACT:
4328 case LY_STMT_DESCRIPTION:
4329 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004330 CHECK_ORDER(Y_MOD_META);
4331 break;
4332
4333 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004334 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004335 CHECK_ORDER(Y_MOD_REVISION);
4336 break;
4337 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_ANYDATA:
4339 case LY_STMT_ANYXML:
4340 case LY_STMT_AUGMENT:
4341 case LY_STMT_CHOICE:
4342 case LY_STMT_CONTAINER:
4343 case LY_STMT_DEVIATION:
4344 case LY_STMT_EXTENSION:
4345 case LY_STMT_FEATURE:
4346 case LY_STMT_GROUPING:
4347 case LY_STMT_IDENTITY:
4348 case LY_STMT_LEAF:
4349 case LY_STMT_LEAF_LIST:
4350 case LY_STMT_LIST:
4351 case LY_STMT_NOTIFICATION:
4352 case LY_STMT_RPC:
4353 case LY_STMT_TYPEDEF:
4354 case LY_STMT_USES:
4355 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004356 mod_stmt = Y_MOD_BODY;
4357 break;
4358 default:
4359 /* error handled in the next switch */
4360 break;
4361 }
4362#undef CHECK_ORDER
4363
4364 prev_kw = kw;
4365 switch (kw) {
4366 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004367 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004368 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004370 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004371 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004372 break;
4373
4374 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004376 if (submod->version == LYS_VERSION_1_1) {
4377 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4378 submod->name);
4379 }
Radek Krejci33090f92020-12-17 20:12:46 +01004380 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004382 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004383 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004384 break;
4385
4386 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004388 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 +01004389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004390 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004391 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 +01004392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004393 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004394 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 +01004395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004396 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004397 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 +01004398 break;
4399
4400 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004401 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004402 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004403 break;
4404
4405 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004406 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004407 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004408 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004409 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004410 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004412 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004413 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004415 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004416 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004418 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004419 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004421 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004422 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004423 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004424 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004425 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004426 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004427 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004428 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004429 break;
4430
Radek Krejcid6b76452019-09-03 17:03:03 +02004431 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004432 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004434 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004435 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004437 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004438 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004440 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004441 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004443 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004444 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004446 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004447 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004449 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004450 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004452 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004453 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004455 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004456 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004458 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004459 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004460 break;
4461
4462 default:
David Sedlákb3077192019-06-19 10:55:37 +02004463 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004464 return LY_EVALID;
4465 }
4466 }
4467 LY_CHECK_RET(ret);
4468
4469checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004470 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004471 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004472 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004473 return LY_EVALID;
4474 }
4475
4476 /* submodules share the namespace with the module names, so there must not be
4477 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004478 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004479 /* main modules may have different revisions */
4480 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004481 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004482 return LY_EVALID;
4483 }
4484
Michal Vasko7fbc8162018-09-17 10:35:16 +02004485 return ret;
4486}
4487
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004488/**
4489 * @brief Skip any redundant characters, namely whitespaces and comments.
4490 *
4491 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004492 * @return LY_SUCCESS on success.
4493 * @return LY_EVALID on invalid comment.
4494 */
4495static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004496skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004497{
4498 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004499 while (ctx->in->current[0]) {
4500 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004501 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004502 ly_in_skip(ctx->in, 2);
4503 LY_CHECK_RET(skip_comment(ctx, 1));
4504 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004505 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004506 ly_in_skip(ctx->in, 2);
4507 LY_CHECK_RET(skip_comment(ctx, 2));
4508 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004509 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004510 if (ctx->in->current[0] == '\n') {
4511 LY_IN_NEW_LINE(ctx->in);
4512 }
Radek Krejci33090f92020-12-17 20:12:46 +01004513 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004514 } else {
4515 break;
4516 }
4517 }
4518
4519 return LY_SUCCESS;
4520}
4521
Radek Krejcid4557c62018-09-17 11:42:09 +02004522LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004523yang_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 +02004524 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004525{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004526 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004527 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004528 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004529 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004530 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004531
David Sedlák1b623122019-08-05 15:27:49 +02004532 /* create context */
4533 *context = calloc(1, sizeof **context);
4534 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004535 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004536 (*context)->unres = main_ctx->unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004537 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004538
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004539 mod_p = calloc(1, sizeof *mod_p);
4540 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4541 mod_p->mod = main_ctx->parsed_mod->mod;
4542 mod_p->parsing = 1;
4543 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4544
Radek Krejciddace2c2021-01-08 11:30:56 +01004545 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004546
David Sedlák1b623122019-08-05 15:27:49 +02004547 /* map the typedefs and groupings list from main context to the submodule's context */
4548 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4549 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4550
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004551 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004552 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004553 LY_CHECK_GOTO(ret, cleanup);
4554
Michal Vasko7fbc8162018-09-17 10:35:16 +02004555 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004556 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004557 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004558
Radek Krejcid6b76452019-09-03 17:03:03 +02004559 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004560 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004561 ret = LY_EINVAL;
4562 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004563 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004564 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004565 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004566 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004567 }
4568
Michal Vasko7fbc8162018-09-17 10:35:16 +02004569 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004570 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004571 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004572
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004573 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004574 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004575 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004576 if (in->current[0]) {
4577 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004578 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004579 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004580 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004581
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004582 mod_p->parsing = 0;
4583 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004584
Radek Krejcibbe09a92018-11-08 09:36:54 +01004585cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004586 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004587 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004588 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004589 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004590 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004591 }
4592
4593 return ret;
4594}
4595
4596LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004597yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod, struct lys_glob_unres *unres)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004598{
4599 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004600 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004601 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004602 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004603 struct lysp_module *mod_p = NULL;
4604
David Sedlák1b623122019-08-05 15:27:49 +02004605 /* create context */
4606 *context = calloc(1, sizeof **context);
4607 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004608 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004609 (*context)->unres = unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004610 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004611
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004612 mod_p = calloc(1, sizeof *mod_p);
4613 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4614 mod_p->mod = mod;
4615 mod_p->parsing = 1;
4616 (*context)->parsed_mod = mod_p;
4617
Radek Krejciddace2c2021-01-08 11:30:56 +01004618 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004619
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004620 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004621 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004622 LY_CHECK_GOTO(ret, cleanup);
4623
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004624 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004625 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004626 LY_CHECK_GOTO(ret, cleanup);
4627
Radek Krejcid6b76452019-09-03 17:03:03 +02004628 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004629 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 +01004630 ret = LY_EINVAL;
4631 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004632 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004633 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004634 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004635 goto cleanup;
4636 }
4637
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004638 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004639 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004640 LY_CHECK_GOTO(ret, cleanup);
4641
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004642 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004643 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004644 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004645 if (in->current[0]) {
4646 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004647 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004648 goto cleanup;
4649 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004650
4651 mod_p->parsing = 0;
4652 mod->parsed = mod_p;
4653
4654cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004655 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004656 if (ret) {
4657 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004658 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004659 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004660 }
4661
Michal Vasko7fbc8162018-09-17 10:35:16 +02004662 return ret;
4663}