blob: 2647764967e89f4c25d714bc9e1db8ac01701e31 [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;
399 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200400 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200401 /* we will be removing the indents so we need our own buffer */
402 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200403
404 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200405 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200406
Radek Krejciff13cd12019-10-25 15:34:24 +0200407 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200408 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200409 }
410
411 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100412 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200413
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200414 /* reset context indentation counter for possible string after this one */
415 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200416 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200417 break;
418 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200419 /* first non-whitespace character, stop eating indentation */
420 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200421
422 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100423 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 +0200424 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200425 break;
426 }
427 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100428 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100430 c = ctx->in->current;
431 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200432 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100433 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100434 /* fix false newline count in buf_store_char() */
435 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200436 break;
437 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100438 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200439 break;
440 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200442 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200443 break;
444 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200445 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100446 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200447 return LY_EVALID;
448 }
449
450 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100451 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 +0200452
Radek Krejcif13b87b2020-12-01 22:02:17 +0100453 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100454 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200455 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100456 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100457 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200458 case '+':
459 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100460 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200461 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200462 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200463 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100464 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100465 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200466 case ' ':
467 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200468 /* just skip */
469 break;
470 default:
471 /* string is finished */
472 goto string_end;
473 }
Radek Krejci33090f92020-12-17 20:12:46 +0100474 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200475 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100476 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100477 switch (ctx->in->current[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200478 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100479 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100480 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200481 case ' ':
482 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200483 /* skip */
484 break;
485 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100486 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 break;
488 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100489 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200490 break;
491 default:
492 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200493 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200494 return LY_EVALID;
495 }
Radek Krejci33090f92020-12-17 20:12:46 +0100496 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200497 break;
498 default:
499 return LY_EINT;
500 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200501 }
502
503string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200504 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200505 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200506 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200507 return LY_EVALID;
508 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200509 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100510
511#undef STRING_ENDED
512#undef STRING_SINGLE_QUOTED
513#undef STRING_DOUBLE_QUOTED
514#undef STRING_DOUBLE_QUOTED_ESCAPED
515#undef STRING_PAUSED_NEXTSTRING
516#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200517}
518
Michal Vaskoea5abea2018-09-18 13:10:54 +0200519/**
520 * @brief Get another YANG string from the raw data.
521 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200522 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200523 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200524 * @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 +0200525 * @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 +0200526 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
527 * set to NULL. Otherwise equal to \p word_p.
528 * @param[out] word_len Length of the read string.
529 *
530 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200531 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200532LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100533get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200534 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200535{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200536 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200537 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200538
Michal Vasko7fbc8162018-09-17 10:35:16 +0200539 /* word buffer - dynamically allocated */
540 *word_b = NULL;
541
542 /* word pointer - just a pointer to data */
543 *word_p = NULL;
544
545 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100546 while (ctx->in->current[0]) {
547 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200548 case '\'':
549 case '\"':
550 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200551 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100552 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200553 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200554 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200555 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200556 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100557 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200558 }
Radek Krejci33090f92020-12-17 20:12:46 +0100559 LY_CHECK_RET(read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560 goto str_end;
561 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100562 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200563 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100564 MOVE_INPUT(ctx, 2);
565 LY_CHECK_RET(skip_comment(ctx, 1));
566 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200567 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100568 MOVE_INPUT(ctx, 2);
569 LY_CHECK_RET(skip_comment(ctx, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200570 } else {
571 /* not a comment after all */
Radek Krejci33090f92020-12-17 20:12:46 +0100572 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 +0200573 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200574 break;
575 case ' ':
576 if (*word_len) {
577 /* word is finished */
578 goto str_end;
579 }
Radek Krejci33090f92020-12-17 20:12:46 +0100580 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200581 break;
582 case '\t':
583 if (*word_len) {
584 /* word is finished */
585 goto str_end;
586 }
587 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100588 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200589
Radek Krejci33090f92020-12-17 20:12:46 +0100590 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200591 break;
592 case '\n':
593 if (*word_len) {
594 /* word is finished */
595 goto str_end;
596 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100597 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100598 MOVE_INPUT(ctx, 1);
599
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200601 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200602 break;
603 case ';':
604 case '{':
605 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
606 /* word is finished */
607 goto str_end;
608 }
609
Radek Krejci33090f92020-12-17 20:12:46 +0100610 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200611 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200612 case '}':
613 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100614 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200615 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200616 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200617 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100618 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 +0200619 break;
620 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200621 }
622
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200623 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200624 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200625 return LY_EVALID;
626
Michal Vasko7fbc8162018-09-17 10:35:16 +0200627str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200628 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200629 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200630 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200631 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200632 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200633 *word_p = *word_b;
634 }
635
636 return LY_SUCCESS;
637}
638
Michal Vaskoea5abea2018-09-18 13:10:54 +0200639/**
640 * @brief Get another YANG keyword from the raw data.
641 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200642 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200643 * @param[out] kw YANG keyword read.
644 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
645 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
646 *
647 * @return LY_ERR values.
648 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200649LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100650get_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 +0200651{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200652 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200653 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200654 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655
656 if (word_p) {
657 *word_p = NULL;
658 *word_len = 0;
659 }
660
661 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100662 while (ctx->in->current[0]) {
663 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200664 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100665 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200666 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100667 MOVE_INPUT(ctx, 2);
668 LY_CHECK_RET(skip_comment(ctx, 1));
669 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100671 MOVE_INPUT(ctx, 2);
672 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200673 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200674 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200675 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200676 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200677 }
Radek Krejci13028282019-06-11 14:56:48 +0200678 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200679 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200680 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100681 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200682 ctx->indent = 0;
683 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200684 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200685 /* skip whitespaces (optsep) */
686 ++ctx->indent;
687 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100690 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200691 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100692 case '\r':
693 /* possible CRLF endline */
694 if (ctx->in->current[1] == '\n') {
695 break;
696 }
697 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200698 default:
699 /* either a keyword start or an invalid character */
700 goto keyword_start;
701 }
702
Radek Krejci33090f92020-12-17 20:12:46 +0100703 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200704 }
705
706keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100707 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100708 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200709
Michal Vasko69730152020-10-09 16:30:07 +0200710 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 +0200711 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200712 }
713
Radek Krejcid6b76452019-09-03 17:03:03 +0200714 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100716 switch (ctx->in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200717 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200718 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200719 case ' ':
720 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200722 case ':':
723 /* keyword is not actually a keyword, but prefix of an extension.
724 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
725 * and we will be checking the keyword (extension instance) itself */
726 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100727 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200728 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200729 case '{':
730 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200731 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200732 break;
733 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100734 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200735 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100736 MOVE_INPUT(ctx, 1);
737 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200738 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200739 return LY_EVALID;
740 }
741 } else {
742 /* still can be an extension */
743 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200744extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100745 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
746 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '{') && (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200747 uint32_t c = 0;
748
Radek Krejci33090f92020-12-17 20:12:46 +0100749 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
750 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200751 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200752 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200753 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100754 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200755 }
Radek Krejci33090f92020-12-17 20:12:46 +0100756 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200757 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200758 return LY_EVALID;
759 }
760
761 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200762 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100763 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200764 return LY_EVALID;
765 }
766
Radek Krejcid6b76452019-09-03 17:03:03 +0200767 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200768 }
Radek Krejci626df482018-10-11 15:06:31 +0200769success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200770 if (word_p) {
771 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100772 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200773 }
774
775 return LY_SUCCESS;
776}
777
Michal Vaskoea5abea2018-09-18 13:10:54 +0200778/**
779 * @brief Parse extension instance substatements.
780 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200781 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200782 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200783 * @param[in] word Extension instance substatement name (keyword).
784 * @param[in] word_len Extension instance substatement name length.
785 * @param[in,out] child Children of this extension instance to add to.
786 *
787 * @return LY_ERR values.
788 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200789static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100790parse_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 +0200791 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200792{
793 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100794 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200795 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200796 struct lysp_stmt *stmt, *par_child;
797
798 stmt = calloc(1, sizeof *stmt);
799 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
800
Radek Krejcibb9b1982019-04-08 14:24:59 +0200801 /* insert into parent statements */
802 if (!*child) {
803 *child = stmt;
804 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200805 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200806 par_child->next = stmt;
807 }
808
Michal Vaskofc2cd072021-02-24 13:17:17 +0100809 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200810 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200811
812 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100813 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200814 if (word) {
815 if (buf) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200816 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200817 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200818 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200819 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200820 }
821
Michal Vaskofc2cd072021-02-24 13:17:17 +0100822 stmt->format = LY_PREF_SCHEMA;
823 stmt->prefix_data = ctx->parsed_mod;
824 stmt->kw = kw;
825
Radek Krejci33090f92020-12-17 20:12:46 +0100826 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, ) {
827 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200828 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200829 return ret;
830}
831
Michal Vaskoea5abea2018-09-18 13:10:54 +0200832/**
833 * @brief Parse extension instance.
834 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200835 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200836 * @param[in] ext_name Extension instance substatement name (keyword).
837 * @param[in] ext_name_len Extension instance substatement name length.
Radek Krejcifc596f92021-02-26 22:40:26 +0100838 * @param[in] insubstmt The statement this extension instance is a substatement of.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200839 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
840 * @param[in,out] exts Extension instances to add to.
841 *
842 * @return LY_ERR values.
843 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200844static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100845parse_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 +0200846 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200847{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100848 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200849 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200850 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200851 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200852 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200853
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200854 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200855
Michal Vaskofc2cd072021-02-24 13:17:17 +0100856 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
857 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
858 return LY_EVALID;
859 }
860
861 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200862 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200863
864 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100865 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200866 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200867 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200868 }
869
Michal Vaskofc2cd072021-02-24 13:17:17 +0100870 /* store the rest of information */
871 e->format = LY_PREF_SCHEMA;
872 e->prefix_data = ctx->parsed_mod;
Radek Krejciab430862021-03-02 20:13:40 +0100873 e->parent_stmt = insubstmt;
874 e->parent_stmt_index = insubstmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100875
Radek Krejci33090f92020-12-17 20:12:46 +0100876 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
877 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200879 return ret;
880}
881
Michal Vaskoea5abea2018-09-18 13:10:54 +0200882/**
883 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
884 * description, etc...
885 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200886 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200887 * @param[in] substmt Type of this substatement.
888 * @param[in] substmt_index Index of this substatement.
889 * @param[in,out] value Place to store the parsed value.
890 * @param[in] arg Type of the YANG keyword argument (of the value).
891 * @param[in,out] exts Extension instances to add to.
892 *
893 * @return LY_ERR values.
894 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100896parse_text_field(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200897 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200898{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100899 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200900 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200901 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200902 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200903
904 if (*value) {
Radek Krejci3972b332021-03-02 16:34:31 +0100905 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200906 return LY_EVALID;
907 }
908
909 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100910 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200911
912 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200913 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200914
Radek Krejci33090f92020-12-17 20:12:46 +0100915 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200917 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100918 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200919 break;
920 default:
Radek Krejci3972b332021-03-02 16:34:31 +0100921 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200922 return LY_EVALID;
923 }
924 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200925 return ret;
926}
927
Michal Vaskoea5abea2018-09-18 13:10:54 +0200928/**
929 * @brief Parse the yang-version statement.
930 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200931 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100932 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200933 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200934 *
935 * @return LY_ERR values.
936 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200937static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100938parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100940 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200941 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200942 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200943 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200944
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100945 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200946 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200947 return LY_EVALID;
948 }
949
950 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100951 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200952
Radek Krejci96e48da2020-09-04 13:18:06 +0200953 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100954 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100955 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100956 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200957 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200958 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200959 free(buf);
960 return LY_EVALID;
961 }
962 free(buf);
963
Radek Krejci33090f92020-12-17 20:12:46 +0100964 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200965 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200966 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +0100967 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968 break;
969 default:
David Sedlákb3077192019-06-19 10:55:37 +0200970 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200971 return LY_EVALID;
972 }
973 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200974 return ret;
975}
976
Michal Vaskoea5abea2018-09-18 13:10:54 +0200977/**
978 * @brief Parse the belongs-to statement.
979 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200980 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200981 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
982 * @param[in,out] exts Extension instances to add to.
983 *
984 * @return LY_ERR values.
985 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200986static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100987parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100989 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200991 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200992 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200993
Michal Vaskoc3781c32020-10-06 14:04:08 +0200994 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200995 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996 return LY_EVALID;
997 }
998
Michal Vaskoc3781c32020-10-06 14:04:08 +0200999 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001000 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001001 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001002 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 +02001003 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001004 free(buf);
1005 return LY_EVALID;
1006 }
1007 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001008
Radek Krejci33090f92020-12-17 20:12:46 +01001009 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001010 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001011 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001012 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001013 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001014 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001015 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_BELONGS_TO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001016 break;
1017 default:
David Sedlákb3077192019-06-19 10:55:37 +02001018 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001019 return LY_EVALID;
1020 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001021 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001022 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001023checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001024 /* mandatory substatements */
1025 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001026 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001027 return LY_EVALID;
1028 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001029 return ret;
1030}
1031
Michal Vaskoea5abea2018-09-18 13:10:54 +02001032/**
1033 * @brief Parse the revision-date statement.
1034 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001035 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001036 * @param[in,out] rev Array to store the parsed value in.
1037 * @param[in,out] exts Extension instances to add to.
1038 *
1039 * @return LY_ERR values.
1040 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001041static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001042parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001043{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001044 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001045 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001046 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001047 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001048
1049 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001050 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051 return LY_EVALID;
1052 }
1053
1054 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001055 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056
1057 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001058 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001059 free(buf);
1060 return LY_EVALID;
1061 }
1062
1063 /* store value and spend buf if allocated */
1064 strncpy(rev, word, word_len);
1065 free(buf);
1066
Radek Krejci33090f92020-12-17 20:12:46 +01001067 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001068 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001069 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001070 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071 break;
1072 default:
David Sedlákb3077192019-06-19 10:55:37 +02001073 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001074 return LY_EVALID;
1075 }
1076 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001077 return ret;
1078}
1079
Michal Vaskoea5abea2018-09-18 13:10:54 +02001080/**
1081 * @brief Parse the include statement.
1082 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001083 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001084 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001085 * @param[in,out] includes Parsed includes to add to.
1086 *
1087 * @return LY_ERR values.
1088 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001090parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001091{
Radek Krejcid33273d2018-10-25 14:55:52 +02001092 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001093 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001094 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001095 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001096 struct lysp_include *inc;
1097
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001098 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001099
1100 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001101 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102
Radek Krejci011e4aa2020-09-04 15:22:31 +02001103 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001104
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001105 /* submodules share the namespace with the module names, so there must not be
1106 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001107 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001108 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001109 return LY_EVALID;
1110 }
1111
Radek Krejci33090f92020-12-17 20:12:46 +01001112 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001114 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001115 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001116 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 +02001117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001118 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001119 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001120 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 +02001121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001122 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001123 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001125 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001126 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127 break;
1128 default:
David Sedlákb3077192019-06-19 10:55:37 +02001129 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130 return LY_EVALID;
1131 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001133 return ret;
1134}
1135
Michal Vaskoea5abea2018-09-18 13:10:54 +02001136/**
1137 * @brief Parse the import statement.
1138 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001139 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001140 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001141 * @param[in,out] imports Parsed imports to add to.
1142 *
1143 * @return LY_ERR values.
1144 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001146parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001148 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001150 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001151 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001152 struct lysp_import *imp;
1153
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001154 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155
1156 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001157 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001158 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001159
Radek Krejci33090f92020-12-17 20:12:46 +01001160 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001162 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001163 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 +01001164 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 +02001165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001166 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001167 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001168 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 +02001169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001170 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001171 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001172 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 +02001173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001174 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001175 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001177 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001178 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001179 break;
1180 default:
David Sedlákb3077192019-06-19 10:55:37 +02001181 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182 return LY_EVALID;
1183 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001185 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001186checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001187 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001188 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189
1190 return ret;
1191}
1192
Michal Vaskoea5abea2018-09-18 13:10:54 +02001193/**
1194 * @brief Parse the revision statement.
1195 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001196 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001197 * @param[in,out] revs Parsed revisions to add to.
1198 *
1199 * @return LY_ERR values.
1200 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001201static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001202parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001203{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001204 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001205 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001206 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001207 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208 struct lysp_revision *rev;
1209
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001210 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211
1212 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001213 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214
1215 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001216 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001217 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218 return LY_EVALID;
1219 }
1220
Radek Krejcib7db73a2018-10-24 14:18:40 +02001221 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001222 free(buf);
1223
Radek Krejci33090f92020-12-17 20:12:46 +01001224 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001225 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001226 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001227 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 +02001228 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001229 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001230 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 +02001231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001232 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001233 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234 break;
1235 default:
David Sedlákb3077192019-06-19 10:55:37 +02001236 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237 return LY_EVALID;
1238 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240 return ret;
1241}
1242
Michal Vaskoea5abea2018-09-18 13:10:54 +02001243/**
1244 * @brief Parse a generic text field that can have more instances such as base.
1245 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001246 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001247 * @param[in] substmt Type of this substatement.
1248 * @param[in,out] texts Parsed values to add to.
1249 * @param[in] arg Type of the expected argument.
1250 * @param[in,out] exts Extension instances to add to.
1251 *
1252 * @return LY_ERR values.
1253 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001255parse_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 +02001256 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001258 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001259 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001260 const char **item;
1261 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001262 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263
1264 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001265 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266
1267 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001268 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001269
Radek Krejci011e4aa2020-09-04 15:22:31 +02001270 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Radek Krejci33090f92020-12-17 20:12:46 +01001271 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001272 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001273 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001274 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001275 break;
1276 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001277 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001278 return LY_EVALID;
1279 }
1280 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001281 return ret;
1282}
1283
Michal Vaskoea5abea2018-09-18 13:10:54 +02001284/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001285 * @brief Parse a generic text field that can have more instances such as base.
1286 *
1287 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001288 * @param[in] substmt Type of this substatement.
1289 * @param[in,out] qnames Parsed qnames to add to.
1290 * @param[in] arg Type of the expected argument.
1291 * @param[in,out] exts Extension instances to add to.
1292 *
1293 * @return LY_ERR values.
1294 */
1295static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001296parse_qnames(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001297 enum yang_arg arg, struct lysp_ext_instance **exts)
1298{
1299 LY_ERR ret = LY_SUCCESS;
1300 char *buf, *word;
1301 struct lysp_qname *item;
1302 size_t word_len;
1303 enum ly_stmt kw;
1304
1305 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001306 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001307
1308 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001309 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001310
1311 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001312 item->mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001313 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001314 switch (kw) {
1315 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001316 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001317 break;
1318 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001319 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001320 return LY_EVALID;
1321 }
1322 }
1323 return ret;
1324}
1325
1326/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001327 * @brief Parse the config statement.
1328 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001329 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001330 * @param[in,out] flags Flags to add to.
1331 * @param[in,out] exts Extension instances to add to.
1332 *
1333 * @return LY_ERR values.
1334 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001335static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001336parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001338 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001340 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001341 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001342
1343 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001344 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345 return LY_EVALID;
1346 }
1347
1348 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001349 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001350
Radek Krejcif13b87b2020-12-01 22:02:17 +01001351 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001352 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001353 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001354 *flags |= LYS_CONFIG_R;
1355 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001356 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001357 free(buf);
1358 return LY_EVALID;
1359 }
1360 free(buf);
1361
Radek Krejci33090f92020-12-17 20:12:46 +01001362 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001363 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001364 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001365 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366 break;
1367 default:
David Sedlákb3077192019-06-19 10:55:37 +02001368 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001369 return LY_EVALID;
1370 }
1371 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001372 return ret;
1373}
1374
Michal Vaskoea5abea2018-09-18 13:10:54 +02001375/**
1376 * @brief Parse the mandatory statement.
1377 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001378 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001379 * @param[in,out] flags Flags to add to.
1380 * @param[in,out] exts Extension instances to add to.
1381 *
1382 * @return LY_ERR values.
1383 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001384static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001385parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001387 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001389 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001390 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001391
1392 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001393 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001394 return LY_EVALID;
1395 }
1396
1397 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001398 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001399
Radek Krejcif13b87b2020-12-01 22:02:17 +01001400 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001402 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403 *flags |= LYS_MAND_FALSE;
1404 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001405 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001406 free(buf);
1407 return LY_EVALID;
1408 }
1409 free(buf);
1410
Radek Krejci33090f92020-12-17 20:12:46 +01001411 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001412 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001413 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001414 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415 break;
1416 default:
David Sedlákb3077192019-06-19 10:55:37 +02001417 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001418 return LY_EVALID;
1419 }
1420 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001421 return ret;
1422}
1423
Michal Vaskoea5abea2018-09-18 13:10:54 +02001424/**
1425 * @brief Parse a restriction such as range or length.
1426 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001427 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001428 * @param[in] restr_kw Type of this particular restriction.
1429 * @param[in,out] exts Extension instances to add to.
1430 *
1431 * @return LY_ERR values.
1432 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001433static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001434parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001436 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001438 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001439 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440
1441 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001442 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443
Michal Vaskob36053d2020-03-26 15:49:30 +01001444 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001445 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001446 restr->arg.mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001447 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001449 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001450 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 +02001451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001452 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001453 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 +02001454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001455 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01001456 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 +02001457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001458 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001459 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 +02001460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001461 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001462 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463 break;
1464 default:
David Sedlákb3077192019-06-19 10:55:37 +02001465 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466 return LY_EVALID;
1467 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001469 return ret;
1470}
1471
Michal Vaskoea5abea2018-09-18 13:10:54 +02001472/**
1473 * @brief Parse a restriction that can have more instances such as must.
1474 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001475 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001476 * @param[in] restr_kw Type of this particular restriction.
1477 * @param[in,out] restrs Restrictions to add to.
1478 *
1479 * @return LY_ERR values.
1480 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001482parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483{
1484 struct lysp_restr *restr;
1485
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001486 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001487 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488}
1489
Michal Vaskoea5abea2018-09-18 13:10:54 +02001490/**
1491 * @brief Parse the status statement.
1492 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001493 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001494 * @param[in,out] flags Flags to add to.
1495 * @param[in,out] exts Extension instances to add to.
1496 *
1497 * @return LY_ERR values.
1498 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001499static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001500parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001502 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001504 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001505 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001506
1507 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001508 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001509 return LY_EVALID;
1510 }
1511
1512 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001513 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514
Radek Krejcif13b87b2020-12-01 22:02:17 +01001515 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001517 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001518 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001519 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 *flags |= LYS_STATUS_OBSLT;
1521 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001522 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001523 free(buf);
1524 return LY_EVALID;
1525 }
1526 free(buf);
1527
Radek Krejci33090f92020-12-17 20:12:46 +01001528 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001529 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001530 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001531 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001532 break;
1533 default:
David Sedlákb3077192019-06-19 10:55:37 +02001534 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535 return LY_EVALID;
1536 }
1537 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538 return ret;
1539}
1540
Michal Vaskoea5abea2018-09-18 13:10:54 +02001541/**
1542 * @brief Parse the when statement.
1543 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001544 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001545 * @param[in,out] when_p When pointer to parse to.
1546 *
1547 * @return LY_ERR values.
1548 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001549LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001550parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001552 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001554 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001555 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556 struct lysp_when *when;
1557
1558 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001559 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001560 return LY_EVALID;
1561 }
1562
1563 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001564 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001565 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001566
1567 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001568 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001569 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001570 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571
Radek Krejci33090f92020-12-17 20:12:46 +01001572 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001573 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001574 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001575 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 +02001576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001577 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001578 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 +02001579 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001580 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001581 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_WHEN, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001582 break;
1583 default:
David Sedlákb3077192019-06-19 10:55:37 +02001584 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001585 return LY_EVALID;
1586 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001587 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588 return ret;
1589}
1590
Michal Vaskoea5abea2018-09-18 13:10:54 +02001591/**
1592 * @brief Parse the anydata or anyxml statement.
1593 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001594 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001595 * @param[in] any_kw Type of this particular keyword.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001596 * @param[in,out] siblings Siblings to add to.
1597 *
1598 * @return LY_ERR values.
1599 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001600LY_ERR
Radek Krejci39b7fc22021-02-26 23:29:18 +01001601parse_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 +02001602{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001603 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001605 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001607 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001608
David Sedlák60adc092019-08-06 15:57:02 +02001609 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001610 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001611
Radek Krejci39b7fc22021-02-26 23:29:18 +01001612 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001613 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001614
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001616 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001617 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001618
1619 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01001620 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001622 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001623 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001626 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 +02001627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001628 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001629 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001631 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001632 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001634 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001635 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001637 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001638 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 +02001639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001640 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001641 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001642 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001643 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001644 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001646 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001647 LY_CHECK_RET(parse_ext(ctx, word, word_len, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001648 break;
1649 default:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001650 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001651 return LY_EVALID;
1652 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001654 return ret;
1655}
1656
Michal Vaskoea5abea2018-09-18 13:10:54 +02001657/**
1658 * @brief Parse the value or position statement. Substatement of type enum statement.
1659 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001660 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001661 * @param[in] val_kw Type of this particular keyword.
1662 * @param[in,out] value Value to write to.
1663 * @param[in,out] flags Flags to write to.
1664 * @param[in,out] exts Extension instances to add to.
1665 *
1666 * @return LY_ERR values.
1667 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001668LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001669parse_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 +02001670 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001672 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001674 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001675 long int num = 0;
1676 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001677 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001678
1679 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001680 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681 return LY_EVALID;
1682 }
1683 *flags |= LYS_SET_VALUE;
1684
1685 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001686 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687
Radek Krejcid6b76452019-09-03 17:03:03 +02001688 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 +02001689 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001690 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001691 }
1692
1693 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001694 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001695 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001696 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001697 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001698 goto error;
1699 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001700 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001701 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001702 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001703 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001704 goto error;
1705 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001706 }
1707 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001708 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001709 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001710 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001711 }
1712 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001713 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001714 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001716 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001717 *value = num;
1718 } else {
1719 *value = unum;
1720 }
1721 free(buf);
1722
Radek Krejci33090f92020-12-17 20:12:46 +01001723 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001725 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001726 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 +02001727 break;
1728 default:
David Sedlákb3077192019-06-19 10:55:37 +02001729 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001730 return LY_EVALID;
1731 }
1732 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001734
1735error:
1736 free(buf);
1737 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738}
1739
Michal Vaskoea5abea2018-09-18 13:10:54 +02001740/**
1741 * @brief Parse the enum or bit statement. Substatement of type statement.
1742 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001743 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001744 * @param[in] enum_kw Type of this particular keyword.
1745 * @param[in,out] enums Enums or bits to add to.
1746 *
1747 * @return LY_ERR values.
1748 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001749static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001750parse_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 +02001751{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001752 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001754 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001755 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756 struct lysp_type_enum *enm;
1757
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001758 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759
1760 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001761 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 +02001762 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001763 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001764 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001765 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001766
Radek Krejci011e4aa2020-09-04 15:22:31 +02001767 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001768 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1769
Radek Krejci33090f92020-12-17 20:12:46 +01001770 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001772 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001773 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 +02001774 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001775 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001776 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001777 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001778 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001779 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001780 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 +02001781 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001782 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001783 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001784 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001785 case LY_STMT_VALUE:
1786 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 +02001787 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001788 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001789 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001790 case LY_STMT_POSITION:
1791 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 +02001792 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001793 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001795 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001796 LY_CHECK_RET(parse_ext(ctx, word, word_len, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797 break;
1798 default:
David Sedlákb3077192019-06-19 10:55:37 +02001799 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001800 return LY_EVALID;
1801 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001802 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001803 return ret;
1804}
1805
Michal Vaskoea5abea2018-09-18 13:10:54 +02001806/**
1807 * @brief Parse the fraction-digits statement. Substatement of type statement.
1808 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001809 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001810 * @param[in,out] fracdig Value to write to.
1811 * @param[in,out] exts Extension instances to add to.
1812 *
1813 * @return LY_ERR values.
1814 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001816parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001817{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001818 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001820 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001822 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001823
1824 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001825 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001826 return LY_EVALID;
1827 }
1828
1829 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001830 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001831
1832 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001833 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001834 free(buf);
1835 return LY_EVALID;
1836 }
1837
1838 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001839 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001841 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001842 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 free(buf);
1844 return LY_EVALID;
1845 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001846 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001847 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 free(buf);
1849 return LY_EVALID;
1850 }
1851 *fracdig = num;
1852 free(buf);
1853
Radek Krejci33090f92020-12-17 20:12:46 +01001854 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001855 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001856 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001857 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FRACTION_DIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001858 break;
1859 default:
David Sedlákb3077192019-06-19 10:55:37 +02001860 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001861 return LY_EVALID;
1862 }
1863 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001864 return ret;
1865}
1866
Michal Vaskoea5abea2018-09-18 13:10:54 +02001867/**
1868 * @brief Parse the require-instance statement. Substatement of type statement.
1869 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001870 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001871 * @param[in,out] reqinst Value to write to.
1872 * @param[in,out] flags Flags to write to.
1873 * @param[in,out] exts Extension instances to add to.
1874 *
1875 * @return LY_ERR values.
1876 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001878parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001879 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001880{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001881 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001882 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001883 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001884 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885
1886 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001887 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888 return LY_EVALID;
1889 }
1890 *flags |= LYS_SET_REQINST;
1891
1892 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001893 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001894
Radek Krejcif13b87b2020-12-01 22:02:17 +01001895 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001897 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001898 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 free(buf);
1900 return LY_EVALID;
1901 }
1902 free(buf);
1903
Radek Krejci33090f92020-12-17 20:12:46 +01001904 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001905 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001906 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001907 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REQUIRE_INSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908 break;
1909 default:
David Sedlákb3077192019-06-19 10:55:37 +02001910 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001911 return LY_EVALID;
1912 }
1913 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001914 return ret;
1915}
1916
Michal Vaskoea5abea2018-09-18 13:10:54 +02001917/**
1918 * @brief Parse the modifier statement. Substatement of type pattern statement.
1919 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001920 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001921 * @param[in,out] pat Value to write to.
1922 * @param[in,out] exts Extension instances to add to.
1923 *
1924 * @return LY_ERR values.
1925 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001927parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001929 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001930 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001931 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001932 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001933
Radek Krejcif13b87b2020-12-01 22:02:17 +01001934 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001935 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001936 return LY_EVALID;
1937 }
1938
1939 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001940 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001941
Radek Krejcif13b87b2020-12-01 22:02:17 +01001942 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001943 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001944 free(buf);
1945 return LY_EVALID;
1946 }
1947 free(buf);
1948
1949 /* replace the value in the dictionary */
1950 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001951 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001952 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001953 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001954
Radek Krejcif13b87b2020-12-01 22:02:17 +01001955 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
1956 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001957 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001958
Radek Krejci33090f92020-12-17 20:12:46 +01001959 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001961 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001962 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001963 break;
1964 default:
David Sedlákb3077192019-06-19 10:55:37 +02001965 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001966 return LY_EVALID;
1967 }
1968 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969 return ret;
1970}
1971
Michal Vaskoea5abea2018-09-18 13:10:54 +02001972/**
1973 * @brief Parse the pattern statement. Substatement of type statement.
1974 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001975 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001976 * @param[in,out] patterns Restrictions to add to.
1977 *
1978 * @return LY_ERR values.
1979 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001980static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001981parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001982{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001983 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001985 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001986 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987 struct lysp_restr *restr;
1988
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001989 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990
1991 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001992 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001993
1994 /* add special meaning first byte */
1995 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001996 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001997 word = buf;
1998 } else {
1999 buf = malloc(word_len + 2);
2000 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002001 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002002 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002003 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002004 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002005 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
2006 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002007
Radek Krejci33090f92020-12-17 20:12:46 +01002008 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002009 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002010 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002011 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 +02002012 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002013 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002014 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 +02002015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002016 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01002017 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 +02002018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002019 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002020 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 +02002021 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002022 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002023 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002024 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002026 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002027 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028 break;
2029 default:
David Sedlákb3077192019-06-19 10:55:37 +02002030 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002031 return LY_EVALID;
2032 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034 return ret;
2035}
2036
Michal Vaskoea5abea2018-09-18 13:10:54 +02002037/**
2038 * @brief Parse the type statement.
2039 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002040 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002041 * @param[in,out] type Type to wrote to.
2042 *
2043 * @return LY_ERR values.
2044 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002046parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002048 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002050 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002051 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002052 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053 struct lysp_type *nest_type;
2054
2055 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002056 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002057 return LY_EVALID;
2058 }
2059
2060 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002061 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002062 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002063
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002064 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002065 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002066
Radek Krejci33090f92020-12-17 20:12:46 +01002067 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002069 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002070 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 +01002071 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002072 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002073 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002074 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002075 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002077 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002078 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002079 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002081 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002082 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002083 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002084 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002085 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002087 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088 return LY_EVALID;
2089 }
2090 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002091 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092
Radek Krejci33090f92020-12-17 20:12:46 +01002093 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002094 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002096 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002097 if (type->path) {
Radek Krejci3972b332021-03-02 16:34:31 +01002098 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002099 return LY_EVALID;
2100 }
2101
Radek Krejcifc596f92021-02-26 22:40:26 +01002102 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 +02002103 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 +02002104 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002105 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002106 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002107 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002109 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002110 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002111 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002113 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002115 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116 return LY_EVALID;
2117 }
2118 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002119 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120
Radek Krejci33090f92020-12-17 20:12:46 +01002121 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002122 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002124 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002125 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002126 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002128 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002129 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002130 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002131 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002133 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002134 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 break;
2136 default:
David Sedlákb3077192019-06-19 10:55:37 +02002137 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002138 return LY_EVALID;
2139 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002140 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141 return ret;
2142}
2143
Michal Vaskoea5abea2018-09-18 13:10:54 +02002144/**
2145 * @brief Parse the leaf statement.
2146 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002147 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002148 * @param[in,out] siblings Siblings to add to.
2149 *
2150 * @return LY_ERR values.
2151 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002152LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002153parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002155 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002157 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002158 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 struct lysp_node_leaf *leaf;
2160
David Sedlák60adc092019-08-06 15:57:02 +02002161 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002162 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002164 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002167 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002168 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169
2170 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002171 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002172 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002173 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002174 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002176 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002177 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 +02002178 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002180 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002181 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 +02002182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002183 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002184 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002186 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002187 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002189 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002190 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002192 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002193 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 +02002194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002195 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002196 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002198 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002199 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002201 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002202 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 +02002203 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002204 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002205 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002206 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002207 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002208 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209 break;
2210 default:
David Sedlákb3077192019-06-19 10:55:37 +02002211 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 return LY_EVALID;
2213 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002215 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002216checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002217 /* mandatory substatements */
2218 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002219 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 return LY_EVALID;
2221 }
2222
2223 return ret;
2224}
2225
Michal Vaskoea5abea2018-09-18 13:10:54 +02002226/**
2227 * @brief Parse the max-elements statement.
2228 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002229 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002230 * @param[in,out] max Value to write to.
2231 * @param[in,out] flags Flags to write to.
2232 * @param[in,out] exts Extension instances to add to.
2233 *
2234 * @return LY_ERR values.
2235 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002236LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002237parse_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 +02002238{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002239 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002241 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002242 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002243 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002244
2245 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002246 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002247 return LY_EVALID;
2248 }
2249 *flags |= LYS_SET_MAX;
2250
2251 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002252 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253
2254 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002255 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256 free(buf);
2257 return LY_EVALID;
2258 }
2259
Radek Krejci7f9b6512019-09-18 13:11:09 +02002260 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002261 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002262 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002264 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002265 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002266 free(buf);
2267 return LY_EVALID;
2268 }
2269 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002270 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002271 free(buf);
2272 return LY_EVALID;
2273 }
2274
2275 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002276 } else {
2277 /* unbounded */
2278 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 }
2280 free(buf);
2281
Radek Krejci33090f92020-12-17 20:12:46 +01002282 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002284 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002285 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MAX_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 break;
2287 default:
David Sedlákb3077192019-06-19 10:55:37 +02002288 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002289 return LY_EVALID;
2290 }
2291 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002292 return ret;
2293}
2294
Michal Vaskoea5abea2018-09-18 13:10:54 +02002295/**
2296 * @brief Parse the min-elements statement.
2297 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002298 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002299 * @param[in,out] min Value to write to.
2300 * @param[in,out] flags Flags to write to.
2301 * @param[in,out] exts Extension instances to add to.
2302 *
2303 * @return LY_ERR values.
2304 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002305LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002306parse_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 +02002307{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002308 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002310 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002312 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313
2314 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002315 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 return LY_EVALID;
2317 }
2318 *flags |= LYS_SET_MIN;
2319
2320 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002321 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002322
2323 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002324 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002325 free(buf);
2326 return LY_EVALID;
2327 }
2328
2329 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002330 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002332 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002333 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 free(buf);
2335 return LY_EVALID;
2336 }
2337 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002338 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002339 free(buf);
2340 return LY_EVALID;
2341 }
2342 *min = num;
2343 free(buf);
2344
Radek Krejci33090f92020-12-17 20:12:46 +01002345 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002347 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002348 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MIN_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349 break;
2350 default:
David Sedlákb3077192019-06-19 10:55:37 +02002351 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002352 return LY_EVALID;
2353 }
2354 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002355 return ret;
2356}
2357
Michal Vaskoea5abea2018-09-18 13:10:54 +02002358/**
2359 * @brief Parse the ordered-by statement.
2360 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002361 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002362 * @param[in,out] flags Flags to write to.
2363 * @param[in,out] exts Extension instances to add to.
2364 *
2365 * @return LY_ERR values.
2366 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002368parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002370 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002371 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002372 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002373 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002374
2375 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002376 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002377 return LY_EVALID;
2378 }
2379
2380 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002381 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382
Radek Krejcif13b87b2020-12-01 22:02:17 +01002383 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002384 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002385 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002386 *flags |= LYS_ORDBY_USER;
2387 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002388 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002389 free(buf);
2390 return LY_EVALID;
2391 }
2392 free(buf);
2393
Radek Krejci33090f92020-12-17 20:12:46 +01002394 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002395 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002396 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002397 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ORDERED_BY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002398 break;
2399 default:
David Sedlákb3077192019-06-19 10:55:37 +02002400 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002401 return LY_EVALID;
2402 }
2403 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002404 return ret;
2405}
2406
Michal Vaskoea5abea2018-09-18 13:10:54 +02002407/**
2408 * @brief Parse the leaf-list statement.
2409 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002410 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002411 * @param[in,out] siblings Siblings to add to.
2412 *
2413 * @return LY_ERR values.
2414 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002415LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002416parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002417{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002418 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002420 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002421 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 struct lysp_node_leaflist *llist;
2423
David Sedlák60adc092019-08-06 15:57:02 +02002424 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002425 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002427 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428
Michal Vasko7fbc8162018-09-17 10:35:16 +02002429 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002430 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002431 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432
2433 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002434 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002436 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002437 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002439 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002440 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002441 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002443 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002444 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 +02002445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002446 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002447 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002449 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002450 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002452 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002453 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002455 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002456 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002458 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002459 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002461 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002462 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 +02002463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002464 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002465 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002467 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002468 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002470 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002471 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 +02002472 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002473 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002474 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002476 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002477 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 break;
2479 default:
David Sedlákb3077192019-06-19 10:55:37 +02002480 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002481 return LY_EVALID;
2482 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002484 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002485checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002486 /* mandatory substatements */
2487 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002488 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002489 return LY_EVALID;
2490 }
2491
2492 return ret;
2493}
2494
Michal Vaskoea5abea2018-09-18 13:10:54 +02002495/**
2496 * @brief Parse the refine statement.
2497 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002498 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002499 * @param[in,out] refines Refines to add to.
2500 *
2501 * @return LY_ERR values.
2502 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002504parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002506 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002508 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002509 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510 struct lysp_refine *rf;
2511
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002512 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513
2514 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002515 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002516 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002517 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518
Radek Krejci33090f92020-12-17 20:12:46 +01002519 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002520 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002521 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002522 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002524 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002525 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002526 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002527 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002528 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 +02002529 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002530 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002531 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002532 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002534 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002535 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002536 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002537 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002538 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002540 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002541 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002543 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002544 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002545 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002546 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002547 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 +02002548 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002549 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002550 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 +02002551 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002552 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002553 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554 break;
2555 default:
David Sedlákb3077192019-06-19 10:55:37 +02002556 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 return LY_EVALID;
2558 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002559 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 return ret;
2561}
2562
Michal Vaskoea5abea2018-09-18 13:10:54 +02002563/**
2564 * @brief Parse the typedef statement.
2565 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002566 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002567 * @param[in,out] typedefs Typedefs to add to.
2568 *
2569 * @return LY_ERR values.
2570 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002572parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002573{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002574 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002576 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002577 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578 struct lysp_tpdf *tpdf;
2579
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002580 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581
2582 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002583 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002584 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002585
2586 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002587 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002588 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002589 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002590 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 +02002591 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002593 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002594 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 +02002595 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002596 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002597 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 +02002598 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002599 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002600 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002601 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002602 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002603 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002604 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002605 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002606 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 +02002607 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002608 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002609 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610 break;
2611 default:
David Sedlákb3077192019-06-19 10:55:37 +02002612 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002613 return LY_EVALID;
2614 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002616 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002617checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 /* mandatory substatements */
2619 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002620 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 return LY_EVALID;
2622 }
2623
Radek Krejcibbe09a92018-11-08 09:36:54 +01002624 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002625 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002626 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002627 }
2628
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 return ret;
2630}
2631
Michal Vaskoea5abea2018-09-18 13:10:54 +02002632/**
2633 * @brief Parse the input or output statement.
2634 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002635 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002636 * @param[in] kw Type of this particular keyword
2637 * @param[in,out] inout_p Input/output pointer to write to.
2638 *
2639 * @return LY_ERR values.
2640 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002642parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002643 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002645 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002647 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002648 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002649 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002651 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002652 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653 return LY_EVALID;
2654 }
2655
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002656 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002657 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2658 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002659 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660
2661 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002662 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002664 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002665 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002666 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002667 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002668 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002670 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002671 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002673 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002674 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002676 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002677 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002679 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002680 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002682 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002683 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002685 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002686 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002688 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002689 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002690 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002691 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002692 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002693 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002695 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002696 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002698 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002699 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 break;
2701 default:
David Sedlákb3077192019-06-19 10:55:37 +02002702 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703 return LY_EVALID;
2704 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002705 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002706 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002707
Radek Krejci7fc68292019-06-12 13:51:09 +02002708checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002709 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002710 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2711 return LY_EVALID;
2712 }
2713
Michal Vasko7fbc8162018-09-17 10:35:16 +02002714 return ret;
2715}
2716
Michal Vaskoea5abea2018-09-18 13:10:54 +02002717/**
2718 * @brief Parse the action statement.
2719 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002720 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002721 * @param[in,out] actions Actions to add to.
2722 *
2723 * @return LY_ERR values.
2724 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002725LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002726parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002728 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002730 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002731 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002732 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733
Radek Krejci2a9fc652021-01-22 17:44:34 +01002734 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735
2736 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002737 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002738 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002739 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002740 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741
Radek Krejci33090f92020-12-17 20:12:46 +01002742 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002743 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002744 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002745 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 +02002746 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002747 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002748 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002750 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002751 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 +02002752 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002753 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002754 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755 break;
2756
Radek Krejcid6b76452019-09-03 17:03:03 +02002757 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002758 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002760 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002761 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002762 break;
2763
Radek Krejcid6b76452019-09-03 17:03:03 +02002764 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002765 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002767 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002768 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002770 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002771 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 +02002772 break;
2773 default:
David Sedlákb3077192019-06-19 10:55:37 +02002774 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 return LY_EVALID;
2776 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002777 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002778 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002779
Radek Krejci2a9fc652021-01-22 17:44:34 +01002780checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002781 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2782 if (!act->input.nodetype) {
2783 act->input.nodetype = LYS_INPUT;
2784 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002785 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002786 }
2787 if (!act->output.nodetype) {
2788 act->output.nodetype = LYS_OUTPUT;
2789 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002790 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002791 }
2792
Michal Vasko7fbc8162018-09-17 10:35:16 +02002793 return ret;
2794}
2795
Michal Vaskoea5abea2018-09-18 13:10:54 +02002796/**
2797 * @brief Parse the notification statement.
2798 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002799 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002800 * @param[in,out] notifs Notifications to add to.
2801 *
2802 * @return LY_ERR values.
2803 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002804LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002805parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002807 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002809 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002810 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002811 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002812
Radek Krejci2a9fc652021-01-22 17:44:34 +01002813 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814
2815 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002816 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002817 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002818 notif->nodetype = LYS_NOTIF;
2819 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820
Radek Krejci2a9fc652021-01-22 17:44:34 +01002821 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002822 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002823 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002824 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 +02002825 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002826 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002827 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002829 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002830 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 +02002831 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002832 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002833 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834 break;
2835
Radek Krejcid6b76452019-09-03 17:03:03 +02002836 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002837 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002838 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002839 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002840 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002841 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002842 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002843 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002845 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002846 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002847 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002848 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002849 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002850 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002851 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002852 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002854 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002855 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002857 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002858 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 break;
2860
Radek Krejcid6b76452019-09-03 17:03:03 +02002861 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002862 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002863 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002865 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002866 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002868 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002869 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002871 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002872 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873 break;
2874 default:
David Sedlákb3077192019-06-19 10:55:37 +02002875 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 return LY_EVALID;
2877 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002878 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002879
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 return ret;
2881}
2882
Michal Vaskoea5abea2018-09-18 13:10:54 +02002883/**
2884 * @brief Parse the grouping statement.
2885 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002886 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002887 * @param[in,out] groupings Groupings to add to.
2888 *
2889 * @return LY_ERR values.
2890 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002891LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002892parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002893{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002894 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002896 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002897 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002898 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002899
Radek Krejci2a9fc652021-01-22 17:44:34 +01002900 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901
2902 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002903 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002904 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002905 grp->nodetype = LYS_GROUPING;
2906 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907
Radek Krejci2a9fc652021-01-22 17:44:34 +01002908 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002910 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002911 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 +02002912 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002913 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002914 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 +02002915 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002916 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002917 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002918 break;
2919
Radek Krejcid6b76452019-09-03 17:03:03 +02002920 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002921 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002922 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002923 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002924 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002926 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002927 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002929 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002930 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002932 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002933 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002934 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002935 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002936 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002938 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002939 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002940 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002941 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002942 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943 break;
2944
Radek Krejcid6b76452019-09-03 17:03:03 +02002945 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002946 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002947 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002948 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002949 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002950 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002951 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002952 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002953 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002954 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002955 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002956 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002957 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002959 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002960 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961 break;
2962 default:
David Sedlákb3077192019-06-19 10:55:37 +02002963 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 return LY_EVALID;
2965 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002966 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002967
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968 return ret;
2969}
2970
Michal Vaskoea5abea2018-09-18 13:10:54 +02002971/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002972 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002973 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002974 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002975 * @param[in,out] augments Augments to add to.
2976 *
2977 * @return LY_ERR values.
2978 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002979LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002980parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002982 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002984 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002985 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002986 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987
Radek Krejci2a9fc652021-01-22 17:44:34 +01002988 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002989
2990 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002991 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002992 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002993 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002994 aug->nodetype = LYS_AUGMENT;
2995 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002996
Radek Krejci2a9fc652021-01-22 17:44:34 +01002997 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002999 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003000 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 +02003001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003002 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003003 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003004 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003005 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003006 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 +02003007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003008 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003009 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003011 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003012 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 break;
3014
Radek Krejcid6b76452019-09-03 17:03:03 +02003015 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003016 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003017 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003018 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003019 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003021 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003022 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003024 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003025 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003027 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003028 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003029 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003030 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003031 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003032 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003033 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003034 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003036 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003037 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003038 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003039 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003040 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003041 break;
3042
Radek Krejcid6b76452019-09-03 17:03:03 +02003043 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003044 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003045 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003046 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003047 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003048 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003049 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003051 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003052 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 break;
3054 default:
David Sedlákb3077192019-06-19 10:55:37 +02003055 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056 return LY_EVALID;
3057 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003059
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060 return ret;
3061}
3062
Michal Vaskoea5abea2018-09-18 13:10:54 +02003063/**
3064 * @brief Parse the uses statement.
3065 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003066 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003067 * @param[in,out] siblings Siblings to add to.
3068 *
3069 * @return LY_ERR values.
3070 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003071LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003072parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003074 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003076 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003077 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 struct lysp_node_uses *uses;
3079
David Sedlák60adc092019-08-06 15:57:02 +02003080 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003081 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003082 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003083 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003086 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003087 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003088
3089 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003090 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003091 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003092 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003093 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 +02003094 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003095 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003096 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003098 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003099 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 +02003100 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003101 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003102 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003103 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003104 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003105 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003106 break;
3107
Radek Krejcid6b76452019-09-03 17:03:03 +02003108 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003109 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003111 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003112 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003114 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003115 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003116 break;
3117 default:
David Sedlákb3077192019-06-19 10:55:37 +02003118 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 return LY_EVALID;
3120 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003122
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123 return ret;
3124}
3125
Michal Vaskoea5abea2018-09-18 13:10:54 +02003126/**
3127 * @brief Parse the case statement.
3128 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003129 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003130 * @param[in,out] siblings Siblings to add to.
3131 *
3132 * @return LY_ERR values.
3133 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003134LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003135parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003136{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003137 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003138 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003139 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003140 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141 struct lysp_node_case *cas;
3142
David Sedlák60adc092019-08-06 15:57:02 +02003143 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003144 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003146 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003149 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003150 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151
3152 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003153 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003155 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003156 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 +02003157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003158 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003159 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003161 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003162 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 +02003163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003164 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003165 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003167 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003168 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003169 break;
3170
Radek Krejcid6b76452019-09-03 17:03:03 +02003171 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003172 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003173 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003174 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003175 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003177 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003178 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003180 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003181 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003183 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003184 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003186 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003187 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003189 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003190 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003192 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003193 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003195 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003196 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003197 break;
3198 default:
David Sedlákb3077192019-06-19 10:55:37 +02003199 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003200 return LY_EVALID;
3201 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203 return ret;
3204}
3205
Michal Vaskoea5abea2018-09-18 13:10:54 +02003206/**
3207 * @brief Parse the choice statement.
3208 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003209 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003210 * @param[in,out] siblings Siblings to add to.
3211 *
3212 * @return LY_ERR values.
3213 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003214LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003215parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003217 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003218 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003219 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003220 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003221 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222
David Sedlák60adc092019-08-06 15:57:02 +02003223 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003224 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003225 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003226 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003229 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003230 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231
3232 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003233 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003234 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003235 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003236 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003238 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003239 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 +02003240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003241 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003242 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003244 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003245 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003247 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003248 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 +02003249 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003250 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003251 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003253 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003254 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003256 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01003257 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 +02003258 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003259 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 break;
3261
Radek Krejcid6b76452019-09-03 17:03:03 +02003262 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003263 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003264 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003265 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003266 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003268 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003269 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003271 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003272 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003273 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003275 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003276 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003278 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003279 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003281 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003282 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003284 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003285 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003287 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003288 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003289 break;
3290 default:
David Sedlákb3077192019-06-19 10:55:37 +02003291 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292 return LY_EVALID;
3293 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295 return ret;
3296}
3297
Michal Vaskoea5abea2018-09-18 13:10:54 +02003298/**
3299 * @brief Parse the container statement.
3300 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003301 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003302 * @param[in,out] siblings Siblings to add to.
3303 *
3304 * @return LY_ERR values.
3305 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003306LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003307parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308{
3309 LY_ERR ret = 0;
3310 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003311 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003312 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313 struct lysp_node_container *cont;
3314
David Sedlák60adc092019-08-06 15:57:02 +02003315 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003316 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003318 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003321 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003322 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323
3324 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003325 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003328 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003331 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 +02003332 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003333 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003334 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003336 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003337 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 +02003338 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003339 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003340 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003342 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003343 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003345 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003346 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 +02003347 break;
3348
Radek Krejcid6b76452019-09-03 17:03:03 +02003349 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003350 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003351 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003352 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003353 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003354 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003355 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003356 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003358 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003359 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003360 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003361 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003362 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003364 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003365 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003367 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003368 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003370 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003371 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 break;
3373
Radek Krejcid6b76452019-09-03 17:03:03 +02003374 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003375 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003377 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003378 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003380 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003381 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003382 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003384 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003385 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003387 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003388 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003389 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003391 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003392 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393 break;
3394 default:
David Sedlákb3077192019-06-19 10:55:37 +02003395 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396 return LY_EVALID;
3397 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003399
Michal Vasko7fbc8162018-09-17 10:35:16 +02003400 return ret;
3401}
3402
Michal Vaskoea5abea2018-09-18 13:10:54 +02003403/**
3404 * @brief Parse the list statement.
3405 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003406 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003407 * @param[in,out] siblings Siblings to add to.
3408 *
3409 * @return LY_ERR values.
3410 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003411LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003412parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003414 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003416 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003417 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 struct lysp_node_list *list;
3419
David Sedlák60adc092019-08-06 15:57:02 +02003420 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003421 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003422 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003423 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424
Michal Vasko7fbc8162018-09-17 10:35:16 +02003425 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003426 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003427 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003428
3429 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003430 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003431 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003432 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003433 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003435 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003436 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 +02003437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003438 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003439 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003441 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003442 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 +02003443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003444 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003445 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003448 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_KEY:
Radek Krejcifc596f92021-02-26 22:40:26 +01003451 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 +02003452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003454 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003456 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003457 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003459 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003460 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003462 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003463 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 break;
3465
Radek Krejcid6b76452019-09-03 17:03:03 +02003466 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003467 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003468 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003469 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003470 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003472 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003473 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003475 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003476 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003477 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003478 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003479 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003481 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003482 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003484 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003485 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003487 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003488 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489 break;
3490
Radek Krejcid6b76452019-09-03 17:03:03 +02003491 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003492 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003494 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003495 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003497 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003498 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003499 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003501 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003502 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003504 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003505 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003506 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003508 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003509 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003510 break;
3511 default:
David Sedlákb3077192019-06-19 10:55:37 +02003512 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513 return LY_EVALID;
3514 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003515 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003516
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 return ret;
3518}
3519
Michal Vaskoea5abea2018-09-18 13:10:54 +02003520/**
3521 * @brief Parse the yin-element statement.
3522 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003523 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003524 * @param[in,out] flags Flags to write to.
3525 * @param[in,out] exts Extension instances to add to.
3526 *
3527 * @return LY_ERR values.
3528 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003530parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003532 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003534 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003535 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003536
3537 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003538 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539 return LY_EVALID;
3540 }
3541
3542 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003543 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544
Radek Krejcif13b87b2020-12-01 22:02:17 +01003545 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003547 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548 *flags |= LYS_YINELEM_FALSE;
3549 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003550 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 free(buf);
3552 return LY_EVALID;
3553 }
3554 free(buf);
3555
Radek Krejci33090f92020-12-17 20:12:46 +01003556 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003558 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003559 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003560 LY_CHECK_RET(ret);
3561 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003562 default:
David Sedlákb3077192019-06-19 10:55:37 +02003563 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 return LY_EVALID;
3565 }
3566 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 return ret;
3568}
3569
Michal Vaskoea5abea2018-09-18 13:10:54 +02003570/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003571 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003572 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003573 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003574 * @param[in,out] argument Value to write to.
3575 * @param[in,out] flags Flags to write to.
3576 * @param[in,out] exts Extension instances to add to.
3577 *
3578 * @return LY_ERR values.
3579 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003581parse_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 +02003582{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003583 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003585 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003586 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587
3588 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003589 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 return LY_EVALID;
3591 }
3592
3593 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003594 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003595 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596
Radek Krejci33090f92020-12-17 20:12:46 +01003597 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003599 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003600 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003602 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003603 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604 break;
3605 default:
David Sedlákb3077192019-06-19 10:55:37 +02003606 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003607 return LY_EVALID;
3608 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003610 return ret;
3611}
3612
Michal Vaskoea5abea2018-09-18 13:10:54 +02003613/**
3614 * @brief Parse the extension statement.
3615 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003616 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003617 * @param[in,out] extensions Extensions to add to.
3618 *
3619 * @return LY_ERR values.
3620 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003622parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003624 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003626 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003627 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 struct lysp_ext *ex;
3629
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003630 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631
3632 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003633 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003634 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635
Radek Krejci33090f92020-12-17 20:12:46 +01003636 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003638 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003639 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 +02003640 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003641 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003642 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 +02003643 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003644 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003645 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003647 case LY_STMT_ARGUMENT:
Radek Krejci9f87b0c2021-03-05 14:45:26 +01003648 LY_CHECK_RET(parse_argument(ctx, &ex->argname, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003650 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003651 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652 break;
3653 default:
David Sedlákb3077192019-06-19 10:55:37 +02003654 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003655 return LY_EVALID;
3656 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658 return ret;
3659}
3660
Michal Vaskoea5abea2018-09-18 13:10:54 +02003661/**
3662 * @brief Parse the deviate statement.
3663 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003664 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003665 * @param[in,out] deviates Deviates to add to.
3666 *
3667 * @return LY_ERR values.
3668 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003669LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003670parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003672 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003674 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003675 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003676 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003677 struct lysp_deviate_add *d_add = NULL;
3678 struct lysp_deviate_rpl *d_rpl = NULL;
3679 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003680 const char **d_units = NULL;
3681 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003682 struct lysp_restr **d_musts = NULL;
3683 uint16_t *d_flags = 0;
3684 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685
3686 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003687 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688
Radek Krejcif13b87b2020-12-01 22:02:17 +01003689 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003691 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003693 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003695 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003696 dev_mod = LYS_DEV_DELETE;
3697 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003698 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699 free(buf);
3700 return LY_EVALID;
3701 }
3702 free(buf);
3703
3704 /* create structure */
3705 switch (dev_mod) {
3706 case LYS_DEV_NOT_SUPPORTED:
3707 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003708 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709 break;
3710 case LYS_DEV_ADD:
3711 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003712 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003713 d = (struct lysp_deviate *)d_add;
3714 d_units = &d_add->units;
3715 d_uniques = &d_add->uniques;
3716 d_dflts = &d_add->dflts;
3717 d_musts = &d_add->musts;
3718 d_flags = &d_add->flags;
3719 d_min = &d_add->min;
3720 d_max = &d_add->max;
3721 break;
3722 case LYS_DEV_REPLACE:
3723 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003724 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003725 d = (struct lysp_deviate *)d_rpl;
3726 d_units = &d_rpl->units;
3727 d_flags = &d_rpl->flags;
3728 d_min = &d_rpl->min;
3729 d_max = &d_rpl->max;
3730 break;
3731 case LYS_DEV_DELETE:
3732 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003733 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 d = (struct lysp_deviate *)d_del;
3735 d_units = &d_del->units;
3736 d_uniques = &d_del->uniques;
3737 d_dflts = &d_del->dflts;
3738 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739 break;
3740 default:
3741 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003742 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743 }
3744 d->mod = dev_mod;
3745
3746 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003747 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748
Radek Krejci33090f92020-12-17 20:12:46 +01003749 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003751 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003752 switch (dev_mod) {
3753 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003754 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003755 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003756 return LY_EVALID;
3757 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003758 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759 break;
3760 }
3761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003762 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003763 switch (dev_mod) {
3764 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003765 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766 return LY_EVALID;
3767 case LYS_DEV_REPLACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003768 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 +02003769 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003770 break;
3771 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003772 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003773 break;
3774 }
3775 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003776 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003777 switch (dev_mod) {
3778 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003779 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003780 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 return LY_EVALID;
3782 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003783 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003784 break;
3785 }
3786 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003787 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 switch (dev_mod) {
3789 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003790 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003791 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003792 return LY_EVALID;
3793 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003794 LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 break;
3796 }
3797 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003798 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799 switch (dev_mod) {
3800 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003801 case LYS_DEV_DELETE:
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 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003805 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003806 break;
3807 }
3808 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003809 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003810 switch (dev_mod) {
3811 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003812 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003813 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 return LY_EVALID;
3815 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003816 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003817 break;
3818 }
3819 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003820 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003821 switch (dev_mod) {
3822 case LYS_DEV_NOT_SUPPORTED:
3823 case LYS_DEV_ADD:
3824 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003825 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 return LY_EVALID;
3827 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003828 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003829 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003830 return LY_EVALID;
3831 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003833 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003834 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835 break;
3836 }
3837 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003838 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003839 switch (dev_mod) {
3840 case LYS_DEV_NOT_SUPPORTED:
3841 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003842 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003843 return LY_EVALID;
3844 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003845 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846 break;
3847 }
3848 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003849 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850 switch (dev_mod) {
3851 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003852 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003853 return LY_EVALID;
3854 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003855 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 +02003856 break;
3857 }
3858 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003859 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003860 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATE, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 break;
3862 default:
David Sedlákb3077192019-06-19 10:55:37 +02003863 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 return LY_EVALID;
3865 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003867 return ret;
3868}
3869
Michal Vaskoea5abea2018-09-18 13:10:54 +02003870/**
3871 * @brief Parse the deviation statement.
3872 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003873 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003874 * @param[in,out] deviations Deviations to add to.
3875 *
3876 * @return LY_ERR values.
3877 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003878LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003879parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003881 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003882 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003883 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003884 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885 struct lysp_deviation *dev;
3886
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003887 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888
3889 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003890 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003891 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003892 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003893
Radek Krejci33090f92020-12-17 20:12:46 +01003894 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003896 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003897 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 +02003898 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003899 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003900 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003902 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003903 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 +02003904 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003905 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003906 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATION, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907 break;
3908 default:
David Sedlákb3077192019-06-19 10:55:37 +02003909 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910 return LY_EVALID;
3911 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003913 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003914checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003915 /* mandatory substatements */
3916 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003917 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003918 return LY_EVALID;
3919 }
3920
3921 return ret;
3922}
3923
Michal Vaskoea5abea2018-09-18 13:10:54 +02003924/**
3925 * @brief Parse the feature statement.
3926 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003927 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003928 * @param[in,out] features Features to add to.
3929 *
3930 * @return LY_ERR values.
3931 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003932LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003933parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003935 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003937 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003938 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939 struct lysp_feature *feat;
3940
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003941 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942
3943 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003944 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003945 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003946
Radek Krejci33090f92020-12-17 20:12:46 +01003947 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003948 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003949 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003950 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 +02003951 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003952 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003953 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003955 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003956 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 +02003957 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003958 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003959 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003961 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003962 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003963 break;
3964 default:
David Sedlákb3077192019-06-19 10:55:37 +02003965 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003966 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003968 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969 return ret;
3970}
3971
Michal Vaskoea5abea2018-09-18 13:10:54 +02003972/**
3973 * @brief Parse the identity statement.
3974 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003975 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003976 * @param[in,out] identities Identities to add to.
3977 *
3978 * @return LY_ERR values.
3979 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003980LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003981parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003982{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003983 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003985 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003986 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 struct lysp_ident *ident;
3988
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003989 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990
3991 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003992 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003993 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003994
Radek Krejci33090f92020-12-17 20:12:46 +01003995 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003997 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003998 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003999 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004000 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004001 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004002 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004004 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004005 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 +02004006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004007 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004008 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004010 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004011 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004012 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 +01004013 return LY_EVALID;
4014 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004015 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 +02004016 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004017 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004018 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019 break;
4020 default:
David Sedlákb3077192019-06-19 10:55:37 +02004021 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004022 return LY_EVALID;
4023 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004025 return ret;
4026}
4027
Michal Vaskoea5abea2018-09-18 13:10:54 +02004028/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004029 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004030 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004031 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004032 * @param[in,out] mod Module to write to.
4033 *
4034 * @return LY_ERR values.
4035 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004036LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004037parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004038{
4039 LY_ERR ret = 0;
4040 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004041 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004042 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004044 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004045
Michal Vaskoc3781c32020-10-06 14:04:08 +02004046 mod->is_submod = 0;
4047
4048 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004049 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004050 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004051
Radek Krejci33090f92020-12-17 20:12:46 +01004052 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053
Radek Krejcie3846472018-10-15 15:24:51 +02004054#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004055 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 +02004056
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 switch (kw) {
4058 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004059 case LY_STMT_NAMESPACE:
4060 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004061 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004063 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004064 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004065 break;
4066 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004067 case LY_STMT_INCLUDE:
4068 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004069 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004070 break;
4071 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004072 case LY_STMT_ORGANIZATION:
4073 case LY_STMT_CONTACT:
4074 case LY_STMT_DESCRIPTION:
4075 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004076 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004077 break;
4078
4079 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004080 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004081 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004082 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004083 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004084 case LY_STMT_ANYDATA:
4085 case LY_STMT_ANYXML:
4086 case LY_STMT_AUGMENT:
4087 case LY_STMT_CHOICE:
4088 case LY_STMT_CONTAINER:
4089 case LY_STMT_DEVIATION:
4090 case LY_STMT_EXTENSION:
4091 case LY_STMT_FEATURE:
4092 case LY_STMT_GROUPING:
4093 case LY_STMT_IDENTITY:
4094 case LY_STMT_LEAF:
4095 case LY_STMT_LEAF_LIST:
4096 case LY_STMT_LIST:
4097 case LY_STMT_NOTIFICATION:
4098 case LY_STMT_RPC:
4099 case LY_STMT_TYPEDEF:
4100 case LY_STMT_USES:
4101 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102 mod_stmt = Y_MOD_BODY;
4103 break;
4104 default:
4105 /* error handled in the next switch */
4106 break;
4107 }
Radek Krejcie3846472018-10-15 15:24:51 +02004108#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004109
Radek Krejcie3846472018-10-15 15:24:51 +02004110 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111 switch (kw) {
4112 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004113 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004114 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004116 case LY_STMT_NAMESPACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004117 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 +02004118 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004119 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01004120 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 +02004121 break;
4122
4123 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004124 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004125 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004127 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004128 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004129 break;
4130
4131 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004132 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004133 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 +02004134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004135 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004136 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 +02004137 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004138 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004139 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 +02004140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004141 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004142 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 +02004143 break;
4144
4145 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004146 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004147 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004148 break;
4149
4150 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004151 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004152 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004153 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004154 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004155 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004157 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004158 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004160 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004161 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004163 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004164 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004167 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004169 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004170 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004172 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004173 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004174 break;
4175
Radek Krejcid6b76452019-09-03 17:03:03 +02004176 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004177 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004179 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004180 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004182 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004183 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004186 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004189 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004191 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004192 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004194 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004195 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004197 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004198 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004200 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004201 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004203 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004204 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 break;
4206
4207 default:
David Sedlákb3077192019-06-19 10:55:37 +02004208 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004209 return LY_EVALID;
4210 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004211 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004212 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004213
Radek Krejci6d6556c2018-11-08 09:37:45 +01004214checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004215 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004216 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004217 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004218 return LY_EVALID;
4219 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004220 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004221 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004222 }
4223
Radek Krejcie9e987e2018-10-31 12:50:27 +01004224 /* submodules share the namespace with the module names, so there must not be
4225 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004226 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004227 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004228 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004229 return LY_EVALID;
4230 }
4231
4232 return ret;
4233}
4234
4235/**
4236 * @brief Parse submodule substatements.
4237 *
4238 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004239 * @param[out] submod Parsed submodule structure.
4240 *
4241 * @return LY_ERR values.
4242 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004243LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004244parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004245{
4246 LY_ERR ret = 0;
4247 char *buf, *word;
4248 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004249 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004250 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004251 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004252
Michal Vaskoc3781c32020-10-06 14:04:08 +02004253 submod->is_submod = 1;
4254
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004255 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004256 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004257 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004258
Radek Krejci33090f92020-12-17 20:12:46 +01004259 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004260
4261#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004262 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 +01004263
4264 switch (kw) {
4265 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004266 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004267 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004269 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004270 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4271 break;
4272 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004273 case LY_STMT_INCLUDE:
4274 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004275 CHECK_ORDER(Y_MOD_LINKAGE);
4276 break;
4277 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004278 case LY_STMT_ORGANIZATION:
4279 case LY_STMT_CONTACT:
4280 case LY_STMT_DESCRIPTION:
4281 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004282 CHECK_ORDER(Y_MOD_META);
4283 break;
4284
4285 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004286 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004287 CHECK_ORDER(Y_MOD_REVISION);
4288 break;
4289 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004290 case LY_STMT_ANYDATA:
4291 case LY_STMT_ANYXML:
4292 case LY_STMT_AUGMENT:
4293 case LY_STMT_CHOICE:
4294 case LY_STMT_CONTAINER:
4295 case LY_STMT_DEVIATION:
4296 case LY_STMT_EXTENSION:
4297 case LY_STMT_FEATURE:
4298 case LY_STMT_GROUPING:
4299 case LY_STMT_IDENTITY:
4300 case LY_STMT_LEAF:
4301 case LY_STMT_LEAF_LIST:
4302 case LY_STMT_LIST:
4303 case LY_STMT_NOTIFICATION:
4304 case LY_STMT_RPC:
4305 case LY_STMT_TYPEDEF:
4306 case LY_STMT_USES:
4307 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004308 mod_stmt = Y_MOD_BODY;
4309 break;
4310 default:
4311 /* error handled in the next switch */
4312 break;
4313 }
4314#undef CHECK_ORDER
4315
4316 prev_kw = kw;
4317 switch (kw) {
4318 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004319 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004320 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004322 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004323 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004324 break;
4325
4326 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004327 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004328 if (submod->version == LYS_VERSION_1_1) {
4329 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4330 submod->name);
4331 }
Radek Krejci33090f92020-12-17 20:12:46 +01004332 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004334 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004335 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004336 break;
4337
4338 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004339 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004340 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 +01004341 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004342 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004343 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 +01004344 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004345 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004346 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 +01004347 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004348 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004349 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 +01004350 break;
4351
4352 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004353 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004354 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004355 break;
4356
4357 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004358 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004359 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004360 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004361 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004362 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004364 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004365 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004367 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004368 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004370 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004371 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004373 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004374 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004376 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004377 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004379 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004380 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004381 break;
4382
Radek Krejcid6b76452019-09-03 17:03:03 +02004383 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004384 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004386 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004387 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004389 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004390 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004392 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004393 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004395 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004396 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004397 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004398 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004399 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004401 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004402 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004404 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004405 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004407 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004408 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004410 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004411 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004412 break;
4413
4414 default:
David Sedlákb3077192019-06-19 10:55:37 +02004415 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004416 return LY_EVALID;
4417 }
4418 }
4419 LY_CHECK_RET(ret);
4420
4421checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004422 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004423 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004424 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004425 return LY_EVALID;
4426 }
4427
4428 /* submodules share the namespace with the module names, so there must not be
4429 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004430 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004431 /* main modules may have different revisions */
4432 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004433 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004434 return LY_EVALID;
4435 }
4436
Michal Vasko7fbc8162018-09-17 10:35:16 +02004437 return ret;
4438}
4439
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004440/**
4441 * @brief Skip any redundant characters, namely whitespaces and comments.
4442 *
4443 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004444 * @return LY_SUCCESS on success.
4445 * @return LY_EVALID on invalid comment.
4446 */
4447static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004448skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004449{
4450 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004451 while (ctx->in->current[0]) {
4452 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004453 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004454 ly_in_skip(ctx->in, 2);
4455 LY_CHECK_RET(skip_comment(ctx, 1));
4456 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004457 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004458 ly_in_skip(ctx->in, 2);
4459 LY_CHECK_RET(skip_comment(ctx, 2));
4460 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004461 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004462 if (ctx->in->current[0] == '\n') {
4463 LY_IN_NEW_LINE(ctx->in);
4464 }
Radek Krejci33090f92020-12-17 20:12:46 +01004465 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004466 } else {
4467 break;
4468 }
4469 }
4470
4471 return LY_SUCCESS;
4472}
4473
Radek Krejcid4557c62018-09-17 11:42:09 +02004474LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004475yang_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 +02004476 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004477{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004478 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004479 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004480 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004481 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004482 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004483
David Sedlák1b623122019-08-05 15:27:49 +02004484 /* create context */
4485 *context = calloc(1, sizeof **context);
4486 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004487 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004488 (*context)->unres = main_ctx->unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004489 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004490
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004491 mod_p = calloc(1, sizeof *mod_p);
4492 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4493 mod_p->mod = main_ctx->parsed_mod->mod;
4494 mod_p->parsing = 1;
4495 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4496
Radek Krejciddace2c2021-01-08 11:30:56 +01004497 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004498
David Sedlák1b623122019-08-05 15:27:49 +02004499 /* map the typedefs and groupings list from main context to the submodule's context */
4500 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4501 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4502
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004503 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004504 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004505 LY_CHECK_GOTO(ret, cleanup);
4506
Michal Vasko7fbc8162018-09-17 10:35:16 +02004507 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004508 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004509 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004510
Radek Krejcid6b76452019-09-03 17:03:03 +02004511 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004512 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004513 ret = LY_EINVAL;
4514 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004515 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004516 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004517 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004518 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004519 }
4520
Michal Vasko7fbc8162018-09-17 10:35:16 +02004521 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004522 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004523 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004524
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004525 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004526 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004527 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004528 if (in->current[0]) {
4529 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004530 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004531 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004532 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004533
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004534 mod_p->parsing = 0;
4535 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004536
Radek Krejcibbe09a92018-11-08 09:36:54 +01004537cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004538 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004539 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004540 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004541 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004542 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004543 }
4544
4545 return ret;
4546}
4547
4548LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004549yang_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 +01004550{
4551 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004552 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004553 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004554 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004555 struct lysp_module *mod_p = NULL;
4556
David Sedlák1b623122019-08-05 15:27:49 +02004557 /* create context */
4558 *context = calloc(1, sizeof **context);
4559 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004560 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004561 (*context)->unres = unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004562 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004563
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004564 mod_p = calloc(1, sizeof *mod_p);
4565 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4566 mod_p->mod = mod;
4567 mod_p->parsing = 1;
4568 (*context)->parsed_mod = mod_p;
4569
Radek Krejciddace2c2021-01-08 11:30:56 +01004570 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004571
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004572 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004573 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004574 LY_CHECK_GOTO(ret, cleanup);
4575
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004576 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004577 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004578 LY_CHECK_GOTO(ret, cleanup);
4579
Radek Krejcid6b76452019-09-03 17:03:03 +02004580 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004581 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 +01004582 ret = LY_EINVAL;
4583 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004584 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004585 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004586 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004587 goto cleanup;
4588 }
4589
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004590 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004591 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004592 LY_CHECK_GOTO(ret, cleanup);
4593
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004594 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004595 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004596 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004597 if (in->current[0]) {
4598 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004599 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004600 goto cleanup;
4601 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004602
4603 mod_p->parsing = 0;
4604 mod->parsed = mod_p;
4605
4606cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004607 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004608 if (ret) {
4609 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004610 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004611 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004612 }
4613
Michal Vasko7fbc8162018-09-17 10:35:16 +02004614 return ret;
4615}