blob: a2b6701ac2824f489acc9714ce74f270a2d7da43 [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.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200544 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QOUTED - see [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200545 * @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 +0200546 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
547 * set to NULL. Otherwise equal to \p word_p.
548 * @param[out] word_len Length of the read string.
549 *
550 * @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{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200556 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200557 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200558
Michal Vasko7fbc8162018-09-17 10:35:16 +0200559 /* word buffer - dynamically allocated */
560 *word_b = NULL;
561
562 /* word pointer - just a pointer to data */
563 *word_p = NULL;
564
565 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100566 while (ctx->in->current[0]) {
567 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200568 case '\'':
569 case '\"':
570 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200571 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100572 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200573 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200574 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200575 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200576 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100577 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200578 }
Radek Krejci33090f92020-12-17 20:12:46 +0100579 LY_CHECK_RET(read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200580 goto str_end;
581 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100582 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200583 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100584 MOVE_INPUT(ctx, 2);
585 LY_CHECK_RET(skip_comment(ctx, 1));
586 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200587 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100588 MOVE_INPUT(ctx, 2);
589 LY_CHECK_RET(skip_comment(ctx, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200590 } else {
591 /* not a comment after all */
Radek Krejci33090f92020-12-17 20:12:46 +0100592 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200593 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200594 break;
595 case ' ':
596 if (*word_len) {
597 /* word is finished */
598 goto str_end;
599 }
Radek Krejci33090f92020-12-17 20:12:46 +0100600 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200601 break;
602 case '\t':
603 if (*word_len) {
604 /* word is finished */
605 goto str_end;
606 }
607 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100608 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200609
Radek Krejci33090f92020-12-17 20:12:46 +0100610 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200611 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200612 case '\r':
613 if (ctx->in->current[1] != '\n') {
614 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
615 return LY_EVALID;
616 }
617 MOVE_INPUT(ctx, 1);
618 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200619 case '\n':
620 if (*word_len) {
621 /* word is finished */
622 goto str_end;
623 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100624 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100625 MOVE_INPUT(ctx, 1);
626
Michal Vasko7fbc8162018-09-17 10:35:16 +0200627 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200628 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200629 break;
630 case ';':
631 case '{':
632 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
633 /* word is finished */
634 goto str_end;
635 }
636
Radek Krejci33090f92020-12-17 20:12:46 +0100637 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200638 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200639 case '}':
640 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100641 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200642 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200643 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200644 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100645 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200646 break;
647 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200648 }
649
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200650 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200651 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200652 return LY_EVALID;
653
Michal Vasko7fbc8162018-09-17 10:35:16 +0200654str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200655 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200656 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200657 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200658 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200659 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200660 *word_p = *word_b;
661 }
662
663 return LY_SUCCESS;
664}
665
Michal Vaskoea5abea2018-09-18 13:10:54 +0200666/**
667 * @brief Get another YANG keyword from the raw data.
668 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200669 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200670 * @param[out] kw YANG keyword read.
671 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
672 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
673 *
674 * @return LY_ERR values.
675 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200676LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100677get_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 +0200678{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200679 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200680 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200681 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200682
683 if (word_p) {
684 *word_p = NULL;
685 *word_len = 0;
686 }
687
688 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100689 while (ctx->in->current[0]) {
690 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200691 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100692 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200693 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100694 MOVE_INPUT(ctx, 2);
695 LY_CHECK_RET(skip_comment(ctx, 1));
696 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200697 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100698 MOVE_INPUT(ctx, 2);
699 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200700 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200701 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200702 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200703 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200704 }
Radek Krejci13028282019-06-11 14:56:48 +0200705 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200706 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200707 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100708 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200709 ctx->indent = 0;
710 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200711 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200712 /* skip whitespaces (optsep) */
713 ++ctx->indent;
714 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200716 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100717 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200718 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100719 case '\r':
720 /* possible CRLF endline */
721 if (ctx->in->current[1] == '\n') {
722 break;
723 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100724 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200725 default:
726 /* either a keyword start or an invalid character */
727 goto keyword_start;
728 }
729
Radek Krejci33090f92020-12-17 20:12:46 +0100730 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200731 }
732
733keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100734 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100735 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736
Michal Vasko69730152020-10-09 16:30:07 +0200737 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 +0200738 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200739 }
740
Radek Krejcid6b76452019-09-03 17:03:03 +0200741 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200742 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100743 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200744 case '\r':
745 if (ctx->in->current[1] != '\n') {
746 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
747 return LY_EVALID;
748 }
749 MOVE_INPUT(ctx, 1);
750 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200751 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200753 case ' ':
754 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200755 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200756 case ':':
757 /* keyword is not actually a keyword, but prefix of an extension.
758 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
759 * and we will be checking the keyword (extension instance) itself */
760 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100761 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200762 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200763 case '{':
764 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200765 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200766 break;
767 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100768 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200769 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100770 MOVE_INPUT(ctx, 1);
771 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200772 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200773 return LY_EVALID;
774 }
775 } else {
776 /* still can be an extension */
777 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200778
Radek Krejci156ccaf2018-10-15 15:49:17 +0200779extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100780 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200781 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
782 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200783 uint32_t c = 0;
784
Radek Krejci33090f92020-12-17 20:12:46 +0100785 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
786 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200787 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200788 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200789 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100790 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200791 }
Radek Krejci33090f92020-12-17 20:12:46 +0100792 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200793 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200794 return LY_EVALID;
795 }
796
797 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200798 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100799 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200800 return LY_EVALID;
801 }
802
Radek Krejcid6b76452019-09-03 17:03:03 +0200803 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200804 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200805
Radek Krejci626df482018-10-11 15:06:31 +0200806success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200807 if (word_p) {
808 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100809 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200810 }
811
812 return LY_SUCCESS;
813}
814
Michal Vaskoea5abea2018-09-18 13:10:54 +0200815/**
816 * @brief Parse extension instance substatements.
817 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200818 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200819 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200820 * @param[in] word Extension instance substatement name (keyword).
821 * @param[in] word_len Extension instance substatement name length.
822 * @param[in,out] child Children of this extension instance to add to.
823 *
824 * @return LY_ERR values.
825 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100827parse_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 +0200828 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200829{
830 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100831 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200832 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200833 struct lysp_stmt *stmt, *par_child;
834
835 stmt = calloc(1, sizeof *stmt);
836 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
837
Radek Krejcibb9b1982019-04-08 14:24:59 +0200838 /* insert into parent statements */
839 if (!*child) {
840 *child = stmt;
841 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200842 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200843 par_child->next = stmt;
844 }
845
Michal Vaskofc2cd072021-02-24 13:17:17 +0100846 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200847 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200848
849 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100850 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200851 if (word) {
852 if (buf) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200853 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200854 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200855 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200856 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200857 }
858
Michal Vaskofc2cd072021-02-24 13:17:17 +0100859 stmt->format = LY_PREF_SCHEMA;
860 stmt->prefix_data = ctx->parsed_mod;
861 stmt->kw = kw;
862
Radek Krejci33090f92020-12-17 20:12:46 +0100863 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, ) {
864 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200865 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200866 return ret;
867}
868
Michal Vaskoea5abea2018-09-18 13:10:54 +0200869/**
870 * @brief Parse extension instance.
871 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200872 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200873 * @param[in] ext_name Extension instance substatement name (keyword).
874 * @param[in] ext_name_len Extension instance substatement name length.
Radek Krejcifc596f92021-02-26 22:40:26 +0100875 * @param[in] insubstmt The statement this extension instance is a substatement of.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200876 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
877 * @param[in,out] exts Extension instances to add to.
878 *
879 * @return LY_ERR values.
880 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100882parse_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 +0200883 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200884{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100885 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200887 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200888 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200889 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200890
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200891 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892
Michal Vaskofc2cd072021-02-24 13:17:17 +0100893 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
894 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
895 return LY_EVALID;
896 }
897
898 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200899 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200900
901 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100902 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200903 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200904 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905 }
906
Michal Vaskofc2cd072021-02-24 13:17:17 +0100907 /* store the rest of information */
908 e->format = LY_PREF_SCHEMA;
909 e->prefix_data = ctx->parsed_mod;
Radek Krejciab430862021-03-02 20:13:40 +0100910 e->parent_stmt = insubstmt;
911 e->parent_stmt_index = insubstmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100912
Radek Krejci33090f92020-12-17 20:12:46 +0100913 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
914 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200915 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916 return ret;
917}
918
Michal Vaskoea5abea2018-09-18 13:10:54 +0200919/**
920 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
921 * description, etc...
922 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200923 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200924 * @param[in] substmt Type of this substatement.
925 * @param[in] substmt_index Index of this substatement.
926 * @param[in,out] value Place to store the parsed value.
927 * @param[in] arg Type of the YANG keyword argument (of the value).
928 * @param[in,out] exts Extension instances to add to.
929 *
930 * @return LY_ERR values.
931 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100933parse_text_field(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200934 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200935{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100936 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200937 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200938 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200939 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200940
941 if (*value) {
Radek Krejci3972b332021-03-02 16:34:31 +0100942 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200943 return LY_EVALID;
944 }
945
946 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100947 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200948
949 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200950 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951
Radek Krejci33090f92020-12-17 20:12:46 +0100952 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200953 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200954 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100955 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200956 break;
957 default:
Radek Krejci3972b332021-03-02 16:34:31 +0100958 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200959 return LY_EVALID;
960 }
961 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200962 return ret;
963}
964
Michal Vaskoea5abea2018-09-18 13:10:54 +0200965/**
966 * @brief Parse the yang-version statement.
967 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200968 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100969 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200970 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200971 *
972 * @return LY_ERR values.
973 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200974static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100975parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200976{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100977 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200978 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200979 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200980 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100982 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200983 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 return LY_EVALID;
985 }
986
987 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100988 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989
Radek Krejci96e48da2020-09-04 13:18:06 +0200990 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100991 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100992 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100993 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200994 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200995 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996 free(buf);
997 return LY_EVALID;
998 }
999 free(buf);
1000
Radek Krejci33090f92020-12-17 20:12:46 +01001001 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001002 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001003 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001004 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001005 break;
1006 default:
David Sedlákb3077192019-06-19 10:55:37 +02001007 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008 return LY_EVALID;
1009 }
1010 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011 return ret;
1012}
1013
Michal Vaskoea5abea2018-09-18 13:10:54 +02001014/**
1015 * @brief Parse the belongs-to statement.
1016 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001017 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001018 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1019 * @param[in,out] exts Extension instances to add to.
1020 *
1021 * @return LY_ERR values.
1022 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001024parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001026 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001027 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001028 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001029 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001030
Michal Vaskoc3781c32020-10-06 14:04:08 +02001031 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001032 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001033 return LY_EVALID;
1034 }
1035
Michal Vaskoc3781c32020-10-06 14:04:08 +02001036 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001037 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001038 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001039 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 +02001040 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001041 free(buf);
1042 return LY_EVALID;
1043 }
1044 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001045
Radek Krejci33090f92020-12-17 20:12:46 +01001046 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001047 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001048 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001049 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001050 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001051 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001052 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_BELONGS_TO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053 break;
1054 default:
David Sedlákb3077192019-06-19 10:55:37 +02001055 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056 return LY_EVALID;
1057 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001059 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001060checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 /* mandatory substatements */
1062 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001063 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064 return LY_EVALID;
1065 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001066 return ret;
1067}
1068
Michal Vaskoea5abea2018-09-18 13:10:54 +02001069/**
1070 * @brief Parse the revision-date statement.
1071 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001072 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001073 * @param[in,out] rev Array to store the parsed value in.
1074 * @param[in,out] exts Extension instances to add to.
1075 *
1076 * @return LY_ERR values.
1077 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001079parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001080{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001081 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001082 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001083 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001084 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001085
1086 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001087 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001088 return LY_EVALID;
1089 }
1090
1091 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001092 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001093
1094 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001095 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001096 free(buf);
1097 return LY_EVALID;
1098 }
1099
1100 /* store value and spend buf if allocated */
1101 strncpy(rev, word, word_len);
1102 free(buf);
1103
Radek Krejci33090f92020-12-17 20:12:46 +01001104 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001105 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001106 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001107 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001108 break;
1109 default:
David Sedlákb3077192019-06-19 10:55:37 +02001110 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001111 return LY_EVALID;
1112 }
1113 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001114 return ret;
1115}
1116
Michal Vaskoea5abea2018-09-18 13:10:54 +02001117/**
1118 * @brief Parse the include statement.
1119 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001120 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001121 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001122 * @param[in,out] includes Parsed includes to add to.
1123 *
1124 * @return LY_ERR values.
1125 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001127parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001128{
Radek Krejcid33273d2018-10-25 14:55:52 +02001129 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001131 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001132 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001133 struct lysp_include *inc;
1134
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001135 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136
1137 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001138 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139
Radek Krejci011e4aa2020-09-04 15:22:31 +02001140 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001141
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001142 /* submodules share the namespace with the module names, so there must not be
1143 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001144 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001145 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001146 return LY_EVALID;
1147 }
1148
Radek Krejci33090f92020-12-17 20:12:46 +01001149 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001151 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001152 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001153 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 +02001154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001155 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001156 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001157 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 +02001158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001159 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001160 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001162 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001163 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164 break;
1165 default:
David Sedlákb3077192019-06-19 10:55:37 +02001166 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001167 return LY_EVALID;
1168 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001170 return ret;
1171}
1172
Michal Vaskoea5abea2018-09-18 13:10:54 +02001173/**
1174 * @brief Parse the import statement.
1175 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001176 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001177 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001178 * @param[in,out] imports Parsed imports to add to.
1179 *
1180 * @return LY_ERR values.
1181 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001183parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001185 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001186 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001187 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001188 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189 struct lysp_import *imp;
1190
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001191 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192
1193 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001194 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001195 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196
Radek Krejci33090f92020-12-17 20:12:46 +01001197 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001199 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001200 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 +01001201 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 +02001202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001203 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001204 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001205 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 +02001206 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001207 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001208 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001209 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 +02001210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001211 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001212 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001214 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001215 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001216 break;
1217 default:
David Sedlákb3077192019-06-19 10:55:37 +02001218 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219 return LY_EVALID;
1220 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001222 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001223checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001224 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001225 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001226
1227 return ret;
1228}
1229
Michal Vaskoea5abea2018-09-18 13:10:54 +02001230/**
1231 * @brief Parse the revision statement.
1232 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001233 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001234 * @param[in,out] revs Parsed revisions to add to.
1235 *
1236 * @return LY_ERR values.
1237 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001238static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001239parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001241 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001243 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001244 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 struct lysp_revision *rev;
1246
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001247 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248
1249 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001250 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251
1252 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001253 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001254 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255 return LY_EVALID;
1256 }
1257
Radek Krejcib7db73a2018-10-24 14:18:40 +02001258 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001259 free(buf);
1260
Radek Krejci33090f92020-12-17 20:12:46 +01001261 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001262 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001263 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001264 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 +02001265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001266 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001267 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 +02001268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001269 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001270 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001271 break;
1272 default:
David Sedlákb3077192019-06-19 10:55:37 +02001273 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001274 return LY_EVALID;
1275 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001277 return ret;
1278}
1279
Michal Vaskoea5abea2018-09-18 13:10:54 +02001280/**
1281 * @brief Parse a generic text field that can have more instances such as base.
1282 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001283 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001284 * @param[in] substmt Type of this substatement.
1285 * @param[in,out] texts Parsed values to add to.
1286 * @param[in] arg Type of the expected argument.
1287 * @param[in,out] exts Extension instances to add to.
1288 *
1289 * @return LY_ERR values.
1290 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001291static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001292parse_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 +02001293 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001294{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001295 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001297 const char **item;
1298 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001299 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001300
1301 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001302 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303
1304 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001305 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001306
Radek Krejci011e4aa2020-09-04 15:22:31 +02001307 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Radek Krejci33090f92020-12-17 20:12:46 +01001308 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001309 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001310 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001311 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001312 break;
1313 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001314 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315 return LY_EVALID;
1316 }
1317 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318 return ret;
1319}
1320
Michal Vaskoea5abea2018-09-18 13:10:54 +02001321/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001322 * @brief Parse a generic text field that can have more instances such as base.
1323 *
1324 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001325 * @param[in] substmt Type of this substatement.
1326 * @param[in,out] qnames Parsed qnames to add to.
1327 * @param[in] arg Type of the expected argument.
1328 * @param[in,out] exts Extension instances to add to.
1329 *
1330 * @return LY_ERR values.
1331 */
1332static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001333parse_qnames(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001334 enum yang_arg arg, struct lysp_ext_instance **exts)
1335{
1336 LY_ERR ret = LY_SUCCESS;
1337 char *buf, *word;
1338 struct lysp_qname *item;
1339 size_t word_len;
1340 enum ly_stmt kw;
1341
1342 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001343 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001344
1345 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001346 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001347
1348 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001349 item->mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001350 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001351 switch (kw) {
1352 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001353 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001354 break;
1355 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001356 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001357 return LY_EVALID;
1358 }
1359 }
1360 return ret;
1361}
1362
1363/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001364 * @brief Parse the config statement.
1365 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001366 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001367 * @param[in,out] flags Flags to add to.
1368 * @param[in,out] exts Extension instances to add to.
1369 *
1370 * @return LY_ERR values.
1371 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001372static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001373parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001375 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001376 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001377 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001378 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001379
1380 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001381 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001382 return LY_EVALID;
1383 }
1384
1385 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001386 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001387
Radek Krejcif13b87b2020-12-01 22:02:17 +01001388 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001389 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001390 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001391 *flags |= LYS_CONFIG_R;
1392 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001393 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001394 free(buf);
1395 return LY_EVALID;
1396 }
1397 free(buf);
1398
Radek Krejci33090f92020-12-17 20:12:46 +01001399 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001400 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001401 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001402 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403 break;
1404 default:
David Sedlákb3077192019-06-19 10:55:37 +02001405 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001406 return LY_EVALID;
1407 }
1408 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001409 return ret;
1410}
1411
Michal Vaskoea5abea2018-09-18 13:10:54 +02001412/**
1413 * @brief Parse the mandatory statement.
1414 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001415 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001416 * @param[in,out] flags Flags to add to.
1417 * @param[in,out] exts Extension instances to add to.
1418 *
1419 * @return LY_ERR values.
1420 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001421static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001422parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001423{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001424 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001425 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001426 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001427 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001428
1429 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001430 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431 return LY_EVALID;
1432 }
1433
1434 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001435 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001436
Radek Krejcif13b87b2020-12-01 22:02:17 +01001437 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001438 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001439 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440 *flags |= LYS_MAND_FALSE;
1441 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001442 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443 free(buf);
1444 return LY_EVALID;
1445 }
1446 free(buf);
1447
Radek Krejci33090f92020-12-17 20:12:46 +01001448 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001450 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001451 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001452 break;
1453 default:
David Sedlákb3077192019-06-19 10:55:37 +02001454 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001455 return LY_EVALID;
1456 }
1457 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001458 return ret;
1459}
1460
Michal Vaskoea5abea2018-09-18 13:10:54 +02001461/**
1462 * @brief Parse a restriction such as range or length.
1463 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001464 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001465 * @param[in] restr_kw Type of this particular restriction.
1466 * @param[in,out] exts Extension instances to add to.
1467 *
1468 * @return LY_ERR values.
1469 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001470static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001471parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001473 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001474 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001475 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001476 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001477
1478 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001479 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001480
Michal Vaskob36053d2020-03-26 15:49:30 +01001481 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001482 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001483 restr->arg.mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001484 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001485 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001486 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001487 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 +02001488 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001489 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001490 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 +02001491 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001492 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01001493 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 +02001494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001495 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001496 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 +02001497 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001498 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001499 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500 break;
1501 default:
David Sedlákb3077192019-06-19 10:55:37 +02001502 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 return LY_EVALID;
1504 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001506 return ret;
1507}
1508
Michal Vaskoea5abea2018-09-18 13:10:54 +02001509/**
1510 * @brief Parse a restriction that can have more instances such as must.
1511 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001512 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001513 * @param[in] restr_kw Type of this particular restriction.
1514 * @param[in,out] restrs Restrictions to add to.
1515 *
1516 * @return LY_ERR values.
1517 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001518static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001519parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520{
1521 struct lysp_restr *restr;
1522
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001523 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001524 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001525}
1526
Michal Vaskoea5abea2018-09-18 13:10:54 +02001527/**
1528 * @brief Parse the status statement.
1529 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001530 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001531 * @param[in,out] flags Flags to add to.
1532 * @param[in,out] exts Extension instances to add to.
1533 *
1534 * @return LY_ERR values.
1535 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001537parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001539 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001540 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001541 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001542 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001543
1544 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001545 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546 return LY_EVALID;
1547 }
1548
1549 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001550 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551
Radek Krejcif13b87b2020-12-01 22:02:17 +01001552 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001554 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001556 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001557 *flags |= LYS_STATUS_OBSLT;
1558 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001559 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001560 free(buf);
1561 return LY_EVALID;
1562 }
1563 free(buf);
1564
Radek Krejci33090f92020-12-17 20:12:46 +01001565 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001566 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001567 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001568 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569 break;
1570 default:
David Sedlákb3077192019-06-19 10:55:37 +02001571 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572 return LY_EVALID;
1573 }
1574 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001575 return ret;
1576}
1577
Michal Vaskoea5abea2018-09-18 13:10:54 +02001578/**
1579 * @brief Parse the when statement.
1580 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001581 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001582 * @param[in,out] when_p When pointer to parse to.
1583 *
1584 * @return LY_ERR values.
1585 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001586LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001587parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001589 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001590 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001591 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001592 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001593 struct lysp_when *when;
1594
1595 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001596 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001597 return LY_EVALID;
1598 }
1599
1600 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001601 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001602 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603
1604 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001605 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001606 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001607 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001608
Radek Krejci33090f92020-12-17 20:12:46 +01001609 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001610 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001611 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001612 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 +02001613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001614 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001615 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 +02001616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001617 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001618 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_WHEN, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001619 break;
1620 default:
David Sedlákb3077192019-06-19 10:55:37 +02001621 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622 return LY_EVALID;
1623 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 return ret;
1626}
1627
Michal Vaskoea5abea2018-09-18 13:10:54 +02001628/**
1629 * @brief Parse the anydata or anyxml statement.
1630 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001631 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001632 * @param[in] any_kw Type of this particular keyword.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001633 * @param[in,out] siblings Siblings to add to.
1634 *
1635 * @return LY_ERR values.
1636 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001637LY_ERR
Radek Krejci39b7fc22021-02-26 23:29:18 +01001638parse_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 +02001639{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001640 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001642 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001643 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001644 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001645
David Sedlák60adc092019-08-06 15:57:02 +02001646 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001647 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001648
Radek Krejci39b7fc22021-02-26 23:29:18 +01001649 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001650 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001651
Michal Vasko7fbc8162018-09-17 10:35:16 +02001652 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001653 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001654 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001655
1656 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01001657 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001659 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001660 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001662 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001663 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 +02001664 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001665 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001666 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001668 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001669 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001670 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001671 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001672 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001674 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001675 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 +02001676 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001677 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001678 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001680 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001681 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001683 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001684 LY_CHECK_RET(parse_ext(ctx, word, word_len, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001685 break;
1686 default:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001687 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001688 return LY_EVALID;
1689 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001691 return ret;
1692}
1693
Michal Vaskoea5abea2018-09-18 13:10:54 +02001694/**
1695 * @brief Parse the value or position statement. Substatement of type enum statement.
1696 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001697 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001698 * @param[in] val_kw Type of this particular keyword.
1699 * @param[in,out] value Value to write to.
1700 * @param[in,out] flags Flags to write to.
1701 * @param[in,out] exts Extension instances to add to.
1702 *
1703 * @return LY_ERR values.
1704 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001705LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001706parse_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 +02001707 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001708{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001709 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001711 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001712 long int num = 0;
1713 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001714 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715
1716 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001717 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718 return LY_EVALID;
1719 }
1720 *flags |= LYS_SET_VALUE;
1721
1722 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001723 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724
Radek Krejcid6b76452019-09-03 17:03:03 +02001725 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 +02001726 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001727 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001728 }
1729
1730 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001731 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001732 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001733 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001734 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001735 goto error;
1736 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001738 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001739 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001740 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001741 goto error;
1742 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001743 }
1744 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001745 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001746 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001747 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001748 }
1749 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001750 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001751 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001752 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001753 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754 *value = num;
1755 } else {
1756 *value = unum;
1757 }
1758 free(buf);
1759
Radek Krejci33090f92020-12-17 20:12:46 +01001760 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001761 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001762 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001763 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 +02001764 break;
1765 default:
David Sedlákb3077192019-06-19 10:55:37 +02001766 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001767 return LY_EVALID;
1768 }
1769 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001770 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001771
1772error:
1773 free(buf);
1774 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001775}
1776
Michal Vaskoea5abea2018-09-18 13:10:54 +02001777/**
1778 * @brief Parse the enum or bit statement. Substatement of type statement.
1779 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001780 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001781 * @param[in] enum_kw Type of this particular keyword.
1782 * @param[in,out] enums Enums or bits to add to.
1783 *
1784 * @return LY_ERR values.
1785 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001786static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001787parse_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 +02001788{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001789 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001790 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001791 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001792 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001793 struct lysp_type_enum *enm;
1794
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001795 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001796
1797 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001798 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 +02001799 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001800 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001801 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001802 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001803
Radek Krejci011e4aa2020-09-04 15:22:31 +02001804 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001805 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1806
Radek Krejci33090f92020-12-17 20:12:46 +01001807 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001808 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001809 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001810 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 +02001811 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001812 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001813 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001814 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001816 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001817 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 +02001818 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001819 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001820 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001822 case LY_STMT_VALUE:
1823 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 +02001824 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001825 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001826 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001827 case LY_STMT_POSITION:
1828 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 +02001829 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001830 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001831 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001832 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001833 LY_CHECK_RET(parse_ext(ctx, word, word_len, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001834 break;
1835 default:
David Sedlákb3077192019-06-19 10:55:37 +02001836 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001837 return LY_EVALID;
1838 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001839 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 return ret;
1841}
1842
Michal Vaskoea5abea2018-09-18 13:10:54 +02001843/**
1844 * @brief Parse the fraction-digits statement. Substatement of type statement.
1845 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001846 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001847 * @param[in,out] fracdig Value to write to.
1848 * @param[in,out] exts Extension instances to add to.
1849 *
1850 * @return LY_ERR values.
1851 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001852static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001853parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001854{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001855 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001856 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001857 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001858 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001859 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001860
1861 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001862 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001863 return LY_EVALID;
1864 }
1865
1866 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001867 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868
1869 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001870 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871 free(buf);
1872 return LY_EVALID;
1873 }
1874
1875 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001876 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001878 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001879 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001880 free(buf);
1881 return LY_EVALID;
1882 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001883 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001884 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 free(buf);
1886 return LY_EVALID;
1887 }
1888 *fracdig = num;
1889 free(buf);
1890
Radek Krejci33090f92020-12-17 20:12:46 +01001891 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001892 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001893 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001894 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FRACTION_DIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001895 break;
1896 default:
David Sedlákb3077192019-06-19 10:55:37 +02001897 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001898 return LY_EVALID;
1899 }
1900 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001901 return ret;
1902}
1903
Michal Vaskoea5abea2018-09-18 13:10:54 +02001904/**
1905 * @brief Parse the require-instance statement. Substatement of type statement.
1906 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001907 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001908 * @param[in,out] reqinst Value to write to.
1909 * @param[in,out] flags Flags to write to.
1910 * @param[in,out] exts Extension instances to add to.
1911 *
1912 * @return LY_ERR values.
1913 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001914static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001915parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001916 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001917{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001918 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001920 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001921 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922
1923 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001924 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925 return LY_EVALID;
1926 }
1927 *flags |= LYS_SET_REQINST;
1928
1929 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001930 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931
Radek Krejcif13b87b2020-12-01 22:02:17 +01001932 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001933 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001934 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001935 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001936 free(buf);
1937 return LY_EVALID;
1938 }
1939 free(buf);
1940
Radek Krejci33090f92020-12-17 20:12:46 +01001941 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001942 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001943 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001944 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REQUIRE_INSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001945 break;
1946 default:
David Sedlákb3077192019-06-19 10:55:37 +02001947 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001948 return LY_EVALID;
1949 }
1950 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951 return ret;
1952}
1953
Michal Vaskoea5abea2018-09-18 13:10:54 +02001954/**
1955 * @brief Parse the modifier statement. Substatement of type pattern statement.
1956 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001957 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001958 * @param[in,out] pat Value to write to.
1959 * @param[in,out] exts Extension instances to add to.
1960 *
1961 * @return LY_ERR values.
1962 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001963static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001964parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001965{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001966 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001967 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001968 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001969 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001970
Radek Krejcif13b87b2020-12-01 22:02:17 +01001971 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001972 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973 return LY_EVALID;
1974 }
1975
1976 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001977 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978
Radek Krejcif13b87b2020-12-01 22:02:17 +01001979 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001980 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981 free(buf);
1982 return LY_EVALID;
1983 }
1984 free(buf);
1985
1986 /* replace the value in the dictionary */
1987 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001988 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001990 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991
Radek Krejcif13b87b2020-12-01 22:02:17 +01001992 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
1993 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001994 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995
Radek Krejci33090f92020-12-17 20:12:46 +01001996 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001997 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001998 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001999 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002000 break;
2001 default:
David Sedlákb3077192019-06-19 10:55:37 +02002002 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 return LY_EVALID;
2004 }
2005 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006 return ret;
2007}
2008
Michal Vaskoea5abea2018-09-18 13:10:54 +02002009/**
2010 * @brief Parse the pattern statement. Substatement of type statement.
2011 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002012 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002013 * @param[in,out] patterns Restrictions to add to.
2014 *
2015 * @return LY_ERR values.
2016 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002018parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002020 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002021 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002022 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002023 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002024 struct lysp_restr *restr;
2025
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002026 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027
2028 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002029 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030
2031 /* add special meaning first byte */
2032 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002033 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034 word = buf;
2035 } else {
2036 buf = malloc(word_len + 2);
2037 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002038 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002039 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002040 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002041 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002042 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
2043 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044
Radek Krejci33090f92020-12-17 20:12:46 +01002045 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002047 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002048 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 +02002049 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002050 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002051 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 +02002052 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002053 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01002054 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 +02002055 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002056 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002057 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 +02002058 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002059 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002060 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002061 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002063 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002064 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002065 break;
2066 default:
David Sedlákb3077192019-06-19 10:55:37 +02002067 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 return LY_EVALID;
2069 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071 return ret;
2072}
2073
Michal Vaskoea5abea2018-09-18 13:10:54 +02002074/**
2075 * @brief Parse the type statement.
2076 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002077 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002078 * @param[in,out] type Type to wrote to.
2079 *
2080 * @return LY_ERR values.
2081 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002083parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002084{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002085 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002087 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002088 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002089 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090 struct lysp_type *nest_type;
2091
2092 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002093 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094 return LY_EVALID;
2095 }
2096
2097 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002098 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002099 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002101 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002102 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002103
Radek Krejci33090f92020-12-17 20:12:46 +01002104 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002106 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002107 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 +01002108 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002110 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002111 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002112 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002114 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002115 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002116 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002118 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002119 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002120 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002122 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002124 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125 return LY_EVALID;
2126 }
2127 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002128 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129
Radek Krejci33090f92020-12-17 20:12:46 +01002130 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002131 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002133 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002134 if (type->path) {
Radek Krejci3972b332021-03-02 16:34:31 +01002135 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002136 return LY_EVALID;
2137 }
2138
Radek Krejcifc596f92021-02-26 22:40:26 +01002139 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 +02002140 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 +02002141 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002142 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002143 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002144 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002146 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002147 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002148 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002150 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002152 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153 return LY_EVALID;
2154 }
2155 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002156 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002157
Radek Krejci33090f92020-12-17 20:12:46 +01002158 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002159 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002161 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002162 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002163 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002164 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002165 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002166 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002167 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002168 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002170 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002171 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002172 break;
2173 default:
David Sedlákb3077192019-06-19 10:55:37 +02002174 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175 return LY_EVALID;
2176 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002178 return ret;
2179}
2180
Michal Vaskoea5abea2018-09-18 13:10:54 +02002181/**
2182 * @brief Parse the leaf statement.
2183 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002184 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002185 * @param[in,out] siblings Siblings to add to.
2186 *
2187 * @return LY_ERR values.
2188 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002189LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002190parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002192 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002193 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002194 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002195 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002196 struct lysp_node_leaf *leaf;
2197
David Sedlák60adc092019-08-06 15:57:02 +02002198 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002199 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002201 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202
Michal Vasko7fbc8162018-09-17 10:35:16 +02002203 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002204 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002205 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002206
2207 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002208 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002210 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002211 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002213 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002214 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 +02002215 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002217 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002218 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 +02002219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002220 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002221 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002223 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002224 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002226 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002227 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002229 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002230 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 +02002231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002232 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002233 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002235 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002236 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002238 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002239 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 +02002240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002241 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002242 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002243 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002244 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002245 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002246 break;
2247 default:
David Sedlákb3077192019-06-19 10:55:37 +02002248 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002249 return LY_EVALID;
2250 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002252 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002253checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254 /* mandatory substatements */
2255 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002256 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002257 return LY_EVALID;
2258 }
2259
2260 return ret;
2261}
2262
Michal Vaskoea5abea2018-09-18 13:10:54 +02002263/**
2264 * @brief Parse the max-elements statement.
2265 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002266 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002267 * @param[in,out] max Value to write to.
2268 * @param[in,out] flags Flags to write to.
2269 * @param[in,out] exts Extension instances to add to.
2270 *
2271 * @return LY_ERR values.
2272 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002273LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002274parse_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 +02002275{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002276 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002278 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002280 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281
2282 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002283 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284 return LY_EVALID;
2285 }
2286 *flags |= LYS_SET_MAX;
2287
2288 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002289 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290
2291 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002292 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 free(buf);
2294 return LY_EVALID;
2295 }
2296
Radek Krejci7f9b6512019-09-18 13:11:09 +02002297 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002299 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002300 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002301 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002302 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002303 free(buf);
2304 return LY_EVALID;
2305 }
2306 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002307 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308 free(buf);
2309 return LY_EVALID;
2310 }
2311
2312 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002313 } else {
2314 /* unbounded */
2315 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 }
2317 free(buf);
2318
Radek Krejci33090f92020-12-17 20:12:46 +01002319 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002320 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002321 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002322 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MAX_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002323 break;
2324 default:
David Sedlákb3077192019-06-19 10:55:37 +02002325 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326 return LY_EVALID;
2327 }
2328 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002329 return ret;
2330}
2331
Michal Vaskoea5abea2018-09-18 13:10:54 +02002332/**
2333 * @brief Parse the min-elements statement.
2334 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002335 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002336 * @param[in,out] min Value to write to.
2337 * @param[in,out] flags Flags to write to.
2338 * @param[in,out] exts Extension instances to add to.
2339 *
2340 * @return LY_ERR values.
2341 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002342LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002343parse_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 +02002344{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002345 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002347 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002349 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350
2351 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002352 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002353 return LY_EVALID;
2354 }
2355 *flags |= LYS_SET_MIN;
2356
2357 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002358 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359
2360 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002361 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362 free(buf);
2363 return LY_EVALID;
2364 }
2365
2366 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002367 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002369 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002370 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002371 free(buf);
2372 return LY_EVALID;
2373 }
2374 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002375 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002376 free(buf);
2377 return LY_EVALID;
2378 }
2379 *min = num;
2380 free(buf);
2381
Radek Krejci33090f92020-12-17 20:12:46 +01002382 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002384 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002385 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MIN_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002386 break;
2387 default:
David Sedlákb3077192019-06-19 10:55:37 +02002388 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002389 return LY_EVALID;
2390 }
2391 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002392 return ret;
2393}
2394
Michal Vaskoea5abea2018-09-18 13:10:54 +02002395/**
2396 * @brief Parse the ordered-by statement.
2397 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002398 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002399 * @param[in,out] flags Flags to write to.
2400 * @param[in,out] exts Extension instances to add to.
2401 *
2402 * @return LY_ERR values.
2403 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002404static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002405parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002407 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002409 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002410 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411
2412 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002413 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002414 return LY_EVALID;
2415 }
2416
2417 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002418 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419
Radek Krejcif13b87b2020-12-01 22:02:17 +01002420 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002421 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002422 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423 *flags |= LYS_ORDBY_USER;
2424 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002425 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426 free(buf);
2427 return LY_EVALID;
2428 }
2429 free(buf);
2430
Radek Krejci33090f92020-12-17 20:12:46 +01002431 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002433 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002434 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ORDERED_BY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435 break;
2436 default:
David Sedlákb3077192019-06-19 10:55:37 +02002437 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438 return LY_EVALID;
2439 }
2440 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002441 return ret;
2442}
2443
Michal Vaskoea5abea2018-09-18 13:10:54 +02002444/**
2445 * @brief Parse the leaf-list statement.
2446 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002447 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002448 * @param[in,out] siblings Siblings to add to.
2449 *
2450 * @return LY_ERR values.
2451 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002452LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002453parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002455 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002456 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002457 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002458 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002459 struct lysp_node_leaflist *llist;
2460
David Sedlák60adc092019-08-06 15:57:02 +02002461 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002462 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002464 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465
Michal Vasko7fbc8162018-09-17 10:35:16 +02002466 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002467 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002468 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469
2470 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002471 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002472 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002473 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002474 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002476 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002477 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002478 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002480 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002481 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 +02002482 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002483 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002484 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002485 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002486 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002487 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002488 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002489 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002490 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002491 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002492 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002493 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002495 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002496 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002498 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002499 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 +02002500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002501 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002502 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002504 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002505 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002507 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002508 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 +02002509 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002510 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002511 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002512 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002513 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002514 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002515 break;
2516 default:
David Sedlákb3077192019-06-19 10:55:37 +02002517 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518 return LY_EVALID;
2519 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002520 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002521 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002522checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523 /* mandatory substatements */
2524 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002525 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002526 return LY_EVALID;
2527 }
2528
2529 return ret;
2530}
2531
Michal Vaskoea5abea2018-09-18 13:10:54 +02002532/**
2533 * @brief Parse the refine statement.
2534 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002535 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002536 * @param[in,out] refines Refines to add to.
2537 *
2538 * @return LY_ERR values.
2539 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002541parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002543 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002544 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002545 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002546 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002547 struct lysp_refine *rf;
2548
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002549 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002550
2551 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002552 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002553 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002554 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555
Radek Krejci33090f92020-12-17 20:12:46 +01002556 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002558 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002559 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002561 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002562 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002563 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002564 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002565 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 +02002566 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002567 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002568 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002569 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002571 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002572 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002573 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002574 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002575 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002577 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002578 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002579 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002580 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002581 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002582 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002583 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002584 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 +02002585 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002586 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002587 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 +02002588 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002589 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002590 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002591 break;
2592 default:
David Sedlákb3077192019-06-19 10:55:37 +02002593 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 return LY_EVALID;
2595 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002596 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 return ret;
2598}
2599
Michal Vaskoea5abea2018-09-18 13:10:54 +02002600/**
2601 * @brief Parse the typedef statement.
2602 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002603 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002604 * @param[in,out] typedefs Typedefs to add to.
2605 *
2606 * @return LY_ERR values.
2607 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002609parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002611 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002613 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002614 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615 struct lysp_tpdf *tpdf;
2616
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002617 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618
2619 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002620 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002621 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622
2623 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002624 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002625 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002626 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002627 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 +02002628 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002630 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002631 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 +02002632 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002633 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002634 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 +02002635 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002636 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002637 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002639 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002640 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002642 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002643 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 +02002644 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002645 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002646 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647 break;
2648 default:
David Sedlákb3077192019-06-19 10:55:37 +02002649 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650 return LY_EVALID;
2651 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002653 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002654checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655 /* mandatory substatements */
2656 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002657 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658 return LY_EVALID;
2659 }
2660
Radek Krejcibbe09a92018-11-08 09:36:54 +01002661 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002662 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002663 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002664 }
2665
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 return ret;
2667}
2668
Michal Vaskoea5abea2018-09-18 13:10:54 +02002669/**
2670 * @brief Parse the input or output statement.
2671 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002672 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002673 * @param[in] kw Type of this particular keyword
2674 * @param[in,out] inout_p Input/output pointer to write to.
2675 *
2676 * @return LY_ERR values.
2677 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002679parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002680 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002682 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002683 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002684 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002685 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002686 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002688 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002689 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002690 return LY_EVALID;
2691 }
2692
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002693 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002694 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2695 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002696 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697
2698 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002699 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002701 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002702 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002703 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002704 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002705 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002707 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002708 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002709 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002710 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002711 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002713 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002714 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002715 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002716 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002717 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002719 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002720 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002722 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002723 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002724 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002725 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002726 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002728 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002729 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002730 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002732 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002733 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002735 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002736 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737 break;
2738 default:
David Sedlákb3077192019-06-19 10:55:37 +02002739 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002740 return LY_EVALID;
2741 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002742 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002743 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002744
Radek Krejci7fc68292019-06-12 13:51:09 +02002745checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002746 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002747 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2748 return LY_EVALID;
2749 }
2750
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751 return ret;
2752}
2753
Michal Vaskoea5abea2018-09-18 13:10:54 +02002754/**
2755 * @brief Parse the action statement.
2756 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002757 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002758 * @param[in,out] actions Actions to add to.
2759 *
2760 * @return LY_ERR values.
2761 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002762LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002763parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002765 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002766 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002767 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002768 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002769 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770
Radek Krejci2a9fc652021-01-22 17:44:34 +01002771 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772
2773 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002774 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002775 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002776 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002777 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778
Radek Krejci33090f92020-12-17 20:12:46 +01002779 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002780 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002781 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002782 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 +02002783 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002784 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002785 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002786 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002787 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002788 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 +02002789 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002790 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002791 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792 break;
2793
Radek Krejcid6b76452019-09-03 17:03:03 +02002794 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002795 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002797 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002798 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799 break;
2800
Radek Krejcid6b76452019-09-03 17:03:03 +02002801 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002802 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002803 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002804 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002805 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002807 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002808 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 +02002809 break;
2810 default:
David Sedlákb3077192019-06-19 10:55:37 +02002811 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002812 return LY_EVALID;
2813 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002815 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002816
Radek Krejci2a9fc652021-01-22 17:44:34 +01002817checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002818 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2819 if (!act->input.nodetype) {
2820 act->input.nodetype = LYS_INPUT;
2821 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002822 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002823 }
2824 if (!act->output.nodetype) {
2825 act->output.nodetype = LYS_OUTPUT;
2826 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002827 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002828 }
2829
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830 return ret;
2831}
2832
Michal Vaskoea5abea2018-09-18 13:10:54 +02002833/**
2834 * @brief Parse the notification statement.
2835 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002836 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002837 * @param[in,out] notifs Notifications to add to.
2838 *
2839 * @return LY_ERR values.
2840 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002841LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002842parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002843{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002844 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002846 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002847 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002848 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002849
Radek Krejci2a9fc652021-01-22 17:44:34 +01002850 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851
2852 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002853 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002854 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002855 notif->nodetype = LYS_NOTIF;
2856 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857
Radek Krejci2a9fc652021-01-22 17:44:34 +01002858 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002860 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002861 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 +02002862 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002863 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002864 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002866 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002867 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 +02002868 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002869 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002870 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002871 break;
2872
Radek Krejcid6b76452019-09-03 17:03:03 +02002873 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002874 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002875 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002876 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002877 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002878 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002879 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002880 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002882 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002883 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002884 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002885 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002886 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002887 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002888 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002889 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002891 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002892 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002893 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002894 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002895 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002896 break;
2897
Radek Krejcid6b76452019-09-03 17:03:03 +02002898 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002899 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002900 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002902 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002903 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002905 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002906 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002908 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002909 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
2911 default:
David Sedlákb3077192019-06-19 10:55:37 +02002912 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 return LY_EVALID;
2914 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002916
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917 return ret;
2918}
2919
Michal Vaskoea5abea2018-09-18 13:10:54 +02002920/**
2921 * @brief Parse the grouping statement.
2922 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002923 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002924 * @param[in,out] groupings Groupings to add to.
2925 *
2926 * @return LY_ERR values.
2927 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002928LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002929parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002930{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002931 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002933 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002934 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002935 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002936
Radek Krejci2a9fc652021-01-22 17:44:34 +01002937 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002938
2939 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002940 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002941 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002942 grp->nodetype = LYS_GROUPING;
2943 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944
Radek Krejci2a9fc652021-01-22 17:44:34 +01002945 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002947 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002948 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 +02002949 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002950 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002951 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 +02002952 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002953 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002954 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955 break;
2956
Radek Krejcid6b76452019-09-03 17:03:03 +02002957 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002958 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002959 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002960 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002961 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002962 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002963 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002964 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002965 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002966 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002967 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002969 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002970 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002971 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002972 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002973 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002975 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002976 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002978 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002979 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002980 break;
2981
Radek Krejcid6b76452019-09-03 17:03:03 +02002982 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002983 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002984 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002985 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002986 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002987 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002988 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002989 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002990 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002992 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002993 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002994 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002996 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002997 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 break;
2999 default:
David Sedlákb3077192019-06-19 10:55:37 +02003000 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 return LY_EVALID;
3002 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003004
Michal Vasko7fbc8162018-09-17 10:35:16 +02003005 return ret;
3006}
3007
Michal Vaskoea5abea2018-09-18 13:10:54 +02003008/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003009 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003010 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003011 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003012 * @param[in,out] augments Augments to add to.
3013 *
3014 * @return LY_ERR values.
3015 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003016LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003017parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003019 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003021 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003022 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003023 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024
Radek Krejci2a9fc652021-01-22 17:44:34 +01003025 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026
3027 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003028 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003029 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003030 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003031 aug->nodetype = LYS_AUGMENT;
3032 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033
Radek Krejci2a9fc652021-01-22 17:44:34 +01003034 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003036 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003037 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 +02003038 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003039 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003040 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003041 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003042 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003043 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 +02003044 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003045 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003046 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003048 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003049 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 break;
3051
Radek Krejcid6b76452019-09-03 17:03:03 +02003052 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003053 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003054 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003055 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003056 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003057 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003058 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003059 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003061 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003062 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003064 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003065 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003067 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003068 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003070 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003071 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003073 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003074 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003076 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003077 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 break;
3079
Radek Krejcid6b76452019-09-03 17:03:03 +02003080 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003081 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003082 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003084 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003085 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003086 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003088 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003089 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003090 break;
3091 default:
David Sedlákb3077192019-06-19 10:55:37 +02003092 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093 return LY_EVALID;
3094 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003096
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097 return ret;
3098}
3099
Michal Vaskoea5abea2018-09-18 13:10:54 +02003100/**
3101 * @brief Parse the uses statement.
3102 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003103 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003104 * @param[in,out] siblings Siblings to add to.
3105 *
3106 * @return LY_ERR values.
3107 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003108LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003109parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003111 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003113 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003114 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 struct lysp_node_uses *uses;
3116
David Sedlák60adc092019-08-06 15:57:02 +02003117 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003118 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003120 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003123 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003124 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125
3126 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003127 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003129 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003130 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 +02003131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003132 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003133 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003135 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003136 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 +02003137 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003138 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003139 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003141 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003142 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143 break;
3144
Radek Krejcid6b76452019-09-03 17:03:03 +02003145 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003146 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003148 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003149 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003151 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003152 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003153 break;
3154 default:
David Sedlákb3077192019-06-19 10:55:37 +02003155 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003156 return LY_EVALID;
3157 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003159
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 return ret;
3161}
3162
Michal Vaskoea5abea2018-09-18 13:10:54 +02003163/**
3164 * @brief Parse the case statement.
3165 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003166 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003167 * @param[in,out] siblings Siblings to add to.
3168 *
3169 * @return LY_ERR values.
3170 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003171LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003172parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003174 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003175 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003176 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003177 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003178 struct lysp_node_case *cas;
3179
David Sedlák60adc092019-08-06 15:57:02 +02003180 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003181 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003183 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003184
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003186 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003187 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188
3189 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003190 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003192 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003193 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 +02003194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003195 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003196 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003198 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003199 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 +02003200 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003201 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003202 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003204 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003205 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003206 break;
3207
Radek Krejcid6b76452019-09-03 17:03:03 +02003208 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003209 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003210 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003211 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003212 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003214 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003215 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003217 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003218 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003220 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003221 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003223 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003224 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003226 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003227 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003229 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003230 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003232 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003233 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003234 break;
3235 default:
David Sedlákb3077192019-06-19 10:55:37 +02003236 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 return LY_EVALID;
3238 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 return ret;
3241}
3242
Michal Vaskoea5abea2018-09-18 13:10:54 +02003243/**
3244 * @brief Parse the choice statement.
3245 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003246 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003247 * @param[in,out] siblings Siblings to add to.
3248 *
3249 * @return LY_ERR values.
3250 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003251LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003252parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003254 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003256 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003257 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003258 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003259
David Sedlák60adc092019-08-06 15:57:02 +02003260 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003261 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003262 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003263 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264
Michal Vasko7fbc8162018-09-17 10:35:16 +02003265 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003266 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003267 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268
3269 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003270 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003272 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003273 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003275 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003276 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 +02003277 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003278 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003279 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003281 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003282 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003284 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003285 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 +02003286 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003287 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003288 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003289 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003290 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003291 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003293 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01003294 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 +02003295 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003296 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297 break;
3298
Radek Krejcid6b76452019-09-03 17:03:03 +02003299 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003300 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003301 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003302 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003303 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003305 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003306 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003308 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003309 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003310 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003312 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003313 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003315 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003316 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003318 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003319 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003321 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003322 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003324 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003325 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326 break;
3327 default:
David Sedlákb3077192019-06-19 10:55:37 +02003328 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 return LY_EVALID;
3330 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332 return ret;
3333}
3334
Michal Vaskoea5abea2018-09-18 13:10:54 +02003335/**
3336 * @brief Parse the container statement.
3337 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003338 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003339 * @param[in,out] siblings Siblings to add to.
3340 *
3341 * @return LY_ERR values.
3342 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003343LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003344parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345{
3346 LY_ERR ret = 0;
3347 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003348 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003349 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350 struct lysp_node_container *cont;
3351
David Sedlák60adc092019-08-06 15:57:02 +02003352 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003353 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003354 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003355 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003358 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003359 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003360
3361 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003362 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003364 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003365 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003367 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003368 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 +02003369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003370 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003371 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003373 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003374 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 +02003375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003376 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003377 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003379 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003380 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003382 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003383 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 +02003384 break;
3385
Radek Krejcid6b76452019-09-03 17:03:03 +02003386 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003387 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003388 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003389 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003390 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003392 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003393 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003395 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003396 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003397 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003398 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003399 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003401 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003402 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003404 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003405 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003407 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003408 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 break;
3410
Radek Krejcid6b76452019-09-03 17:03:03 +02003411 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003412 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003414 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003415 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003417 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003418 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003419 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003421 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003422 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003424 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003425 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003426 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003429 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 break;
3431 default:
David Sedlákb3077192019-06-19 10:55:37 +02003432 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433 return LY_EVALID;
3434 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003436
Michal Vasko7fbc8162018-09-17 10:35:16 +02003437 return ret;
3438}
3439
Michal Vaskoea5abea2018-09-18 13:10:54 +02003440/**
3441 * @brief Parse the list statement.
3442 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003443 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003444 * @param[in,out] siblings Siblings to add to.
3445 *
3446 * @return LY_ERR values.
3447 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003448LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003449parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003450{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003451 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003453 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003454 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 struct lysp_node_list *list;
3456
David Sedlák60adc092019-08-06 15:57:02 +02003457 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003458 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003460 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003463 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003464 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465
3466 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003467 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003468 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003469 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003470 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003472 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003473 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 +02003474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003475 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003476 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003477 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003478 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003479 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 +02003480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003481 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003482 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003484 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003485 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003487 case LY_STMT_KEY:
Radek Krejcifc596f92021-02-26 22:40:26 +01003488 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 +02003489 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003490 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003491 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003492 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003493 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003494 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003495 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003496 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003497 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003498 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003499 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003500 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003501 break;
3502
Radek Krejcid6b76452019-09-03 17:03:03 +02003503 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003504 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003505 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003506 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003507 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003509 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003510 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003512 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003513 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003514 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003515 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003516 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003518 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003519 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003521 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003522 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003524 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003525 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 break;
3527
Radek Krejcid6b76452019-09-03 17:03:03 +02003528 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003529 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003531 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003532 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003534 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003535 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003536 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003538 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003539 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003541 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003542 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003543 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003545 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003546 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 break;
3548 default:
David Sedlákb3077192019-06-19 10:55:37 +02003549 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 return LY_EVALID;
3551 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003552 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003553
Michal Vasko7fbc8162018-09-17 10:35:16 +02003554 return ret;
3555}
3556
Michal Vaskoea5abea2018-09-18 13:10:54 +02003557/**
3558 * @brief Parse the yin-element statement.
3559 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003560 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003561 * @param[in,out] flags Flags to write to.
3562 * @param[in,out] exts Extension instances to add to.
3563 *
3564 * @return LY_ERR values.
3565 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003567parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003568{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003569 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003571 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003572 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573
3574 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003575 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576 return LY_EVALID;
3577 }
3578
3579 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003580 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581
Radek Krejcif13b87b2020-12-01 22:02:17 +01003582 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003583 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003584 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003585 *flags |= LYS_YINELEM_FALSE;
3586 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003587 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588 free(buf);
3589 return LY_EVALID;
3590 }
3591 free(buf);
3592
Radek Krejci33090f92020-12-17 20:12:46 +01003593 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003595 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003596 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003597 LY_CHECK_RET(ret);
3598 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 default:
David Sedlákb3077192019-06-19 10:55:37 +02003600 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601 return LY_EVALID;
3602 }
3603 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604 return ret;
3605}
3606
Michal Vaskoea5abea2018-09-18 13:10:54 +02003607/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003608 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003609 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003610 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003611 * @param[in,out] argument Value to write to.
3612 * @param[in,out] flags Flags to write to.
3613 * @param[in,out] exts Extension instances to add to.
3614 *
3615 * @return LY_ERR values.
3616 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003618parse_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 +02003619{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003620 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003622 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003623 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003624
3625 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003626 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627 return LY_EVALID;
3628 }
3629
3630 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003631 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003632 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633
Radek Krejci33090f92020-12-17 20:12:46 +01003634 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003636 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003637 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003639 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003640 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 break;
3642 default:
David Sedlákb3077192019-06-19 10:55:37 +02003643 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 return LY_EVALID;
3645 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 return ret;
3648}
3649
Michal Vaskoea5abea2018-09-18 13:10:54 +02003650/**
3651 * @brief Parse the extension statement.
3652 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003653 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003654 * @param[in,out] extensions Extensions to add to.
3655 *
3656 * @return LY_ERR values.
3657 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003659parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003661 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003662 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003663 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003664 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665 struct lysp_ext *ex;
3666
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003667 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668
3669 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003670 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003671 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672
Radek Krejci33090f92020-12-17 20:12:46 +01003673 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003675 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003676 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 +02003677 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003678 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003679 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 +02003680 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003681 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003682 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003683 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003684 case LY_STMT_ARGUMENT:
Radek Krejci9f87b0c2021-03-05 14:45:26 +01003685 LY_CHECK_RET(parse_argument(ctx, &ex->argname, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003686 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003687 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003688 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003689 break;
3690 default:
David Sedlákb3077192019-06-19 10:55:37 +02003691 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692 return LY_EVALID;
3693 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003695 return ret;
3696}
3697
Michal Vaskoea5abea2018-09-18 13:10:54 +02003698/**
3699 * @brief Parse the deviate statement.
3700 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003701 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003702 * @param[in,out] deviates Deviates to add to.
3703 *
3704 * @return LY_ERR values.
3705 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003706LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003707parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003708{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003709 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003710 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003711 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003712 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003713 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714 struct lysp_deviate_add *d_add = NULL;
3715 struct lysp_deviate_rpl *d_rpl = NULL;
3716 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003717 const char **d_units = NULL;
3718 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003719 struct lysp_restr **d_musts = NULL;
3720 uint16_t *d_flags = 0;
3721 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003722
3723 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003724 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003725
Radek Krejcif13b87b2020-12-01 22:02:17 +01003726 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003727 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003728 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003729 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003730 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003731 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003732 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733 dev_mod = LYS_DEV_DELETE;
3734 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003735 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736 free(buf);
3737 return LY_EVALID;
3738 }
3739 free(buf);
3740
3741 /* create structure */
3742 switch (dev_mod) {
3743 case LYS_DEV_NOT_SUPPORTED:
3744 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003745 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003746 break;
3747 case LYS_DEV_ADD:
3748 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003749 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 d = (struct lysp_deviate *)d_add;
3751 d_units = &d_add->units;
3752 d_uniques = &d_add->uniques;
3753 d_dflts = &d_add->dflts;
3754 d_musts = &d_add->musts;
3755 d_flags = &d_add->flags;
3756 d_min = &d_add->min;
3757 d_max = &d_add->max;
3758 break;
3759 case LYS_DEV_REPLACE:
3760 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003761 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762 d = (struct lysp_deviate *)d_rpl;
3763 d_units = &d_rpl->units;
3764 d_flags = &d_rpl->flags;
3765 d_min = &d_rpl->min;
3766 d_max = &d_rpl->max;
3767 break;
3768 case LYS_DEV_DELETE:
3769 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003770 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003771 d = (struct lysp_deviate *)d_del;
3772 d_units = &d_del->units;
3773 d_uniques = &d_del->uniques;
3774 d_dflts = &d_del->dflts;
3775 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003776 break;
3777 default:
3778 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003779 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003780 }
3781 d->mod = dev_mod;
3782
3783 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003784 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785
Radek Krejci33090f92020-12-17 20:12:46 +01003786 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003787 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003788 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003789 switch (dev_mod) {
3790 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003791 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003792 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003793 return LY_EVALID;
3794 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003795 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003796 break;
3797 }
3798 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003799 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003800 switch (dev_mod) {
3801 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003802 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003803 return LY_EVALID;
3804 case LYS_DEV_REPLACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003805 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 +02003806 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003807 break;
3808 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003809 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003810 break;
3811 }
3812 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003813 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 switch (dev_mod) {
3815 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003816 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003817 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003818 return LY_EVALID;
3819 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003820 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &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_MAX_ELEMENTS:
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_maxelements(ctx, d_max, 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_MIN_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_minelements(ctx, d_min, 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_MUST:
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_REPLACE:
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_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003854 break;
3855 }
3856 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003857 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858 switch (dev_mod) {
3859 case LYS_DEV_NOT_SUPPORTED:
3860 case LYS_DEV_ADD:
3861 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003862 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863 return LY_EVALID;
3864 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003865 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003866 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003867 return LY_EVALID;
3868 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003869 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003870 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003871 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872 break;
3873 }
3874 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003875 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003876 switch (dev_mod) {
3877 case LYS_DEV_NOT_SUPPORTED:
3878 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003879 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880 return LY_EVALID;
3881 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003882 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003883 break;
3884 }
3885 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003886 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 switch (dev_mod) {
3888 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003889 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003890 return LY_EVALID;
3891 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003892 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 +02003893 break;
3894 }
3895 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003896 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003897 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATE, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 break;
3899 default:
David Sedlákb3077192019-06-19 10:55:37 +02003900 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 return LY_EVALID;
3902 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003903 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 return ret;
3905}
3906
Michal Vaskoea5abea2018-09-18 13:10:54 +02003907/**
3908 * @brief Parse the deviation statement.
3909 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003910 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003911 * @param[in,out] deviations Deviations to add to.
3912 *
3913 * @return LY_ERR values.
3914 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003915LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003916parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003918 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003920 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003921 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003922 struct lysp_deviation *dev;
3923
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003924 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925
3926 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003927 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003928 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003929 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930
Radek Krejci33090f92020-12-17 20:12:46 +01003931 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003932 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003933 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003934 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 +02003935 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003936 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003937 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003939 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003940 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 +02003941 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003942 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003943 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATION, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 break;
3945 default:
David Sedlákb3077192019-06-19 10:55:37 +02003946 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 return LY_EVALID;
3948 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003950 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003951checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003952 /* mandatory substatements */
3953 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003954 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955 return LY_EVALID;
3956 }
3957
3958 return ret;
3959}
3960
Michal Vaskoea5abea2018-09-18 13:10:54 +02003961/**
3962 * @brief Parse the feature statement.
3963 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003964 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003965 * @param[in,out] features Features to add to.
3966 *
3967 * @return LY_ERR values.
3968 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003969LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003970parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003971{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003972 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003973 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003974 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003975 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 struct lysp_feature *feat;
3977
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003978 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003979
3980 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003981 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003982 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003983
Radek Krejci33090f92020-12-17 20:12:46 +01003984 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003986 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003987 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 +02003988 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003989 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003990 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003992 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003993 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 +02003994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003995 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003996 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003998 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003999 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 break;
4001 default:
David Sedlákb3077192019-06-19 10:55:37 +02004002 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004003 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004004 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004005 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004006 return ret;
4007}
4008
Michal Vaskoea5abea2018-09-18 13:10:54 +02004009/**
4010 * @brief Parse the identity statement.
4011 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004012 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004013 * @param[in,out] identities Identities to add to.
4014 *
4015 * @return LY_ERR values.
4016 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004017LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004018parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004020 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004021 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004022 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004023 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024 struct lysp_ident *ident;
4025
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004026 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004027
4028 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004029 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004030 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004031
Radek Krejci33090f92020-12-17 20:12:46 +01004032 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004034 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004035 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 +02004036 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004037 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004038 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004039 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004041 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004042 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 +02004043 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004044 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004045 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004046 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004047 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004048 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004049 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 +01004050 return LY_EVALID;
4051 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004052 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 +02004053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004054 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004055 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 break;
4057 default:
David Sedlákb3077192019-06-19 10:55:37 +02004058 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059 return LY_EVALID;
4060 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004061 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004062 return ret;
4063}
4064
Michal Vaskoea5abea2018-09-18 13:10:54 +02004065/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004066 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004067 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004068 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004069 * @param[in,out] mod Module to write to.
4070 *
4071 * @return LY_ERR values.
4072 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004073LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004074parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004075{
4076 LY_ERR ret = 0;
4077 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004078 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004079 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004080 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004081 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004082
Michal Vaskoc3781c32020-10-06 14:04:08 +02004083 mod->is_submod = 0;
4084
4085 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004086 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004087 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004088
Radek Krejci33090f92020-12-17 20:12:46 +01004089 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004090
Radek Krejcie3846472018-10-15 15:24:51 +02004091#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004092 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 +02004093
Michal Vasko7fbc8162018-09-17 10:35:16 +02004094 switch (kw) {
4095 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004096 case LY_STMT_NAMESPACE:
4097 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004098 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004100 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004101 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102 break;
4103 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004104 case LY_STMT_INCLUDE:
4105 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004106 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107 break;
4108 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004109 case LY_STMT_ORGANIZATION:
4110 case LY_STMT_CONTACT:
4111 case LY_STMT_DESCRIPTION:
4112 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004113 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004114 break;
4115
4116 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004117 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004118 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004119 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004121 case LY_STMT_ANYDATA:
4122 case LY_STMT_ANYXML:
4123 case LY_STMT_AUGMENT:
4124 case LY_STMT_CHOICE:
4125 case LY_STMT_CONTAINER:
4126 case LY_STMT_DEVIATION:
4127 case LY_STMT_EXTENSION:
4128 case LY_STMT_FEATURE:
4129 case LY_STMT_GROUPING:
4130 case LY_STMT_IDENTITY:
4131 case LY_STMT_LEAF:
4132 case LY_STMT_LEAF_LIST:
4133 case LY_STMT_LIST:
4134 case LY_STMT_NOTIFICATION:
4135 case LY_STMT_RPC:
4136 case LY_STMT_TYPEDEF:
4137 case LY_STMT_USES:
4138 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139 mod_stmt = Y_MOD_BODY;
4140 break;
4141 default:
4142 /* error handled in the next switch */
4143 break;
4144 }
Radek Krejcie3846472018-10-15 15:24:51 +02004145#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004146
Radek Krejcie3846472018-10-15 15:24:51 +02004147 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004148 switch (kw) {
4149 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004150 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004151 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004153 case LY_STMT_NAMESPACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004154 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 +02004155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004156 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01004157 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 +02004158 break;
4159
4160 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004161 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004162 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004164 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004165 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 break;
4167
4168 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004169 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004170 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 +02004171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004172 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004173 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 +02004174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004175 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004176 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 +02004177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004178 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004179 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 +02004180 break;
4181
4182 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004183 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004184 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004185 break;
4186
4187 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004189 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004190 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004191 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004192 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004194 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004195 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004197 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004198 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004200 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004201 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004203 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004204 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004206 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004207 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004209 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004210 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004211 break;
4212
Radek Krejcid6b76452019-09-03 17:03:03 +02004213 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004214 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004216 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004217 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004219 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004220 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004222 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004223 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004225 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004226 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004228 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004229 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004231 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004232 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004234 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004235 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004237 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004238 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004240 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004241 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004242 break;
4243
4244 default:
David Sedlákb3077192019-06-19 10:55:37 +02004245 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004246 return LY_EVALID;
4247 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004248 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004249 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004250
Radek Krejci6d6556c2018-11-08 09:37:45 +01004251checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004252 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004253 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004254 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004255 return LY_EVALID;
4256 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004257 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004258 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004259 }
4260
Radek Krejcie9e987e2018-10-31 12:50:27 +01004261 /* submodules share the namespace with the module names, so there must not be
4262 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004263 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004264 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004265 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004266 return LY_EVALID;
4267 }
4268
4269 return ret;
4270}
4271
4272/**
4273 * @brief Parse submodule substatements.
4274 *
4275 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004276 * @param[out] submod Parsed submodule structure.
4277 *
4278 * @return LY_ERR values.
4279 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004280LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004281parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004282{
4283 LY_ERR ret = 0;
4284 char *buf, *word;
4285 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004286 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004287 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004288 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004289
Michal Vaskoc3781c32020-10-06 14:04:08 +02004290 submod->is_submod = 1;
4291
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004292 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004293 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004294 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004295
Radek Krejci33090f92020-12-17 20:12:46 +01004296 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004297
4298#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004299 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 +01004300
4301 switch (kw) {
4302 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004303 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004304 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4305 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004306 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004307 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4308 break;
4309 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004310 case LY_STMT_INCLUDE:
4311 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004312 CHECK_ORDER(Y_MOD_LINKAGE);
4313 break;
4314 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004315 case LY_STMT_ORGANIZATION:
4316 case LY_STMT_CONTACT:
4317 case LY_STMT_DESCRIPTION:
4318 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004319 CHECK_ORDER(Y_MOD_META);
4320 break;
4321
4322 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004323 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004324 CHECK_ORDER(Y_MOD_REVISION);
4325 break;
4326 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004327 case LY_STMT_ANYDATA:
4328 case LY_STMT_ANYXML:
4329 case LY_STMT_AUGMENT:
4330 case LY_STMT_CHOICE:
4331 case LY_STMT_CONTAINER:
4332 case LY_STMT_DEVIATION:
4333 case LY_STMT_EXTENSION:
4334 case LY_STMT_FEATURE:
4335 case LY_STMT_GROUPING:
4336 case LY_STMT_IDENTITY:
4337 case LY_STMT_LEAF:
4338 case LY_STMT_LEAF_LIST:
4339 case LY_STMT_LIST:
4340 case LY_STMT_NOTIFICATION:
4341 case LY_STMT_RPC:
4342 case LY_STMT_TYPEDEF:
4343 case LY_STMT_USES:
4344 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004345 mod_stmt = Y_MOD_BODY;
4346 break;
4347 default:
4348 /* error handled in the next switch */
4349 break;
4350 }
4351#undef CHECK_ORDER
4352
4353 prev_kw = kw;
4354 switch (kw) {
4355 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004356 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004357 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004359 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004360 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004361 break;
4362
4363 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004364 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004365 if (submod->version == LYS_VERSION_1_1) {
4366 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4367 submod->name);
4368 }
Radek Krejci33090f92020-12-17 20:12:46 +01004369 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004371 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004372 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004373 break;
4374
4375 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004376 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004377 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 +01004378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004379 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004380 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 +01004381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004382 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004383 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 +01004384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004385 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004386 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 +01004387 break;
4388
4389 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004390 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004391 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004392 break;
4393
4394 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004395 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004396 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004397 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004398 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004399 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004401 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004402 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004404 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004405 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004407 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004408 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004410 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004411 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004413 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004414 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004416 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004417 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004418 break;
4419
Radek Krejcid6b76452019-09-03 17:03:03 +02004420 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004421 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004422 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004423 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004424 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004425 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004426 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004427 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004429 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004430 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004432 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004433 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004435 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004436 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004438 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004439 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004441 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004442 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004444 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004445 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004447 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004448 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004449 break;
4450
4451 default:
David Sedlákb3077192019-06-19 10:55:37 +02004452 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004453 return LY_EVALID;
4454 }
4455 }
4456 LY_CHECK_RET(ret);
4457
4458checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004459 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004460 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004461 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004462 return LY_EVALID;
4463 }
4464
4465 /* submodules share the namespace with the module names, so there must not be
4466 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004467 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004468 /* main modules may have different revisions */
4469 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004470 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004471 return LY_EVALID;
4472 }
4473
Michal Vasko7fbc8162018-09-17 10:35:16 +02004474 return ret;
4475}
4476
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004477/**
4478 * @brief Skip any redundant characters, namely whitespaces and comments.
4479 *
4480 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004481 * @return LY_SUCCESS on success.
4482 * @return LY_EVALID on invalid comment.
4483 */
4484static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004485skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004486{
4487 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004488 while (ctx->in->current[0]) {
4489 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004490 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004491 ly_in_skip(ctx->in, 2);
4492 LY_CHECK_RET(skip_comment(ctx, 1));
4493 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004494 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004495 ly_in_skip(ctx->in, 2);
4496 LY_CHECK_RET(skip_comment(ctx, 2));
4497 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004498 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004499 if (ctx->in->current[0] == '\n') {
4500 LY_IN_NEW_LINE(ctx->in);
4501 }
Radek Krejci33090f92020-12-17 20:12:46 +01004502 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004503 } else {
4504 break;
4505 }
4506 }
4507
4508 return LY_SUCCESS;
4509}
4510
Radek Krejcid4557c62018-09-17 11:42:09 +02004511LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004512yang_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 +02004513 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004514{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004515 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004516 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004517 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004518 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004519 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004520
David Sedlák1b623122019-08-05 15:27:49 +02004521 /* create context */
4522 *context = calloc(1, sizeof **context);
4523 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004524 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004525 (*context)->unres = main_ctx->unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004526 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004527
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004528 mod_p = calloc(1, sizeof *mod_p);
4529 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4530 mod_p->mod = main_ctx->parsed_mod->mod;
4531 mod_p->parsing = 1;
4532 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4533
Radek Krejciddace2c2021-01-08 11:30:56 +01004534 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004535
David Sedlák1b623122019-08-05 15:27:49 +02004536 /* map the typedefs and groupings list from main context to the submodule's context */
4537 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4538 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4539
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004540 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004541 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004542 LY_CHECK_GOTO(ret, cleanup);
4543
Michal Vasko7fbc8162018-09-17 10:35:16 +02004544 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004545 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004546 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004547
Radek Krejcid6b76452019-09-03 17:03:03 +02004548 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004549 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004550 ret = LY_EINVAL;
4551 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004552 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004553 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004554 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004555 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004556 }
4557
Michal Vasko7fbc8162018-09-17 10:35:16 +02004558 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004559 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004560 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004561
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004562 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004563 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004564 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004565 if (in->current[0]) {
4566 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004567 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004568 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004569 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004570
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004571 mod_p->parsing = 0;
4572 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004573
Radek Krejcibbe09a92018-11-08 09:36:54 +01004574cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004575 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004576 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004577 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004578 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004579 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004580 }
4581
4582 return ret;
4583}
4584
4585LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004586yang_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 +01004587{
4588 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004589 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004590 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004591 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004592 struct lysp_module *mod_p = NULL;
4593
David Sedlák1b623122019-08-05 15:27:49 +02004594 /* create context */
4595 *context = calloc(1, sizeof **context);
4596 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004597 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004598 (*context)->unres = unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004599 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004600
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004601 mod_p = calloc(1, sizeof *mod_p);
4602 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4603 mod_p->mod = mod;
4604 mod_p->parsing = 1;
4605 (*context)->parsed_mod = mod_p;
4606
Radek Krejciddace2c2021-01-08 11:30:56 +01004607 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004608
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004609 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004610 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004611 LY_CHECK_GOTO(ret, cleanup);
4612
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004613 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004614 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004615 LY_CHECK_GOTO(ret, cleanup);
4616
Radek Krejcid6b76452019-09-03 17:03:03 +02004617 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004618 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 +01004619 ret = LY_EINVAL;
4620 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004621 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004622 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004623 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004624 goto cleanup;
4625 }
4626
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004627 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004628 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004629 LY_CHECK_GOTO(ret, cleanup);
4630
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004631 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004632 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004633 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004634 if (in->current[0]) {
4635 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004636 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004637 goto cleanup;
4638 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004639
4640 mod_p->parsing = 0;
4641 mod->parsed = mod_p;
4642
4643cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004644 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004645 if (ret) {
4646 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004647 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004648 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004649 }
4650
Michal Vasko7fbc8162018-09-17 10:35:16 +02004651 return ret;
4652}