blob: 6e38e2cc368a867d1189441a7fac1e21d631356c [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;
692 default:
693 /* either a keyword start or an invalid character */
694 goto keyword_start;
695 }
696
Radek Krejci33090f92020-12-17 20:12:46 +0100697 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200698 }
699
700keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100701 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100702 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200703
Michal Vasko69730152020-10-09 16:30:07 +0200704 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 +0200705 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200706 }
707
Radek Krejcid6b76452019-09-03 17:03:03 +0200708 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200709 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100710 switch (ctx->in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200711 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200712 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200713 case ' ':
714 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200716 case ':':
717 /* keyword is not actually a keyword, but prefix of an extension.
718 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
719 * and we will be checking the keyword (extension instance) itself */
720 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100721 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200722 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200723 case '{':
724 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200725 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200726 break;
727 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100728 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200729 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100730 MOVE_INPUT(ctx, 1);
731 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200732 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200733 return LY_EVALID;
734 }
735 } else {
736 /* still can be an extension */
737 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200738extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100739 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
740 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '{') && (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200741 uint32_t c = 0;
742
Radek Krejci33090f92020-12-17 20:12:46 +0100743 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
744 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200745 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200746 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200747 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100748 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200749 }
Radek Krejci33090f92020-12-17 20:12:46 +0100750 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200751 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 return LY_EVALID;
753 }
754
755 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200756 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100757 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200758 return LY_EVALID;
759 }
760
Radek Krejcid6b76452019-09-03 17:03:03 +0200761 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200762 }
Radek Krejci626df482018-10-11 15:06:31 +0200763success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200764 if (word_p) {
765 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100766 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200767 }
768
769 return LY_SUCCESS;
770}
771
Michal Vaskoea5abea2018-09-18 13:10:54 +0200772/**
773 * @brief Parse extension instance substatements.
774 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200775 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200776 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200777 * @param[in] word Extension instance substatement name (keyword).
778 * @param[in] word_len Extension instance substatement name length.
779 * @param[in,out] child Children of this extension instance to add to.
780 *
781 * @return LY_ERR values.
782 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200783static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100784parse_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 +0200785 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200786{
787 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100788 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200789 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200790 struct lysp_stmt *stmt, *par_child;
791
792 stmt = calloc(1, sizeof *stmt);
793 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
794
Radek Krejcibb9b1982019-04-08 14:24:59 +0200795 /* insert into parent statements */
796 if (!*child) {
797 *child = stmt;
798 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200799 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200800 par_child->next = stmt;
801 }
802
Michal Vaskofc2cd072021-02-24 13:17:17 +0100803 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200804 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200805
806 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100807 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200808 if (word) {
809 if (buf) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200810 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200811 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200812 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200813 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200814 }
815
Michal Vaskofc2cd072021-02-24 13:17:17 +0100816 stmt->format = LY_PREF_SCHEMA;
817 stmt->prefix_data = ctx->parsed_mod;
818 stmt->kw = kw;
819
Radek Krejci33090f92020-12-17 20:12:46 +0100820 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, ) {
821 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200822 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200823 return ret;
824}
825
Michal Vaskoea5abea2018-09-18 13:10:54 +0200826/**
827 * @brief Parse extension instance.
828 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200829 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200830 * @param[in] ext_name Extension instance substatement name (keyword).
831 * @param[in] ext_name_len Extension instance substatement name length.
Radek Krejcifc596f92021-02-26 22:40:26 +0100832 * @param[in] insubstmt The statement this extension instance is a substatement of.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200833 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
834 * @param[in,out] exts Extension instances to add to.
835 *
836 * @return LY_ERR values.
837 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200838static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100839parse_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 +0200840 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100842 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200844 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200846 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200847
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200848 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200849
Michal Vaskofc2cd072021-02-24 13:17:17 +0100850 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
851 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
852 return LY_EVALID;
853 }
854
855 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200856 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200857
858 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100859 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200860 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200861 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200862 }
863
Michal Vaskofc2cd072021-02-24 13:17:17 +0100864 /* store the rest of information */
865 e->format = LY_PREF_SCHEMA;
866 e->prefix_data = ctx->parsed_mod;
Radek Krejciab430862021-03-02 20:13:40 +0100867 e->parent_stmt = insubstmt;
868 e->parent_stmt_index = insubstmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100869
Radek Krejci33090f92020-12-17 20:12:46 +0100870 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
871 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200872 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200873 return ret;
874}
875
Michal Vaskoea5abea2018-09-18 13:10:54 +0200876/**
877 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
878 * description, etc...
879 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200880 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200881 * @param[in] substmt Type of this substatement.
882 * @param[in] substmt_index Index of this substatement.
883 * @param[in,out] value Place to store the parsed value.
884 * @param[in] arg Type of the YANG keyword argument (of the value).
885 * @param[in,out] exts Extension instances to add to.
886 *
887 * @return LY_ERR values.
888 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200889static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100890parse_text_field(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200891 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100893 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200895 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200896 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200897
898 if (*value) {
Radek Krejci3972b332021-03-02 16:34:31 +0100899 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200900 return LY_EVALID;
901 }
902
903 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100904 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905
906 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200907 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200908
Radek Krejci33090f92020-12-17 20:12:46 +0100909 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200911 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100912 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200913 break;
914 default:
Radek Krejci3972b332021-03-02 16:34:31 +0100915 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916 return LY_EVALID;
917 }
918 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200919 return ret;
920}
921
Michal Vaskoea5abea2018-09-18 13:10:54 +0200922/**
923 * @brief Parse the yang-version statement.
924 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200925 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100926 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200927 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200928 *
929 * @return LY_ERR values.
930 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100932parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100934 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200935 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200936 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200937 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200938
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100939 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200940 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200941 return LY_EVALID;
942 }
943
944 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100945 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200946
Radek Krejci96e48da2020-09-04 13:18:06 +0200947 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100948 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100949 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100950 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200952 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200953 free(buf);
954 return LY_EVALID;
955 }
956 free(buf);
957
Radek Krejci33090f92020-12-17 20:12:46 +0100958 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200959 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200960 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +0100961 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200962 break;
963 default:
David Sedlákb3077192019-06-19 10:55:37 +0200964 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200965 return LY_EVALID;
966 }
967 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968 return ret;
969}
970
Michal Vaskoea5abea2018-09-18 13:10:54 +0200971/**
972 * @brief Parse the belongs-to statement.
973 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200974 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200975 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
976 * @param[in,out] exts Extension instances to add to.
977 *
978 * @return LY_ERR values.
979 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200980static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100981parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200982{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100983 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200985 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200986 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987
Michal Vaskoc3781c32020-10-06 14:04:08 +0200988 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200989 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 return LY_EVALID;
991 }
992
Michal Vaskoc3781c32020-10-06 14:04:08 +0200993 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +0100994 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200995 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +0200996 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 +0200997 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +0200998 free(buf);
999 return LY_EVALID;
1000 }
1001 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001002
Radek Krejci33090f92020-12-17 20:12:46 +01001003 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001004 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001005 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001006 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001008 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001009 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_BELONGS_TO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001010 break;
1011 default:
David Sedlákb3077192019-06-19 10:55:37 +02001012 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001013 return LY_EVALID;
1014 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001015 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001016 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001017checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001018 /* mandatory substatements */
1019 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001020 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001021 return LY_EVALID;
1022 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023 return ret;
1024}
1025
Michal Vaskoea5abea2018-09-18 13:10:54 +02001026/**
1027 * @brief Parse the revision-date statement.
1028 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001029 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001030 * @param[in,out] rev Array to store the parsed value in.
1031 * @param[in,out] exts Extension instances to add to.
1032 *
1033 * @return LY_ERR values.
1034 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001035static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001036parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001038 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001040 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001041 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001042
1043 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001044 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001045 return LY_EVALID;
1046 }
1047
1048 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001049 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001050
1051 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001052 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053 free(buf);
1054 return LY_EVALID;
1055 }
1056
1057 /* store value and spend buf if allocated */
1058 strncpy(rev, word, word_len);
1059 free(buf);
1060
Radek Krejci33090f92020-12-17 20:12:46 +01001061 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001062 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001063 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001064 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001065 break;
1066 default:
David Sedlákb3077192019-06-19 10:55:37 +02001067 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001068 return LY_EVALID;
1069 }
1070 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071 return ret;
1072}
1073
Michal Vaskoea5abea2018-09-18 13:10:54 +02001074/**
1075 * @brief Parse the include statement.
1076 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001077 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001078 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001079 * @param[in,out] includes Parsed includes to add to.
1080 *
1081 * @return LY_ERR values.
1082 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001084parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001085{
Radek Krejcid33273d2018-10-25 14:55:52 +02001086 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001087 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001088 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001089 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001090 struct lysp_include *inc;
1091
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001092 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001093
1094 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001095 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001096
Radek Krejci011e4aa2020-09-04 15:22:31 +02001097 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001098
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001099 /* submodules share the namespace with the module names, so there must not be
1100 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001101 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001102 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001103 return LY_EVALID;
1104 }
1105
Radek Krejci33090f92020-12-17 20:12:46 +01001106 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001108 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001109 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001110 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 +02001111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001112 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001113 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001114 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 +02001115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001116 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001117 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001119 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001120 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001121 break;
1122 default:
David Sedlákb3077192019-06-19 10:55:37 +02001123 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124 return LY_EVALID;
1125 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127 return ret;
1128}
1129
Michal Vaskoea5abea2018-09-18 13:10:54 +02001130/**
1131 * @brief Parse the import statement.
1132 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001133 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001134 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001135 * @param[in,out] imports Parsed imports to add to.
1136 *
1137 * @return LY_ERR values.
1138 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001140parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001142 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001144 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001145 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001146 struct lysp_import *imp;
1147
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001148 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149
1150 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001151 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001152 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153
Radek Krejci33090f92020-12-17 20:12:46 +01001154 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001156 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001157 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 +01001158 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 +02001159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001160 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001161 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001162 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 +02001163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001164 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001165 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001166 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 +02001167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001168 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001169 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001171 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001172 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001173 break;
1174 default:
David Sedlákb3077192019-06-19 10:55:37 +02001175 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001176 return LY_EVALID;
1177 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001179 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001180checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001182 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183
1184 return ret;
1185}
1186
Michal Vaskoea5abea2018-09-18 13:10:54 +02001187/**
1188 * @brief Parse the revision statement.
1189 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001190 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001191 * @param[in,out] revs Parsed revisions to add to.
1192 *
1193 * @return LY_ERR values.
1194 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001196parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001198 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001199 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001200 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001201 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001202 struct lysp_revision *rev;
1203
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001204 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001205
1206 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001207 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208
1209 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001210 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001211 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212 return LY_EVALID;
1213 }
1214
Radek Krejcib7db73a2018-10-24 14:18:40 +02001215 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001216 free(buf);
1217
Radek Krejci33090f92020-12-17 20:12:46 +01001218 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001220 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001221 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 +02001222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001223 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001224 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 +02001225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001226 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001227 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001228 break;
1229 default:
David Sedlákb3077192019-06-19 10:55:37 +02001230 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231 return LY_EVALID;
1232 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234 return ret;
1235}
1236
Michal Vaskoea5abea2018-09-18 13:10:54 +02001237/**
1238 * @brief Parse a generic text field that can have more instances such as base.
1239 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001240 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001241 * @param[in] substmt Type of this substatement.
1242 * @param[in,out] texts Parsed values to add to.
1243 * @param[in] arg Type of the expected argument.
1244 * @param[in,out] exts Extension instances to add to.
1245 *
1246 * @return LY_ERR values.
1247 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001249parse_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 +02001250 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001252 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001253 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001254 const char **item;
1255 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001256 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257
1258 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001259 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001260
1261 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001262 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263
Radek Krejci011e4aa2020-09-04 15:22:31 +02001264 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Radek Krejci33090f92020-12-17 20:12:46 +01001265 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001267 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001268 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001269 break;
1270 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001271 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001272 return LY_EVALID;
1273 }
1274 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001275 return ret;
1276}
1277
Michal Vaskoea5abea2018-09-18 13:10:54 +02001278/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001279 * @brief Parse a generic text field that can have more instances such as base.
1280 *
1281 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001282 * @param[in] substmt Type of this substatement.
1283 * @param[in,out] qnames Parsed qnames to add to.
1284 * @param[in] arg Type of the expected argument.
1285 * @param[in,out] exts Extension instances to add to.
1286 *
1287 * @return LY_ERR values.
1288 */
1289static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001290parse_qnames(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001291 enum yang_arg arg, struct lysp_ext_instance **exts)
1292{
1293 LY_ERR ret = LY_SUCCESS;
1294 char *buf, *word;
1295 struct lysp_qname *item;
1296 size_t word_len;
1297 enum ly_stmt kw;
1298
1299 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001300 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001301
1302 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001303 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001304
1305 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001306 item->mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001307 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001308 switch (kw) {
1309 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001310 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001311 break;
1312 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001313 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001314 return LY_EVALID;
1315 }
1316 }
1317 return ret;
1318}
1319
1320/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001321 * @brief Parse the config statement.
1322 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001323 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001324 * @param[in,out] flags Flags to add to.
1325 * @param[in,out] exts Extension instances to add to.
1326 *
1327 * @return LY_ERR values.
1328 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001330parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001332 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001333 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001334 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001335 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336
1337 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001338 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 return LY_EVALID;
1340 }
1341
1342 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001343 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001344
Radek Krejcif13b87b2020-12-01 22:02:17 +01001345 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001347 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 *flags |= LYS_CONFIG_R;
1349 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001350 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001351 free(buf);
1352 return LY_EVALID;
1353 }
1354 free(buf);
1355
Radek Krejci33090f92020-12-17 20:12:46 +01001356 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001357 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001358 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001359 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001360 break;
1361 default:
David Sedlákb3077192019-06-19 10:55:37 +02001362 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001363 return LY_EVALID;
1364 }
1365 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366 return ret;
1367}
1368
Michal Vaskoea5abea2018-09-18 13:10:54 +02001369/**
1370 * @brief Parse the mandatory statement.
1371 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001372 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001373 * @param[in,out] flags Flags to add to.
1374 * @param[in,out] exts Extension instances to add to.
1375 *
1376 * @return LY_ERR values.
1377 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001378static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001379parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001380{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001381 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001382 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001383 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001384 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001385
1386 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001387 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388 return LY_EVALID;
1389 }
1390
1391 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001392 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001393
Radek Krejcif13b87b2020-12-01 22:02:17 +01001394 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001395 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001396 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001397 *flags |= LYS_MAND_FALSE;
1398 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001399 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001400 free(buf);
1401 return LY_EVALID;
1402 }
1403 free(buf);
1404
Radek Krejci33090f92020-12-17 20:12:46 +01001405 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001406 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001407 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001408 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001409 break;
1410 default:
David Sedlákb3077192019-06-19 10:55:37 +02001411 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001412 return LY_EVALID;
1413 }
1414 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415 return ret;
1416}
1417
Michal Vaskoea5abea2018-09-18 13:10:54 +02001418/**
1419 * @brief Parse a restriction such as range or length.
1420 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001421 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001422 * @param[in] restr_kw Type of this particular restriction.
1423 * @param[in,out] exts Extension instances to add to.
1424 *
1425 * @return LY_ERR values.
1426 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001427static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001428parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001429{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001430 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001432 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001433 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434
1435 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001436 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437
Michal Vaskob36053d2020-03-26 15:49:30 +01001438 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001439 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001440 restr->arg.mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001441 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001442 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001443 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001444 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 +02001445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001446 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001447 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 +02001448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001449 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01001450 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 +02001451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001452 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001453 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 +02001454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001455 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001456 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001457 break;
1458 default:
David Sedlákb3077192019-06-19 10:55:37 +02001459 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001460 return LY_EVALID;
1461 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001462 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463 return ret;
1464}
1465
Michal Vaskoea5abea2018-09-18 13:10:54 +02001466/**
1467 * @brief Parse a restriction that can have more instances such as must.
1468 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001469 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001470 * @param[in] restr_kw Type of this particular restriction.
1471 * @param[in,out] restrs Restrictions to add to.
1472 *
1473 * @return LY_ERR values.
1474 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001476parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001477{
1478 struct lysp_restr *restr;
1479
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001480 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001481 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001482}
1483
Michal Vaskoea5abea2018-09-18 13:10:54 +02001484/**
1485 * @brief Parse the status statement.
1486 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001487 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001488 * @param[in,out] flags Flags to add to.
1489 * @param[in,out] exts Extension instances to add to.
1490 *
1491 * @return LY_ERR values.
1492 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001494parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001495{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001496 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001497 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001498 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001499 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500
1501 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001502 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 return LY_EVALID;
1504 }
1505
1506 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001507 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508
Radek Krejcif13b87b2020-12-01 22:02:17 +01001509 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001511 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001512 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001513 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 *flags |= LYS_STATUS_OBSLT;
1515 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001516 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 free(buf);
1518 return LY_EVALID;
1519 }
1520 free(buf);
1521
Radek Krejci33090f92020-12-17 20:12:46 +01001522 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001523 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001524 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001525 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526 break;
1527 default:
David Sedlákb3077192019-06-19 10:55:37 +02001528 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001529 return LY_EVALID;
1530 }
1531 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001532 return ret;
1533}
1534
Michal Vaskoea5abea2018-09-18 13:10:54 +02001535/**
1536 * @brief Parse the when statement.
1537 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001538 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001539 * @param[in,out] when_p When pointer to parse to.
1540 *
1541 * @return LY_ERR values.
1542 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001543LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001544parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001545{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001546 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001548 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001549 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550 struct lysp_when *when;
1551
1552 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001553 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554 return LY_EVALID;
1555 }
1556
1557 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001558 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001559 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001560
1561 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001562 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001563 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001564 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565
Radek Krejci33090f92020-12-17 20:12:46 +01001566 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001568 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001569 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 +02001570 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001571 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001572 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 +02001573 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001574 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001575 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_WHEN, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001576 break;
1577 default:
David Sedlákb3077192019-06-19 10:55:37 +02001578 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001579 return LY_EVALID;
1580 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001581 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001582 return ret;
1583}
1584
Michal Vaskoea5abea2018-09-18 13:10:54 +02001585/**
1586 * @brief Parse the anydata or anyxml statement.
1587 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001588 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001589 * @param[in] any_kw Type of this particular keyword.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001590 * @param[in,out] siblings Siblings to add to.
1591 *
1592 * @return LY_ERR values.
1593 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001594LY_ERR
Radek Krejci39b7fc22021-02-26 23:29:18 +01001595parse_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 +02001596{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001597 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001598 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001599 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001601 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001602
David Sedlák60adc092019-08-06 15:57:02 +02001603 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001604 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001605
Radek Krejci39b7fc22021-02-26 23:29:18 +01001606 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001607 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001608
Michal Vasko7fbc8162018-09-17 10:35:16 +02001609 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001610 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001611 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612
1613 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01001614 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001616 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001617 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001619 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001620 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 +02001621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001622 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001623 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001626 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001628 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001629 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001631 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001632 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 +02001633 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001634 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001635 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001637 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001638 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001640 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001641 LY_CHECK_RET(parse_ext(ctx, word, word_len, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001642 break;
1643 default:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001644 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001645 return LY_EVALID;
1646 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001648 return ret;
1649}
1650
Michal Vaskoea5abea2018-09-18 13:10:54 +02001651/**
1652 * @brief Parse the value or position statement. Substatement of type enum statement.
1653 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001654 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001655 * @param[in] val_kw Type of this particular keyword.
1656 * @param[in,out] value Value to write to.
1657 * @param[in,out] flags Flags to write to.
1658 * @param[in,out] exts Extension instances to add to.
1659 *
1660 * @return LY_ERR values.
1661 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001662LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001663parse_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 +02001664 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001665{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001666 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001668 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001669 long int num = 0;
1670 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001671 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672
1673 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001674 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001675 return LY_EVALID;
1676 }
1677 *flags |= LYS_SET_VALUE;
1678
1679 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001680 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681
Radek Krejcid6b76452019-09-03 17:03:03 +02001682 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 +02001683 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001684 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001685 }
1686
1687 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001688 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001689 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001690 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001691 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001692 goto error;
1693 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001694 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001695 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001696 if (unum > UINT64_C(4294967295)) {
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 }
1701 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001702 if ((size_t)(ptr - word) != word_len) {
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;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001705 }
1706 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001707 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001708 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001709 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001710 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001711 *value = num;
1712 } else {
1713 *value = unum;
1714 }
1715 free(buf);
1716
Radek Krejci33090f92020-12-17 20:12:46 +01001717 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001719 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001720 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 +02001721 break;
1722 default:
David Sedlákb3077192019-06-19 10:55:37 +02001723 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724 return LY_EVALID;
1725 }
1726 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001727 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001728
1729error:
1730 free(buf);
1731 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001732}
1733
Michal Vaskoea5abea2018-09-18 13:10:54 +02001734/**
1735 * @brief Parse the enum or bit statement. Substatement of type statement.
1736 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001737 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001738 * @param[in] enum_kw Type of this particular keyword.
1739 * @param[in,out] enums Enums or bits to add to.
1740 *
1741 * @return LY_ERR values.
1742 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001743static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001744parse_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 +02001745{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001746 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001747 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001748 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001749 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001750 struct lysp_type_enum *enm;
1751
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001752 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753
1754 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001755 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 +02001756 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001757 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001758 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001759 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001760
Radek Krejci011e4aa2020-09-04 15:22:31 +02001761 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001762 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1763
Radek Krejci33090f92020-12-17 20:12:46 +01001764 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001766 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001767 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 +02001768 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001769 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001770 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001771 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001773 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001774 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 +02001775 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001776 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001777 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001778 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001779 case LY_STMT_VALUE:
1780 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 +02001781 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001782 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001783 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001784 case LY_STMT_POSITION:
1785 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 +02001786 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001787 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001788 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001789 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001790 LY_CHECK_RET(parse_ext(ctx, word, word_len, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001791 break;
1792 default:
David Sedlákb3077192019-06-19 10:55:37 +02001793 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 return LY_EVALID;
1795 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001796 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797 return ret;
1798}
1799
Michal Vaskoea5abea2018-09-18 13:10:54 +02001800/**
1801 * @brief Parse the fraction-digits statement. Substatement of type statement.
1802 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001803 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001804 * @param[in,out] fracdig Value to write to.
1805 * @param[in,out] exts Extension instances to add to.
1806 *
1807 * @return LY_ERR values.
1808 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001809static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001810parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001812 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001813 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001814 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001816 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001817
1818 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001819 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001820 return LY_EVALID;
1821 }
1822
1823 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001824 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825
1826 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001827 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 free(buf);
1829 return LY_EVALID;
1830 }
1831
1832 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001833 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001834 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001835 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001836 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001837 free(buf);
1838 return LY_EVALID;
1839 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001840 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001841 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001842 free(buf);
1843 return LY_EVALID;
1844 }
1845 *fracdig = num;
1846 free(buf);
1847
Radek Krejci33090f92020-12-17 20:12:46 +01001848 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001849 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001850 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001851 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FRACTION_DIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001852 break;
1853 default:
David Sedlákb3077192019-06-19 10:55:37 +02001854 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001855 return LY_EVALID;
1856 }
1857 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001858 return ret;
1859}
1860
Michal Vaskoea5abea2018-09-18 13:10:54 +02001861/**
1862 * @brief Parse the require-instance statement. Substatement of type statement.
1863 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001864 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001865 * @param[in,out] reqinst Value to write to.
1866 * @param[in,out] flags Flags to write to.
1867 * @param[in,out] exts Extension instances to add to.
1868 *
1869 * @return LY_ERR values.
1870 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001872parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001873 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001875 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001877 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001878 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879
1880 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001881 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001882 return LY_EVALID;
1883 }
1884 *flags |= LYS_SET_REQINST;
1885
1886 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001887 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888
Radek Krejcif13b87b2020-12-01 22:02:17 +01001889 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001890 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001891 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001892 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001893 free(buf);
1894 return LY_EVALID;
1895 }
1896 free(buf);
1897
Radek Krejci33090f92020-12-17 20:12:46 +01001898 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001900 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001901 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REQUIRE_INSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902 break;
1903 default:
David Sedlákb3077192019-06-19 10:55:37 +02001904 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001905 return LY_EVALID;
1906 }
1907 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908 return ret;
1909}
1910
Michal Vaskoea5abea2018-09-18 13:10:54 +02001911/**
1912 * @brief Parse the modifier statement. Substatement of type pattern statement.
1913 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001914 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001915 * @param[in,out] pat Value to write to.
1916 * @param[in,out] exts Extension instances to add to.
1917 *
1918 * @return LY_ERR values.
1919 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001920static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001921parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001923 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001925 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001926 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927
Radek Krejcif13b87b2020-12-01 22:02:17 +01001928 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001929 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001930 return LY_EVALID;
1931 }
1932
1933 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001934 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001935
Radek Krejcif13b87b2020-12-01 22:02:17 +01001936 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001937 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001938 free(buf);
1939 return LY_EVALID;
1940 }
1941 free(buf);
1942
1943 /* replace the value in the dictionary */
1944 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001945 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001947 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001948
Radek Krejcif13b87b2020-12-01 22:02:17 +01001949 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
1950 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001951 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001952
Radek Krejci33090f92020-12-17 20:12:46 +01001953 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001954 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001955 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001956 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001957 break;
1958 default:
David Sedlákb3077192019-06-19 10:55:37 +02001959 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960 return LY_EVALID;
1961 }
1962 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001963 return ret;
1964}
1965
Michal Vaskoea5abea2018-09-18 13:10:54 +02001966/**
1967 * @brief Parse the pattern statement. Substatement of type statement.
1968 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001969 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001970 * @param[in,out] patterns Restrictions to add to.
1971 *
1972 * @return LY_ERR values.
1973 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001974static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001975parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001977 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001979 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001980 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981 struct lysp_restr *restr;
1982
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001983 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984
1985 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001986 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987
1988 /* add special meaning first byte */
1989 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001990 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991 word = buf;
1992 } else {
1993 buf = malloc(word_len + 2);
1994 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001995 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001996 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001997 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02001998 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001999 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
2000 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001
Radek Krejci33090f92020-12-17 20:12:46 +01002002 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002004 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002005 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 +02002006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002007 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002008 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 +02002009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002010 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01002011 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 +02002012 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002013 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002014 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 +02002015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002016 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002017 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002018 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002020 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002021 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022 break;
2023 default:
David Sedlákb3077192019-06-19 10:55:37 +02002024 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025 return LY_EVALID;
2026 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028 return ret;
2029}
2030
Michal Vaskoea5abea2018-09-18 13:10:54 +02002031/**
2032 * @brief Parse the type statement.
2033 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002034 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002035 * @param[in,out] type Type to wrote to.
2036 *
2037 * @return LY_ERR values.
2038 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002040parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002042 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002044 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002045 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002046 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047 struct lysp_type *nest_type;
2048
2049 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002050 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002051 return LY_EVALID;
2052 }
2053
2054 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002055 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002056 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002057
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002058 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002059 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002060
Radek Krejci33090f92020-12-17 20:12:46 +01002061 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002063 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002064 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 +01002065 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002067 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002068 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002069 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002071 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002072 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002073 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002075 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002076 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002077 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002079 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002081 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 return LY_EVALID;
2083 }
2084 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002085 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086
Radek Krejci33090f92020-12-17 20:12:46 +01002087 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002088 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002090 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002091 if (type->path) {
Radek Krejci3972b332021-03-02 16:34:31 +01002092 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002093 return LY_EVALID;
2094 }
2095
Radek Krejcifc596f92021-02-26 22:40:26 +01002096 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 +02002097 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 +02002098 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002099 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002100 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002101 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002103 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002104 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002105 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002107 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002108 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002109 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002110 return LY_EVALID;
2111 }
2112 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002113 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114
Radek Krejci33090f92020-12-17 20:12:46 +01002115 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002116 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002118 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002119 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002120 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002122 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002123 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002124 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002125 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002127 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002128 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 break;
2130 default:
David Sedlákb3077192019-06-19 10:55:37 +02002131 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132 return LY_EVALID;
2133 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 return ret;
2136}
2137
Michal Vaskoea5abea2018-09-18 13:10:54 +02002138/**
2139 * @brief Parse the leaf statement.
2140 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002141 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002142 * @param[in,out] siblings Siblings to add to.
2143 *
2144 * @return LY_ERR values.
2145 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002146LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002147parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002149 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002150 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002151 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002152 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153 struct lysp_node_leaf *leaf;
2154
David Sedlák60adc092019-08-06 15:57:02 +02002155 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002156 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002157 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002158 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159
Michal Vasko7fbc8162018-09-17 10:35:16 +02002160 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002161 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002162 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163
2164 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002165 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002167 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002168 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002170 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002171 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 +02002172 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002174 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002175 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 +02002176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002177 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002178 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002180 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002181 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002183 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002184 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002186 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002187 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 +02002188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002189 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002190 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002192 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002193 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002195 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002196 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 +02002197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002198 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002199 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002201 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002202 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002203 break;
2204 default:
David Sedlákb3077192019-06-19 10:55:37 +02002205 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002206 return LY_EVALID;
2207 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002208 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002209 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002210checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211 /* mandatory substatements */
2212 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002213 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 return LY_EVALID;
2215 }
2216
2217 return ret;
2218}
2219
Michal Vaskoea5abea2018-09-18 13:10:54 +02002220/**
2221 * @brief Parse the max-elements statement.
2222 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002223 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002224 * @param[in,out] max Value to write to.
2225 * @param[in,out] flags Flags to write to.
2226 * @param[in,out] exts Extension instances to add to.
2227 *
2228 * @return LY_ERR values.
2229 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002230LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002231parse_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 +02002232{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002233 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002235 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002237 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002238
2239 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002240 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241 return LY_EVALID;
2242 }
2243 *flags |= LYS_SET_MAX;
2244
2245 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002246 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002247
2248 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002249 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250 free(buf);
2251 return LY_EVALID;
2252 }
2253
Radek Krejci7f9b6512019-09-18 13:11:09 +02002254 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002256 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002257 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002258 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002259 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 free(buf);
2261 return LY_EVALID;
2262 }
2263 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002264 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002265 free(buf);
2266 return LY_EVALID;
2267 }
2268
2269 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002270 } else {
2271 /* unbounded */
2272 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002273 }
2274 free(buf);
2275
Radek Krejci33090f92020-12-17 20:12:46 +01002276 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002278 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002279 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MAX_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002280 break;
2281 default:
David Sedlákb3077192019-06-19 10:55:37 +02002282 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 return LY_EVALID;
2284 }
2285 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 return ret;
2287}
2288
Michal Vaskoea5abea2018-09-18 13:10:54 +02002289/**
2290 * @brief Parse the min-elements statement.
2291 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002292 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002293 * @param[in,out] min Value to write to.
2294 * @param[in,out] flags Flags to write to.
2295 * @param[in,out] exts Extension instances to add to.
2296 *
2297 * @return LY_ERR values.
2298 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002299LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002300parse_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 +02002301{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002302 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002303 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002304 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002305 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002306 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307
2308 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002309 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310 return LY_EVALID;
2311 }
2312 *flags |= LYS_SET_MIN;
2313
2314 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002315 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316
2317 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002318 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319 free(buf);
2320 return LY_EVALID;
2321 }
2322
2323 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002324 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002325 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002326 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002327 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 free(buf);
2329 return LY_EVALID;
2330 }
2331 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002332 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002333 free(buf);
2334 return LY_EVALID;
2335 }
2336 *min = num;
2337 free(buf);
2338
Radek Krejci33090f92020-12-17 20:12:46 +01002339 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002341 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002342 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MIN_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343 break;
2344 default:
David Sedlákb3077192019-06-19 10:55:37 +02002345 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 return LY_EVALID;
2347 }
2348 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349 return ret;
2350}
2351
Michal Vaskoea5abea2018-09-18 13:10:54 +02002352/**
2353 * @brief Parse the ordered-by statement.
2354 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002355 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002356 * @param[in,out] flags Flags to write to.
2357 * @param[in,out] exts Extension instances to add to.
2358 *
2359 * @return LY_ERR values.
2360 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002362parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002364 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002366 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002367 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368
2369 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002370 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002371 return LY_EVALID;
2372 }
2373
2374 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002375 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002376
Radek Krejcif13b87b2020-12-01 22:02:17 +01002377 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002379 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002380 *flags |= LYS_ORDBY_USER;
2381 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002382 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383 free(buf);
2384 return LY_EVALID;
2385 }
2386 free(buf);
2387
Radek Krejci33090f92020-12-17 20:12:46 +01002388 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002389 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002390 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002391 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ORDERED_BY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002392 break;
2393 default:
David Sedlákb3077192019-06-19 10:55:37 +02002394 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002395 return LY_EVALID;
2396 }
2397 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002398 return ret;
2399}
2400
Michal Vaskoea5abea2018-09-18 13:10:54 +02002401/**
2402 * @brief Parse the leaf-list statement.
2403 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002404 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002405 * @param[in,out] siblings Siblings to add to.
2406 *
2407 * @return LY_ERR values.
2408 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002409LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002410parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002412 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002414 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002415 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416 struct lysp_node_leaflist *llist;
2417
David Sedlák60adc092019-08-06 15:57:02 +02002418 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002419 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002421 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002424 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002425 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426
2427 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002428 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002429 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002430 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002431 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002433 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002434 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002435 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002437 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002438 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 +02002439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002440 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002441 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, 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_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002444 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002446 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002447 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002449 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002450 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002452 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002453 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002455 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002456 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 +02002457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002458 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002459 LY_CHECK_RET(parse_status(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_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002462 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002464 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002465 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 +02002466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002467 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002468 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002470 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002471 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002472 break;
2473 default:
David Sedlákb3077192019-06-19 10:55:37 +02002474 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 return LY_EVALID;
2476 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002478 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002479checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 /* mandatory substatements */
2481 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002482 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483 return LY_EVALID;
2484 }
2485
2486 return ret;
2487}
2488
Michal Vaskoea5abea2018-09-18 13:10:54 +02002489/**
2490 * @brief Parse the refine statement.
2491 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002492 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002493 * @param[in,out] refines Refines to add to.
2494 *
2495 * @return LY_ERR values.
2496 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002498parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002500 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002502 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002503 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504 struct lysp_refine *rf;
2505
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002506 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507
2508 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002509 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002510 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002511 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002512
Radek Krejci33090f92020-12-17 20:12:46 +01002513 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002514 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002515 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002516 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002518 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002519 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002520 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002521 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002522 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 +02002523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002524 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002525 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002526 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002528 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002529 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002530 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002531 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002532 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002534 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002535 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002536 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002537 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002538 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002540 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002541 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 +02002542 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002543 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002544 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 +02002545 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002546 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002547 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002548 break;
2549 default:
David Sedlákb3077192019-06-19 10:55:37 +02002550 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002551 return LY_EVALID;
2552 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002553 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554 return ret;
2555}
2556
Michal Vaskoea5abea2018-09-18 13:10:54 +02002557/**
2558 * @brief Parse the typedef statement.
2559 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002560 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002561 * @param[in,out] typedefs Typedefs to add to.
2562 *
2563 * @return LY_ERR values.
2564 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002565static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002566parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002568 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002569 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002570 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002571 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572 struct lysp_tpdf *tpdf;
2573
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002574 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575
2576 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002577 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002578 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002579
2580 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002581 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002582 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002583 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002584 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 +02002585 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002587 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002588 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 +02002589 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002590 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002591 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 +02002592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002593 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002594 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002595 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002596 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002597 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002598 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002599 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002600 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 +02002601 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002602 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002603 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002604 break;
2605 default:
David Sedlákb3077192019-06-19 10:55:37 +02002606 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607 return LY_EVALID;
2608 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002610 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002611checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 /* mandatory substatements */
2613 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002614 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615 return LY_EVALID;
2616 }
2617
Radek Krejcibbe09a92018-11-08 09:36:54 +01002618 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002619 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002620 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002621 }
2622
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623 return ret;
2624}
2625
Michal Vaskoea5abea2018-09-18 13:10:54 +02002626/**
2627 * @brief Parse the input or output statement.
2628 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002629 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002630 * @param[in] kw Type of this particular keyword
2631 * @param[in,out] inout_p Input/output pointer to write to.
2632 *
2633 * @return LY_ERR values.
2634 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002636parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002637 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002639 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002641 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002642 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002643 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002645 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002646 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647 return LY_EVALID;
2648 }
2649
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002650 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002651 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2652 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002653 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654
2655 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002656 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002658 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002659 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002660 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002661 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002662 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002664 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002665 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002667 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002668 LY_CHECK_RET(parse_container(ctx, (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_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002671 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002674 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002677 LY_CHECK_RET(parse_list(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_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002680 LY_CHECK_RET(parse_uses(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_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002683 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002685 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002686 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002687 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002689 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002690 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002692 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002693 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 break;
2695 default:
David Sedlákb3077192019-06-19 10:55:37 +02002696 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 return LY_EVALID;
2698 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002699 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002700 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002701
Radek Krejci7fc68292019-06-12 13:51:09 +02002702checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002703 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002704 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2705 return LY_EVALID;
2706 }
2707
Michal Vasko7fbc8162018-09-17 10:35:16 +02002708 return ret;
2709}
2710
Michal Vaskoea5abea2018-09-18 13:10:54 +02002711/**
2712 * @brief Parse the action statement.
2713 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002714 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002715 * @param[in,out] actions Actions to add to.
2716 *
2717 * @return LY_ERR values.
2718 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002719LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002720parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002722 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002723 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002724 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002725 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002726 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727
Radek Krejci2a9fc652021-01-22 17:44:34 +01002728 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729
2730 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002731 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002732 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002733 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002734 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735
Radek Krejci33090f92020-12-17 20:12:46 +01002736 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002738 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002739 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 +02002740 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002741 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002742 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002743 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002744 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002745 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 +02002746 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002747 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002748 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749 break;
2750
Radek Krejcid6b76452019-09-03 17:03:03 +02002751 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002752 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002754 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002755 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002756 break;
2757
Radek Krejcid6b76452019-09-03 17:03:03 +02002758 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002759 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002760 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002761 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002762 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002764 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002765 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 +02002766 break;
2767 default:
David Sedlákb3077192019-06-19 10:55:37 +02002768 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 return LY_EVALID;
2770 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002771 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002772 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002773
Radek Krejci2a9fc652021-01-22 17:44:34 +01002774checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002775 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2776 if (!act->input.nodetype) {
2777 act->input.nodetype = LYS_INPUT;
2778 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002779 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002780 }
2781 if (!act->output.nodetype) {
2782 act->output.nodetype = LYS_OUTPUT;
2783 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002784 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002785 }
2786
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 return ret;
2788}
2789
Michal Vaskoea5abea2018-09-18 13:10:54 +02002790/**
2791 * @brief Parse the notification statement.
2792 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002793 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002794 * @param[in,out] notifs Notifications to add to.
2795 *
2796 * @return LY_ERR values.
2797 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002798LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002799parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002801 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002803 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002804 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002805 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806
Radek Krejci2a9fc652021-01-22 17:44:34 +01002807 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808
2809 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002810 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002811 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002812 notif->nodetype = LYS_NOTIF;
2813 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814
Radek Krejci2a9fc652021-01-22 17:44:34 +01002815 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002816 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002817 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002818 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 +02002819 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002820 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002821 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002822 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002823 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002824 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 +02002825 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002826 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002827 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 break;
2829
Radek Krejcid6b76452019-09-03 17:03:03 +02002830 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002831 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002832 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002833 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002834 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002835 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002836 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002837 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002838 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002839 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002840 LY_CHECK_RET(parse_container(ctx, (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_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002843 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002846 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002849 LY_CHECK_RET(parse_list(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_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002852 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853 break;
2854
Radek Krejcid6b76452019-09-03 17:03:03 +02002855 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002856 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002857 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002859 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002860 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002862 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002863 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002865 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002866 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 break;
2868 default:
David Sedlákb3077192019-06-19 10:55:37 +02002869 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 return LY_EVALID;
2871 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002872 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002873
Michal Vasko7fbc8162018-09-17 10:35:16 +02002874 return ret;
2875}
2876
Michal Vaskoea5abea2018-09-18 13:10:54 +02002877/**
2878 * @brief Parse the grouping statement.
2879 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002880 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002881 * @param[in,out] groupings Groupings to add to.
2882 *
2883 * @return LY_ERR values.
2884 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002885LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002886parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002887{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002888 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002890 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002891 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002892 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002893
Radek Krejci2a9fc652021-01-22 17:44:34 +01002894 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895
2896 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002897 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002898 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002899 grp->nodetype = LYS_GROUPING;
2900 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901
Radek Krejci2a9fc652021-01-22 17:44:34 +01002902 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002903 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002904 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002905 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 +02002906 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002907 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002908 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 +02002909 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002910 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002911 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002912 break;
2913
Radek Krejcid6b76452019-09-03 17:03:03 +02002914 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002915 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002916 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002917 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002918 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002920 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002921 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002922 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002923 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002924 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002926 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002927 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002930 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002933 LY_CHECK_RET(parse_list(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_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002936 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002937 break;
2938
Radek Krejcid6b76452019-09-03 17:03:03 +02002939 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002940 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002942 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002943 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002944 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002945 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002946 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002947 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002949 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002950 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002951 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002953 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002954 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955 break;
2956 default:
David Sedlákb3077192019-06-19 10:55:37 +02002957 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 return LY_EVALID;
2959 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002961
Michal Vasko7fbc8162018-09-17 10:35:16 +02002962 return ret;
2963}
2964
Michal Vaskoea5abea2018-09-18 13:10:54 +02002965/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002966 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002967 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002968 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002969 * @param[in,out] augments Augments to add to.
2970 *
2971 * @return LY_ERR values.
2972 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002973LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002974parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002976 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002978 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002979 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002980 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981
Radek Krejci2a9fc652021-01-22 17:44:34 +01002982 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983
2984 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002985 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002986 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002987 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002988 aug->nodetype = LYS_AUGMENT;
2989 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990
Radek Krejci2a9fc652021-01-22 17:44:34 +01002991 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002993 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002994 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 +02002995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002996 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002997 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002999 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003000 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 +02003001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003002 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003003 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003004 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003005 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003006 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003007 break;
3008
Radek Krejcid6b76452019-09-03 17:03:03 +02003009 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003010 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003011 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003012 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003013 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003015 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003016 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003018 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003019 LY_CHECK_RET(parse_choice(ctx, (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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003022 LY_CHECK_RET(parse_container(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_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003025 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003028 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003031 LY_CHECK_RET(parse_list(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_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003034 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 break;
3036
Radek Krejcid6b76452019-09-03 17:03:03 +02003037 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003038 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003039 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003041 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003042 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003043 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003044 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003045 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003046 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 break;
3048 default:
David Sedlákb3077192019-06-19 10:55:37 +02003049 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 return LY_EVALID;
3051 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003053
Michal Vasko7fbc8162018-09-17 10:35:16 +02003054 return ret;
3055}
3056
Michal Vaskoea5abea2018-09-18 13:10:54 +02003057/**
3058 * @brief Parse the uses statement.
3059 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003060 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003061 * @param[in,out] siblings Siblings to add to.
3062 *
3063 * @return LY_ERR values.
3064 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003065LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003066parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003067{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003068 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003070 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003071 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 struct lysp_node_uses *uses;
3073
David Sedlák60adc092019-08-06 15:57:02 +02003074 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003075 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003076 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003077 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003080 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003081 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003082
3083 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003084 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003086 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003087 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 +02003088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003089 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003090 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003091 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003092 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003093 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 +02003094 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003095 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003096 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003098 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003099 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003100 break;
3101
Radek Krejcid6b76452019-09-03 17:03:03 +02003102 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003103 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003105 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003106 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003108 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003109 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 break;
3111 default:
David Sedlákb3077192019-06-19 10:55:37 +02003112 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003113 return LY_EVALID;
3114 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003116
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117 return ret;
3118}
3119
Michal Vaskoea5abea2018-09-18 13:10:54 +02003120/**
3121 * @brief Parse the case statement.
3122 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003123 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003124 * @param[in,out] siblings Siblings to add to.
3125 *
3126 * @return LY_ERR values.
3127 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003128LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003129parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003131 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003133 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003134 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 struct lysp_node_case *cas;
3136
David Sedlák60adc092019-08-06 15:57:02 +02003137 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003138 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003140 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003143 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003144 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145
3146 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003147 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003149 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003150 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 +02003151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003152 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003153 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003155 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003156 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 +02003157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003158 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003159 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003161 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003162 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003163 break;
3164
Radek Krejcid6b76452019-09-03 17:03:03 +02003165 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003166 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003167 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003168 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003169 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003171 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003172 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003174 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003175 LY_CHECK_RET(parse_container(ctx, (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_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003178 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003181 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003184 LY_CHECK_RET(parse_list(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_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003187 LY_CHECK_RET(parse_uses(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_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003190 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191 break;
3192 default:
David Sedlákb3077192019-06-19 10:55:37 +02003193 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 return LY_EVALID;
3195 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003197 return ret;
3198}
3199
Michal Vaskoea5abea2018-09-18 13:10:54 +02003200/**
3201 * @brief Parse the choice statement.
3202 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003203 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003204 * @param[in,out] siblings Siblings to add to.
3205 *
3206 * @return LY_ERR values.
3207 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003208LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003209parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003211 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003213 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003214 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003215 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216
David Sedlák60adc092019-08-06 15:57:02 +02003217 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003218 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003219 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003220 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003223 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003224 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225
3226 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003227 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003229 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003230 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003232 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003233 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 +02003234 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003235 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003236 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003238 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003239 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003241 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003242 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 +02003243 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003244 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003245 LY_CHECK_RET(parse_status(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_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003248 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003250 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01003251 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 +02003252 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003253 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 break;
3255
Radek Krejcid6b76452019-09-03 17:03:03 +02003256 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003257 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003258 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003259 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003260 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003261 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003262 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003263 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003265 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003266 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003267 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003269 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003270 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003272 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003273 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003276 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003279 LY_CHECK_RET(parse_list(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_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003282 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283 break;
3284 default:
David Sedlákb3077192019-06-19 10:55:37 +02003285 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 return LY_EVALID;
3287 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003289 return ret;
3290}
3291
Michal Vaskoea5abea2018-09-18 13:10:54 +02003292/**
3293 * @brief Parse the container statement.
3294 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003295 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003296 * @param[in,out] siblings Siblings to add to.
3297 *
3298 * @return LY_ERR values.
3299 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003300LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003301parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003302{
3303 LY_ERR ret = 0;
3304 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003305 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003306 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 struct lysp_node_container *cont;
3308
David Sedlák60adc092019-08-06 15:57:02 +02003309 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003310 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003312 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003315 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003316 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317
3318 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003319 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003321 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003322 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003324 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003325 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 +02003326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003328 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003331 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 +02003332 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003333 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003334 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003336 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003337 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003339 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003340 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 +02003341 break;
3342
Radek Krejcid6b76452019-09-03 17:03:03 +02003343 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003344 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003345 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003346 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003347 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003349 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003350 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003352 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003353 LY_CHECK_RET(parse_container(ctx, (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_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003356 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003359 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003362 LY_CHECK_RET(parse_list(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_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003365 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 break;
3367
Radek Krejcid6b76452019-09-03 17:03:03 +02003368 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003369 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003371 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003372 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003374 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003375 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003376 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003378 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003379 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003381 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003382 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003383 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003385 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003386 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003387 break;
3388 default:
David Sedlákb3077192019-06-19 10:55:37 +02003389 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 return LY_EVALID;
3391 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003393
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394 return ret;
3395}
3396
Michal Vaskoea5abea2018-09-18 13:10:54 +02003397/**
3398 * @brief Parse the list statement.
3399 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003400 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003401 * @param[in,out] siblings Siblings to add to.
3402 *
3403 * @return LY_ERR values.
3404 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003405LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003406parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003407{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003408 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003410 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003411 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 struct lysp_node_list *list;
3413
David Sedlák60adc092019-08-06 15:57:02 +02003414 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003415 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003417 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418
Michal Vasko7fbc8162018-09-17 10:35:16 +02003419 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003420 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003421 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003422
3423 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003424 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003425 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003426 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003427 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003429 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003430 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 +02003431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003432 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003433 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003435 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003436 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 +02003437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003438 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003439 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003441 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003442 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003444 case LY_STMT_KEY:
Radek Krejcifc596f92021-02-26 22:40:26 +01003445 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 +02003446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003448 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003451 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003454 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003456 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003457 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 break;
3459
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003461 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003462 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003463 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003464 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003466 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003467 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003468 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003469 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003470 LY_CHECK_RET(parse_container(ctx, (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_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003473 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003476 LY_CHECK_RET(parse_leaflist(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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003479 LY_CHECK_RET(parse_list(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_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003482 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
3484
Radek Krejcid6b76452019-09-03 17:03:03 +02003485 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003486 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003488 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003489 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003491 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003492 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003493 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003495 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003496 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003498 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003499 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003500 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003501 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003502 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003503 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003504 break;
3505 default:
David Sedlákb3077192019-06-19 10:55:37 +02003506 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507 return LY_EVALID;
3508 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003510
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 return ret;
3512}
3513
Michal Vaskoea5abea2018-09-18 13:10:54 +02003514/**
3515 * @brief Parse the yin-element statement.
3516 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003517 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003518 * @param[in,out] flags Flags to write to.
3519 * @param[in,out] exts Extension instances to add to.
3520 *
3521 * @return LY_ERR values.
3522 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003524parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003526 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003528 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003529 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530
3531 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003532 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533 return LY_EVALID;
3534 }
3535
3536 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003537 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538
Radek Krejcif13b87b2020-12-01 22:02:17 +01003539 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003541 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 *flags |= LYS_YINELEM_FALSE;
3543 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003544 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545 free(buf);
3546 return LY_EVALID;
3547 }
3548 free(buf);
3549
Radek Krejci33090f92020-12-17 20:12:46 +01003550 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003552 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003553 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003554 LY_CHECK_RET(ret);
3555 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556 default:
David Sedlákb3077192019-06-19 10:55:37 +02003557 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 return LY_EVALID;
3559 }
3560 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003561 return ret;
3562}
3563
Michal Vaskoea5abea2018-09-18 13:10:54 +02003564/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003565 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003566 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003567 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003568 * @param[in,out] argument Value to write to.
3569 * @param[in,out] flags Flags to write to.
3570 * @param[in,out] exts Extension instances to add to.
3571 *
3572 * @return LY_ERR values.
3573 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003575parse_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 +02003576{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003577 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003579 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003580 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581
3582 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003583 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 return LY_EVALID;
3585 }
3586
3587 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003588 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003589 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590
Radek Krejci33090f92020-12-17 20:12:46 +01003591 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003593 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003594 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003596 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003597 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 break;
3599 default:
David Sedlákb3077192019-06-19 10:55:37 +02003600 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601 return LY_EVALID;
3602 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003603 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604 return ret;
3605}
3606
Michal Vaskoea5abea2018-09-18 13:10:54 +02003607/**
3608 * @brief Parse the extension statement.
3609 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003610 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003611 * @param[in,out] extensions Extensions to add to.
3612 *
3613 * @return LY_ERR values.
3614 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003616parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003618 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003619 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003620 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003621 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622 struct lysp_ext *ex;
3623
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003624 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625
3626 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003627 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003628 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629
Radek Krejci33090f92020-12-17 20:12:46 +01003630 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003632 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003633 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 +02003634 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003635 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003636 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 +02003637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003638 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003639 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003641 case LY_STMT_ARGUMENT:
Radek Krejci9f87b0c2021-03-05 14:45:26 +01003642 LY_CHECK_RET(parse_argument(ctx, &ex->argname, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003644 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003645 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 break;
3647 default:
David Sedlákb3077192019-06-19 10:55:37 +02003648 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 return LY_EVALID;
3650 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003651 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652 return ret;
3653}
3654
Michal Vaskoea5abea2018-09-18 13:10:54 +02003655/**
3656 * @brief Parse the deviate statement.
3657 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003658 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003659 * @param[in,out] deviates Deviates to add to.
3660 *
3661 * @return LY_ERR values.
3662 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003663LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003664parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003666 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003668 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003669 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003670 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671 struct lysp_deviate_add *d_add = NULL;
3672 struct lysp_deviate_rpl *d_rpl = NULL;
3673 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003674 const char **d_units = NULL;
3675 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003676 struct lysp_restr **d_musts = NULL;
3677 uint16_t *d_flags = 0;
3678 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679
3680 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003681 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682
Radek Krejcif13b87b2020-12-01 22:02:17 +01003683 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003684 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003685 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003686 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003687 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003689 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 dev_mod = LYS_DEV_DELETE;
3691 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003692 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003693 free(buf);
3694 return LY_EVALID;
3695 }
3696 free(buf);
3697
3698 /* create structure */
3699 switch (dev_mod) {
3700 case LYS_DEV_NOT_SUPPORTED:
3701 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003702 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003703 break;
3704 case LYS_DEV_ADD:
3705 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003706 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003707 d = (struct lysp_deviate *)d_add;
3708 d_units = &d_add->units;
3709 d_uniques = &d_add->uniques;
3710 d_dflts = &d_add->dflts;
3711 d_musts = &d_add->musts;
3712 d_flags = &d_add->flags;
3713 d_min = &d_add->min;
3714 d_max = &d_add->max;
3715 break;
3716 case LYS_DEV_REPLACE:
3717 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003718 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719 d = (struct lysp_deviate *)d_rpl;
3720 d_units = &d_rpl->units;
3721 d_flags = &d_rpl->flags;
3722 d_min = &d_rpl->min;
3723 d_max = &d_rpl->max;
3724 break;
3725 case LYS_DEV_DELETE:
3726 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003727 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728 d = (struct lysp_deviate *)d_del;
3729 d_units = &d_del->units;
3730 d_uniques = &d_del->uniques;
3731 d_dflts = &d_del->dflts;
3732 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733 break;
3734 default:
3735 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003736 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003737 }
3738 d->mod = dev_mod;
3739
3740 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003741 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742
Radek Krejci33090f92020-12-17 20:12:46 +01003743 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003744 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003745 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003746 switch (dev_mod) {
3747 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003748 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003749 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 return LY_EVALID;
3751 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003752 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003753 break;
3754 }
3755 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003756 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 switch (dev_mod) {
3758 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003759 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760 return LY_EVALID;
3761 case LYS_DEV_REPLACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003762 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 +02003763 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764 break;
3765 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003766 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003767 break;
3768 }
3769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003770 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003771 switch (dev_mod) {
3772 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003773 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003774 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 return LY_EVALID;
3776 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003777 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 break;
3779 }
3780 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003781 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003782 switch (dev_mod) {
3783 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003784 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003785 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786 return LY_EVALID;
3787 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003788 LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003789 break;
3790 }
3791 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003792 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003793 switch (dev_mod) {
3794 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003795 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003796 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003797 return LY_EVALID;
3798 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003799 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003800 break;
3801 }
3802 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003803 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003804 switch (dev_mod) {
3805 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003806 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003807 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 return LY_EVALID;
3809 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003810 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003811 break;
3812 }
3813 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003814 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003815 switch (dev_mod) {
3816 case LYS_DEV_NOT_SUPPORTED:
3817 case LYS_DEV_ADD:
3818 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003819 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003820 return LY_EVALID;
3821 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003822 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003823 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003824 return LY_EVALID;
3825 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003827 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003828 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 break;
3830 }
3831 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003832 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003833 switch (dev_mod) {
3834 case LYS_DEV_NOT_SUPPORTED:
3835 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003836 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003837 return LY_EVALID;
3838 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003839 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003840 break;
3841 }
3842 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003843 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844 switch (dev_mod) {
3845 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003846 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847 return LY_EVALID;
3848 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003849 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 +02003850 break;
3851 }
3852 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003853 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003854 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATE, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855 break;
3856 default:
David Sedlákb3077192019-06-19 10:55:37 +02003857 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858 return LY_EVALID;
3859 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003860 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 return ret;
3862}
3863
Michal Vaskoea5abea2018-09-18 13:10:54 +02003864/**
3865 * @brief Parse the deviation statement.
3866 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003867 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003868 * @param[in,out] deviations Deviations to add to.
3869 *
3870 * @return LY_ERR values.
3871 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003872LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003873parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003875 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003876 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003877 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003878 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003879 struct lysp_deviation *dev;
3880
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003881 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003882
3883 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003884 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003885 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003886 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887
Radek Krejci33090f92020-12-17 20:12:46 +01003888 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003889 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003890 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003891 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 +02003892 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003893 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003894 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003896 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003897 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 +02003898 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003899 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003900 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATION, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 break;
3902 default:
David Sedlákb3077192019-06-19 10:55:37 +02003903 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 return LY_EVALID;
3905 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003906 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003907 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003908checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003909 /* mandatory substatements */
3910 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003911 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 return LY_EVALID;
3913 }
3914
3915 return ret;
3916}
3917
Michal Vaskoea5abea2018-09-18 13:10:54 +02003918/**
3919 * @brief Parse the feature statement.
3920 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003921 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003922 * @param[in,out] features Features to add to.
3923 *
3924 * @return LY_ERR values.
3925 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003926LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003927parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003929 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003931 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003932 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933 struct lysp_feature *feat;
3934
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003935 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936
3937 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003938 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003939 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003940
Radek Krejci33090f92020-12-17 20:12:46 +01003941 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003943 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003944 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 +02003945 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003946 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003947 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003948 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003949 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003950 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 +02003951 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003952 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003953 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003955 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003956 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003957 break;
3958 default:
David Sedlákb3077192019-06-19 10:55:37 +02003959 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003960 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003963 return ret;
3964}
3965
Michal Vaskoea5abea2018-09-18 13:10:54 +02003966/**
3967 * @brief Parse the identity statement.
3968 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003969 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003970 * @param[in,out] identities Identities to add to.
3971 *
3972 * @return LY_ERR values.
3973 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003974LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003975parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003977 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003979 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003980 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981 struct lysp_ident *ident;
3982
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003983 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984
3985 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003986 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003987 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003988
Radek Krejci33090f92020-12-17 20:12:46 +01003989 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003991 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003992 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 +02003993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003994 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02003995 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01003996 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003998 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003999 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 +02004000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004001 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004002 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004004 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004005 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004006 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 +01004007 return LY_EVALID;
4008 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004009 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 +02004010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004011 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004012 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 break;
4014 default:
David Sedlákb3077192019-06-19 10:55:37 +02004015 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016 return LY_EVALID;
4017 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004018 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019 return ret;
4020}
4021
Michal Vaskoea5abea2018-09-18 13:10:54 +02004022/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004023 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004024 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004025 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004026 * @param[in,out] mod Module to write to.
4027 *
4028 * @return LY_ERR values.
4029 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004030LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004031parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004032{
4033 LY_ERR ret = 0;
4034 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004035 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004036 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004037 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004038 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004039
Michal Vaskoc3781c32020-10-06 14:04:08 +02004040 mod->is_submod = 0;
4041
4042 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004043 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004044 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004045
Radek Krejci33090f92020-12-17 20:12:46 +01004046 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004047
Radek Krejcie3846472018-10-15 15:24:51 +02004048#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004049 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 +02004050
Michal Vasko7fbc8162018-09-17 10:35:16 +02004051 switch (kw) {
4052 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004053 case LY_STMT_NAMESPACE:
4054 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004055 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4056 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004057 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004058 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059 break;
4060 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004061 case LY_STMT_INCLUDE:
4062 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004063 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004064 break;
4065 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004066 case LY_STMT_ORGANIZATION:
4067 case LY_STMT_CONTACT:
4068 case LY_STMT_DESCRIPTION:
4069 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004070 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004071 break;
4072
4073 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004074 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004075 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004077 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004078 case LY_STMT_ANYDATA:
4079 case LY_STMT_ANYXML:
4080 case LY_STMT_AUGMENT:
4081 case LY_STMT_CHOICE:
4082 case LY_STMT_CONTAINER:
4083 case LY_STMT_DEVIATION:
4084 case LY_STMT_EXTENSION:
4085 case LY_STMT_FEATURE:
4086 case LY_STMT_GROUPING:
4087 case LY_STMT_IDENTITY:
4088 case LY_STMT_LEAF:
4089 case LY_STMT_LEAF_LIST:
4090 case LY_STMT_LIST:
4091 case LY_STMT_NOTIFICATION:
4092 case LY_STMT_RPC:
4093 case LY_STMT_TYPEDEF:
4094 case LY_STMT_USES:
4095 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004096 mod_stmt = Y_MOD_BODY;
4097 break;
4098 default:
4099 /* error handled in the next switch */
4100 break;
4101 }
Radek Krejcie3846472018-10-15 15:24:51 +02004102#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004103
Radek Krejcie3846472018-10-15 15:24:51 +02004104 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004105 switch (kw) {
4106 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004107 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004108 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004109 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004110 case LY_STMT_NAMESPACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004111 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 +02004112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004113 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01004114 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 +02004115 break;
4116
4117 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004118 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004119 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004121 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004122 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004123 break;
4124
4125 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004126 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004127 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 +02004128 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004129 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004130 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 +02004131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004132 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004133 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 +02004134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004135 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004136 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 +02004137 break;
4138
4139 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004140 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004141 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004142 break;
4143
4144 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004145 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004146 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004147 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004148 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004149 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004151 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004152 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004154 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004155 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004157 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004158 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004160 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004161 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004163 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004164 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004167 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 break;
4169
Radek Krejcid6b76452019-09-03 17:03:03 +02004170 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004171 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004173 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004174 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004176 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004177 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004179 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004180 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004182 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004183 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004186 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004189 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004191 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004192 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004194 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004195 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004197 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004198 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 break;
4200
4201 default:
David Sedlákb3077192019-06-19 10:55:37 +02004202 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004203 return LY_EVALID;
4204 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004206 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004207
Radek Krejci6d6556c2018-11-08 09:37:45 +01004208checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004209 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004210 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004211 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004212 return LY_EVALID;
4213 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004214 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004215 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004216 }
4217
Radek Krejcie9e987e2018-10-31 12:50:27 +01004218 /* submodules share the namespace with the module names, so there must not be
4219 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004220 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004221 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004222 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004223 return LY_EVALID;
4224 }
4225
4226 return ret;
4227}
4228
4229/**
4230 * @brief Parse submodule substatements.
4231 *
4232 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004233 * @param[out] submod Parsed submodule structure.
4234 *
4235 * @return LY_ERR values.
4236 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004237LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004238parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004239{
4240 LY_ERR ret = 0;
4241 char *buf, *word;
4242 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004243 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004244 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004245 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004246
Michal Vaskoc3781c32020-10-06 14:04:08 +02004247 submod->is_submod = 1;
4248
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004249 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004250 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004251 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004252
Radek Krejci33090f92020-12-17 20:12:46 +01004253 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004254
4255#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004256 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 +01004257
4258 switch (kw) {
4259 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004260 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004261 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4262 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004263 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004264 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4265 break;
4266 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004267 case LY_STMT_INCLUDE:
4268 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004269 CHECK_ORDER(Y_MOD_LINKAGE);
4270 break;
4271 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004272 case LY_STMT_ORGANIZATION:
4273 case LY_STMT_CONTACT:
4274 case LY_STMT_DESCRIPTION:
4275 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004276 CHECK_ORDER(Y_MOD_META);
4277 break;
4278
4279 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004280 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004281 CHECK_ORDER(Y_MOD_REVISION);
4282 break;
4283 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004284 case LY_STMT_ANYDATA:
4285 case LY_STMT_ANYXML:
4286 case LY_STMT_AUGMENT:
4287 case LY_STMT_CHOICE:
4288 case LY_STMT_CONTAINER:
4289 case LY_STMT_DEVIATION:
4290 case LY_STMT_EXTENSION:
4291 case LY_STMT_FEATURE:
4292 case LY_STMT_GROUPING:
4293 case LY_STMT_IDENTITY:
4294 case LY_STMT_LEAF:
4295 case LY_STMT_LEAF_LIST:
4296 case LY_STMT_LIST:
4297 case LY_STMT_NOTIFICATION:
4298 case LY_STMT_RPC:
4299 case LY_STMT_TYPEDEF:
4300 case LY_STMT_USES:
4301 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004302 mod_stmt = Y_MOD_BODY;
4303 break;
4304 default:
4305 /* error handled in the next switch */
4306 break;
4307 }
4308#undef CHECK_ORDER
4309
4310 prev_kw = kw;
4311 switch (kw) {
4312 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004313 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004314 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004315 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004316 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004317 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004318 break;
4319
4320 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004321 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004322 if (submod->version == LYS_VERSION_1_1) {
4323 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4324 submod->name);
4325 }
Radek Krejci33090f92020-12-17 20:12:46 +01004326 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004328 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004329 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004330 break;
4331
4332 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004333 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004334 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 +01004335 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004336 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004337 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 +01004338 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004339 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004340 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 +01004341 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004342 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004343 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 +01004344 break;
4345
4346 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004347 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004348 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004349 break;
4350
4351 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004352 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004353 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004354 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004355 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004356 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004357 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004358 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004359 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004360 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004361 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004362 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004364 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004365 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004367 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004368 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004370 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004371 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004373 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004374 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004375 break;
4376
Radek Krejcid6b76452019-09-03 17:03:03 +02004377 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004378 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004380 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004381 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004383 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004384 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004386 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004387 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004389 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004390 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004392 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004393 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004395 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004396 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004397 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004398 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004399 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004401 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004402 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004404 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004405 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004406 break;
4407
4408 default:
David Sedlákb3077192019-06-19 10:55:37 +02004409 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004410 return LY_EVALID;
4411 }
4412 }
4413 LY_CHECK_RET(ret);
4414
4415checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004416 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004417 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004418 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004419 return LY_EVALID;
4420 }
4421
4422 /* submodules share the namespace with the module names, so there must not be
4423 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004424 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004425 /* main modules may have different revisions */
4426 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004427 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004428 return LY_EVALID;
4429 }
4430
Michal Vasko7fbc8162018-09-17 10:35:16 +02004431 return ret;
4432}
4433
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004434/**
4435 * @brief Skip any redundant characters, namely whitespaces and comments.
4436 *
4437 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004438 * @return LY_SUCCESS on success.
4439 * @return LY_EVALID on invalid comment.
4440 */
4441static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004442skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004443{
4444 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004445 while (ctx->in->current[0]) {
4446 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004447 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004448 ly_in_skip(ctx->in, 2);
4449 LY_CHECK_RET(skip_comment(ctx, 1));
4450 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004451 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004452 ly_in_skip(ctx->in, 2);
4453 LY_CHECK_RET(skip_comment(ctx, 2));
4454 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004455 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004456 if (ctx->in->current[0] == '\n') {
4457 LY_IN_NEW_LINE(ctx->in);
4458 }
Radek Krejci33090f92020-12-17 20:12:46 +01004459 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004460 } else {
4461 break;
4462 }
4463 }
4464
4465 return LY_SUCCESS;
4466}
4467
Radek Krejcid4557c62018-09-17 11:42:09 +02004468LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004469yang_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 +02004470 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004471{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004472 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004473 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004474 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004475 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004476 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004477
David Sedlák1b623122019-08-05 15:27:49 +02004478 /* create context */
4479 *context = calloc(1, sizeof **context);
4480 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004481 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004482 (*context)->unres = main_ctx->unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004483 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004484
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004485 mod_p = calloc(1, sizeof *mod_p);
4486 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4487 mod_p->mod = main_ctx->parsed_mod->mod;
4488 mod_p->parsing = 1;
4489 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4490
Radek Krejciddace2c2021-01-08 11:30:56 +01004491 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004492
David Sedlák1b623122019-08-05 15:27:49 +02004493 /* map the typedefs and groupings list from main context to the submodule's context */
4494 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4495 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4496
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004497 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004498 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004499 LY_CHECK_GOTO(ret, cleanup);
4500
Michal Vasko7fbc8162018-09-17 10:35:16 +02004501 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004502 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004503 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004504
Radek Krejcid6b76452019-09-03 17:03:03 +02004505 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004506 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004507 ret = LY_EINVAL;
4508 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004509 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004510 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004511 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004512 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004513 }
4514
Michal Vasko7fbc8162018-09-17 10:35:16 +02004515 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004516 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004517 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004518
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004519 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004520 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004521 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004522 if (in->current[0]) {
4523 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004524 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004525 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004526 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004527
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004528 mod_p->parsing = 0;
4529 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004530
Radek Krejcibbe09a92018-11-08 09:36:54 +01004531cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004532 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004533 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004534 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004535 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004536 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004537 }
4538
4539 return ret;
4540}
4541
4542LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004543yang_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 +01004544{
4545 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004546 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004547 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004548 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004549 struct lysp_module *mod_p = NULL;
4550
David Sedlák1b623122019-08-05 15:27:49 +02004551 /* create context */
4552 *context = calloc(1, sizeof **context);
4553 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004554 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004555 (*context)->unres = unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004556 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004557
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004558 mod_p = calloc(1, sizeof *mod_p);
4559 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4560 mod_p->mod = mod;
4561 mod_p->parsing = 1;
4562 (*context)->parsed_mod = mod_p;
4563
Radek Krejciddace2c2021-01-08 11:30:56 +01004564 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004565
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004566 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004567 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004568 LY_CHECK_GOTO(ret, cleanup);
4569
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004570 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004571 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004572 LY_CHECK_GOTO(ret, cleanup);
4573
Radek Krejcid6b76452019-09-03 17:03:03 +02004574 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004575 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 +01004576 ret = LY_EINVAL;
4577 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004578 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004579 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004580 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004581 goto cleanup;
4582 }
4583
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004584 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004585 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004586 LY_CHECK_GOTO(ret, cleanup);
4587
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004588 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004589 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004590 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004591 if (in->current[0]) {
4592 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004593 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004594 goto cleanup;
4595 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004596
4597 mod_p->parsing = 0;
4598 mod->parsed = mod_p;
4599
4600cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004601 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004602 if (ret) {
4603 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004604 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004605 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004606 }
4607
Michal Vasko7fbc8162018-09-17 10:35:16 +02004608 return ret;
4609}