blob: c385f5ccdba65508b030039621f80fc061aac7bc [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko63f3d842020-07-08 10:10:14 +020014#include "parser_internal.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
17#include <ctype.h>
18#include <errno.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "log.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020030#include "path.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "set.h"
32#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010033#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020035#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020036
Radek Krejci77114102021-03-10 15:21:57 +010037struct lys_glob_unres;
38
Radek Krejciceaf2122019-01-02 15:03:26 +010039/**
40 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
41 * @param[in] CTX yang parser context to access libyang context.
42 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
43 * @param[out] TARGET variable where to store the pointer to the inserted value.
44 * @param[in] WORD string to store.
45 * @param[in] LEN length of the string in WORD to store.
46 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020047#define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \
Michal Vasko5d24f6c2020-10-13 13:49:06 +020048 if (BUF) {LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)));}\
49 else {LY_CHECK_RET(lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)));}
Radek Krejci44ceedc2018-10-02 15:54:31 +020050
Radek Krejciceaf2122019-01-02 15:03:26 +010051/**
Michal Vasko63f3d842020-07-08 10:10:14 +020052 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Radek Krejciceaf2122019-01-02 15:03:26 +010053 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010054 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
55 */
Radek Krejcid54412f2020-12-17 20:25:35 +010056#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020057
Michal Vaskoea5abea2018-09-18 13:10:54 +020058/**
59 * @brief Loop through all substatements providing, return if there are none.
60 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020061 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020062 * @param[out] KW YANG keyword read.
63 * @param[out] WORD Pointer to the keyword itself.
64 * @param[out] WORD_LEN Length of the keyword.
65 * @param[out] ERR Variable for error storing.
66 *
67 * @return In case there are no substatements or a fatal error encountered.
68 */
Radek Krejci33090f92020-12-17 20:12:46 +010069#define YANG_READ_SUBSTMT_FOR(CTX, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
70 LY_CHECK_RET(get_keyword(CTX, &KW, &WORD, &WORD_LEN)); \
Radek Krejcid6b76452019-09-03 17:03:03 +020071 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010072 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010073 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020074 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020075 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020076 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020077 return LY_EVALID; \
78 } \
Radek Krejci33090f92020-12-17 20:12:46 +010079 for (ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020080 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Radek Krejci33090f92020-12-17 20:12:46 +010081 ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020082
Radek Krejci33090f92020-12-17 20:12:46 +010083LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020084 struct lysp_node **siblings);
Radek Krejci33090f92020-12-17 20:12:46 +010085LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
86LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
87LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
88LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +010089LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020090
Michal Vaskoea5abea2018-09-18 13:10:54 +020091/**
92 * @brief Add another character to dynamic buffer, a low-level function.
93 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020094 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020095 *
Radek Krejci404251e2018-10-09 12:06:44 +020096 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +020097 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +020098 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020099 * @param[in,out] buf Buffer to use, can be moved by realloc().
100 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200101 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200102 *
103 * @return LY_ERR values.
104 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200105LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200106buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200107{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100108#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200109 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100110 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200111 *buf = ly_realloc(*buf, *buf_len);
112 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
113 }
Radek Krejcic0917392019-04-10 13:04:04 +0200114 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200116 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200117 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200118 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200119
Radek Krejci44ceedc2018-10-02 15:54:31 +0200120 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200121 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100122#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200123}
124
Michal Vaskoea5abea2018-09-18 13:10:54 +0200125/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
127 *
128 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200129 * @param[in] arg Type of the input string to select method of checking character validity.
130 * @param[in,out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first
Michal Vaskoea5abea2018-09-18 13:10:54 +0200131 * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
133 * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not requested (word is a substring of the data).
Michal Vaskoea5abea2018-09-18 13:10:54 +0200134 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200135 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200136 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
137 * 0 - colon not yet found (no prefix)
138 * 1 - \p c is the colon character
139 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200140 *
141 * @return LY_ERR values.
142 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200143LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100144buf_store_char(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200145 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200146{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200147 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200148 size_t len;
149
Radek Krejcif29b7c32019-04-09 16:17:49 +0200150 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
151 assert(!need_buf || (need_buf && word_b));
152
Radek Krejci44ceedc2018-10-02 15:54:31 +0200153 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100154 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
155 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
156 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200157 if (c == '\n') {
158 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100159 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200160 } else {
161 /* note - even the multibyte character is count as 1 */
162 ++ctx->indent;
163 }
164
Radek Krejci44ceedc2018-10-02 15:54:31 +0200165 /* check character validity */
166 switch (arg) {
167 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100168 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200169 break;
170 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100171 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200172 break;
173 case Y_STR_ARG:
174 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100175 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176 break;
177 }
178
Michal Vasko7fbc8162018-09-17 10:35:16 +0200179 if (word_b && *word_b) {
180 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100181 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200182 return LY_EMEM;
183 }
184
185 /* in case of realloc */
186 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200187 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200188 /* first time we need a buffer, copy everything read up to now */
189 if (*word_len) {
190 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200191 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200192 *buf_len = *word_len;
193 memcpy(*word_b, *word_p, *word_len);
194 }
195
196 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100197 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200198 return LY_EMEM;
199 }
200
201 /* in case of realloc */
202 *word_p = *word_b;
203 } else {
204 /* just remember the first character pointer */
205 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100206 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200207 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200208 /* ... and update the word's length */
209 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100210 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200211 }
212
213 return LY_SUCCESS;
214}
215
Michal Vaskoea5abea2018-09-18 13:10:54 +0200216/**
217 * @brief Skip YANG comment in data.
218 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200219 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200220 * @param[in] comment Type of the comment to process:
221 * 1 for a one-line comment,
222 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200223 * @return LY_ERR values.
224 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200225LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100226skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200227{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100228 /* internal statuses: */
229#define COMMENT_NO 0 /* comment ended */
230#define COMMENT_LINE 1 /* in line comment */
231#define COMMENT_BLOCK 2 /* in block comment */
232#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
233
Radek Krejci33090f92020-12-17 20:12:46 +0100234 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200235 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100236 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100237 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100238 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100239 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200240 }
241 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100242 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100243 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100244 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100245 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100246 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200247 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200248 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100249 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100250 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100251 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100252 } else if (ctx->in->current[0] != '*') {
253 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100254 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200255 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100256 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200257 }
258 break;
259 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200260 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200261 }
262
Radek Krejci33090f92020-12-17 20:12:46 +0100263 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200264 ctx->indent = 0;
265 } else {
266 ++ctx->indent;
267 }
Radek Krejci33090f92020-12-17 20:12:46 +0100268 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200269 }
270
Radek Krejci33090f92020-12-17 20:12:46 +0100271 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200272 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200273 return LY_EVALID;
274 }
275
276 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100277
278#undef COMMENT_NO
279#undef COMMENT_LINE
280#undef COMMENT_BLOCK
281#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200282}
283
Michal Vaskoea5abea2018-09-18 13:10:54 +0200284/**
285 * @brief Read a quoted string from data.
286 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200287 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200288 * @param[in] arg Type of YANG keyword argument expected.
289 * @param[out] word_p Pointer to the read quoted string.
290 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
291 * set to NULL. Otherwise equal to \p word_p.
292 * @param[out] word_len Length of the read quoted string.
293 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
294 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
295 * indenation in the final quoted string.
296 *
297 * @return LY_ERR values.
298 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200299static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100300read_qstring(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200301 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200302{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100303 /* string parsing status: */
304#define STRING_ENDED 0 /* string ended */
305#define STRING_SINGLE_QUOTED 1 /* string with ' */
306#define STRING_DOUBLE_QUOTED 2 /* string with " */
307#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
308#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
309#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
310
Radek Krejci1deb5be2020-08-26 16:43:36 +0200311 uint8_t string;
312 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200313 ly_bool need_buf = 0;
314 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200315 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200316 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200317
Radek Krejci33090f92020-12-17 20:12:46 +0100318 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100319 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200320 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100322 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100323 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200324 }
Radek Krejci33090f92020-12-17 20:12:46 +0100325 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200326
Radek Krejci33090f92020-12-17 20:12:46 +0100327 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200328 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100329 case STRING_SINGLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100330 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200331 case '\'':
332 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100333 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100334 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200335 break;
336 default:
337 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100338 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200339 break;
340 }
341 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100342 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100343 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200344 case '\"':
345 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100346 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100347 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200348 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200349 break;
350 case '\\':
351 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100352 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200353
354 /* the backslash sequence is substituted, so we will need a buffer to store the result */
355 need_buf = 1;
356
357 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100358 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200359
360 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
361 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
362 trailing_ws = 0;
363
364 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
365 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200366 break;
367 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200368 if (current_indent < block_indent) {
369 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100370 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200371 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100372 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100373 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100374 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200375 }
376 break;
377 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200378 if (current_indent < block_indent) {
379 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100380 current_indent += Y_TAB_SPACES;
381 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200382 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200383 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100384 c = ctx->in->current;
385 ctx->in->current = " ";
386 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
387 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100388 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200389 }
Radek Krejci33090f92020-12-17 20:12:46 +0100390 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200391 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100392 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100393 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100394 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200395 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100396 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200397 }
398 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200399 case '\r':
400 if (ctx->in->current[1] != '\n') {
401 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
402 return LY_EVALID;
403 }
404 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200405 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200406 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200407 /* we will be removing the indents so we need our own buffer */
408 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200409
410 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200411 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200412
Radek Krejciff13cd12019-10-25 15:34:24 +0200413 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200414 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200415 }
416
417 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100418 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200419
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200420 /* reset context indentation counter for possible string after this one */
421 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200422 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200423 break;
424 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200425 /* first non-whitespace character, stop eating indentation */
426 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200427
428 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100429 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200430 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200431 break;
432 }
433 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100434 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200435 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100436 c = ctx->in->current;
437 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100439 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100440 /* fix false newline count in buf_store_char() */
441 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200442 break;
443 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100444 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200445 break;
446 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200447 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200448 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200449 break;
450 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200451 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100452 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200453 return LY_EVALID;
454 }
455
456 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100457 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200458
Radek Krejcif13b87b2020-12-01 22:02:17 +0100459 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100460 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200461 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100462 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100463 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200464 case '+':
465 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100466 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200467 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200468 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200469 case '\r':
470 if (ctx->in->current[1] != '\n') {
471 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
472 return LY_EVALID;
473 }
474 MOVE_INPUT(ctx, 1);
475 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200476 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100477 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100478 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200479 case ' ':
480 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200481 /* just skip */
482 break;
483 default:
484 /* string is finished */
485 goto string_end;
486 }
Radek Krejci33090f92020-12-17 20:12:46 +0100487 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200488 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100489 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100490 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200491 case '\r':
492 if (ctx->in->current[1] != '\n') {
493 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
494 return LY_EVALID;
495 }
496 MOVE_INPUT(ctx, 1);
497 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200498 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100499 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100500 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200501 case ' ':
502 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200503 /* skip */
504 break;
505 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100506 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200507 break;
508 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100509 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200510 break;
511 default:
512 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200513 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200514 return LY_EVALID;
515 }
Radek Krejci33090f92020-12-17 20:12:46 +0100516 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200517 break;
518 default:
519 return LY_EINT;
520 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200521 }
522
523string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200524 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200525 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200526 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200527 return LY_EVALID;
528 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200529 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100530
531#undef STRING_ENDED
532#undef STRING_SINGLE_QUOTED
533#undef STRING_DOUBLE_QUOTED
534#undef STRING_DOUBLE_QUOTED_ESCAPED
535#undef STRING_PAUSED_NEXTSTRING
536#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537}
538
Michal Vaskoea5abea2018-09-18 13:10:54 +0200539/**
540 * @brief Get another YANG string from the raw data.
541 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200542 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200543 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200544 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
545 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200546 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200547 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
548 * set to NULL. Otherwise equal to \p word_p.
549 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200550 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200551 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200552LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100553get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200554 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200555{
Michal Vaskob68ea142021-04-26 13:46:00 +0200556 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200557 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200558 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200559
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560 /* word buffer - dynamically allocated */
561 *word_b = NULL;
562
563 /* word pointer - just a pointer to data */
564 *word_p = NULL;
565
566 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100567 while (ctx->in->current[0]) {
568 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200569 case '\'':
570 case '\"':
571 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200572 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100573 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200574 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200575 ret = LY_EVALID;
576 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200577 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200578 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100579 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200580 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200581 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200582 goto str_end;
583 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100584 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200585 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100586 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200587 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100588 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200589 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100590 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200591 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200592 } else {
593 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200594 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200595 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200596 break;
597 case ' ':
598 if (*word_len) {
599 /* word is finished */
600 goto str_end;
601 }
Radek Krejci33090f92020-12-17 20:12:46 +0100602 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200603 break;
604 case '\t':
605 if (*word_len) {
606 /* word is finished */
607 goto str_end;
608 }
609 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100610 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200611
Radek Krejci33090f92020-12-17 20:12:46 +0100612 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200613 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200614 case '\r':
615 if (ctx->in->current[1] != '\n') {
616 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200617 ret = LY_EVALID;
618 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200619 }
620 MOVE_INPUT(ctx, 1);
621 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200622 case '\n':
623 if (*word_len) {
624 /* word is finished */
625 goto str_end;
626 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100627 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100628 MOVE_INPUT(ctx, 1);
629
Michal Vasko7fbc8162018-09-17 10:35:16 +0200630 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200631 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200632 break;
633 case ';':
634 case '{':
635 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
636 /* word is finished */
637 goto str_end;
638 }
639
Radek Krejci33090f92020-12-17 20:12:46 +0100640 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200641 ret = LY_EVALID;
642 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200643 case '}':
644 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100645 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200646 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200647 ret = LY_EVALID;
648 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200649 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200650 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200651 break;
652 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200653 }
654
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200655 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200656 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200657 ret = LY_EVALID;
658 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200659
Michal Vasko7fbc8162018-09-17 10:35:16 +0200660str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200661 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200662 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200663 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200664 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200665 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200666 *word_p = *word_b;
667 }
668
669 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200670
671error:
672 free(*word_b);
673 *word_b = NULL;
674 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200675}
676
Michal Vaskoea5abea2018-09-18 13:10:54 +0200677/**
678 * @brief Get another YANG keyword from the raw data.
679 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200680 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200681 * @param[out] kw YANG keyword read.
682 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
683 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
684 *
685 * @return LY_ERR values.
686 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200687LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100688get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200690 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200691 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200692 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200693
694 if (word_p) {
695 *word_p = NULL;
696 *word_len = 0;
697 }
698
699 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100700 while (ctx->in->current[0]) {
701 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200702 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100703 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200704 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100705 MOVE_INPUT(ctx, 2);
706 LY_CHECK_RET(skip_comment(ctx, 1));
707 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200708 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100709 MOVE_INPUT(ctx, 2);
710 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200711 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200712 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200713 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200714 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 }
Radek Krejci13028282019-06-11 14:56:48 +0200716 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200717 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200718 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100719 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200720 ctx->indent = 0;
721 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200722 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200723 /* skip whitespaces (optsep) */
724 ++ctx->indent;
725 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200726 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200727 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100728 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200729 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100730 case '\r':
731 /* possible CRLF endline */
732 if (ctx->in->current[1] == '\n') {
733 break;
734 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100735 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 default:
737 /* either a keyword start or an invalid character */
738 goto keyword_start;
739 }
740
Radek Krejci33090f92020-12-17 20:12:46 +0100741 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200742 }
743
744keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100745 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100746 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200747
Michal Vasko69730152020-10-09 16:30:07 +0200748 if ((*kw == LY_STMT_SYNTAX_SEMICOLON) || (*kw == LY_STMT_SYNTAX_LEFT_BRACE) || (*kw == LY_STMT_SYNTAX_RIGHT_BRACE)) {
Radek Krejci626df482018-10-11 15:06:31 +0200749 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200750 }
751
Radek Krejcid6b76452019-09-03 17:03:03 +0200752 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200753 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100754 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200755 case '\r':
756 if (ctx->in->current[1] != '\n') {
757 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
758 return LY_EVALID;
759 }
760 MOVE_INPUT(ctx, 1);
761 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200762 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200763 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200764 case ' ':
765 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200766 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200767 case ':':
768 /* keyword is not actually a keyword, but prefix of an extension.
769 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
770 * and we will be checking the keyword (extension instance) itself */
771 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100772 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200773 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200774 case '{':
775 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200776 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200777 break;
778 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100779 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200780 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100781 MOVE_INPUT(ctx, 1);
782 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200783 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200784 return LY_EVALID;
785 }
786 } else {
787 /* still can be an extension */
788 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200789
Radek Krejci156ccaf2018-10-15 15:49:17 +0200790extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100791 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200792 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
793 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200794 uint32_t c = 0;
795
Radek Krejci33090f92020-12-17 20:12:46 +0100796 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
797 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200798 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200799 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200800 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100801 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200802 }
Radek Krejci33090f92020-12-17 20:12:46 +0100803 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200804 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200805 return LY_EVALID;
806 }
807
808 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200809 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100810 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200811 return LY_EVALID;
812 }
813
Radek Krejcid6b76452019-09-03 17:03:03 +0200814 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200815 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200816
Radek Krejci626df482018-10-11 15:06:31 +0200817success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818 if (word_p) {
819 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100820 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821 }
822
823 return LY_SUCCESS;
824}
825
Michal Vaskoea5abea2018-09-18 13:10:54 +0200826/**
827 * @brief Parse extension instance substatements.
828 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200829 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200830 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200831 * @param[in] word Extension instance substatement name (keyword).
832 * @param[in] word_len Extension instance substatement name length.
833 * @param[in,out] child Children of this extension instance to add to.
834 *
835 * @return LY_ERR values.
836 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100838parse_ext_substmt(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200839 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200840{
841 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100842 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200843 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200844 struct lysp_stmt *stmt, *par_child;
845
846 stmt = calloc(1, sizeof *stmt);
847 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
848
Radek Krejcibb9b1982019-04-08 14:24:59 +0200849 /* insert into parent statements */
850 if (!*child) {
851 *child = stmt;
852 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200853 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200854 par_child->next = stmt;
855 }
856
Michal Vaskofc2cd072021-02-24 13:17:17 +0100857 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200858 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200859
860 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100861 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200862 if (word) {
863 if (buf) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200864 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200865 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200866 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200867 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200868 }
869
Radek Krejci8df109d2021-04-23 12:19:08 +0200870 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100871 stmt->prefix_data = ctx->parsed_mod;
872 stmt->kw = kw;
873
Radek Krejci33090f92020-12-17 20:12:46 +0100874 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, ) {
875 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200877 return ret;
878}
879
Michal Vaskoea5abea2018-09-18 13:10:54 +0200880/**
881 * @brief Parse extension instance.
882 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200883 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200884 * @param[in] ext_name Extension instance substatement name (keyword).
885 * @param[in] ext_name_len Extension instance substatement name length.
Radek Krejcifc596f92021-02-26 22:40:26 +0100886 * @param[in] insubstmt The statement this extension instance is a substatement of.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200887 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
888 * @param[in,out] exts Extension instances to add to.
889 *
890 * @return LY_ERR values.
891 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100893parse_ext(struct lys_yang_parser_ctx *ctx, const char *ext_name, size_t ext_name_len, enum ly_stmt insubstmt,
Radek Krejci0f969882020-08-21 16:56:47 +0200894 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100896 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200897 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200898 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200899 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200900 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200901
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200902 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200903
Michal Vaskofc2cd072021-02-24 13:17:17 +0100904 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
905 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
906 return LY_EVALID;
907 }
908
909 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200910 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200911
912 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100913 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200914 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200915 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916 }
917
Michal Vaskofc2cd072021-02-24 13:17:17 +0100918 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200919 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200920 e->parsed = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100921 e->prefix_data = ctx->parsed_mod;
Radek Krejciab430862021-03-02 20:13:40 +0100922 e->parent_stmt = insubstmt;
923 e->parent_stmt_index = insubstmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100924
Radek Krejci33090f92020-12-17 20:12:46 +0100925 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
926 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928 return ret;
929}
930
Michal Vaskoea5abea2018-09-18 13:10:54 +0200931/**
932 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
933 * description, etc...
934 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200935 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200936 * @param[in] substmt Type of this substatement.
937 * @param[in] substmt_index Index of this substatement.
938 * @param[in,out] value Place to store the parsed value.
939 * @param[in] arg Type of the YANG keyword argument (of the value).
940 * @param[in,out] exts Extension instances to add to.
941 *
942 * @return LY_ERR values.
943 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200944static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100945parse_text_field(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200946 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200947{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100948 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200949 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200950 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200951 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200952
953 if (*value) {
Radek Krejci3972b332021-03-02 16:34:31 +0100954 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200955 return LY_EVALID;
956 }
957
958 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100959 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960
961 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200962 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200963
Radek Krejci33090f92020-12-17 20:12:46 +0100964 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200965 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200966 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100967 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968 break;
969 default:
Radek Krejci3972b332021-03-02 16:34:31 +0100970 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200971 return LY_EVALID;
972 }
973 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200974 return ret;
975}
976
Michal Vaskoea5abea2018-09-18 13:10:54 +0200977/**
978 * @brief Parse the yang-version statement.
979 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200980 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100981 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200982 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200983 *
984 * @return LY_ERR values.
985 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200986static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100987parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100989 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200991 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200992 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200993
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100994 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200995 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996 return LY_EVALID;
997 }
998
999 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001000 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001
Radek Krejci96e48da2020-09-04 13:18:06 +02001002 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001003 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001004 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001005 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001007 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008 free(buf);
1009 return LY_EVALID;
1010 }
1011 free(buf);
1012
Radek Krejci33090f92020-12-17 20:12:46 +01001013 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001015 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001016 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001017 break;
1018 default:
David Sedlákb3077192019-06-19 10:55:37 +02001019 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001020 return LY_EVALID;
1021 }
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 belongs-to 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] prefix Place to store the parsed belongs-to prefix value.
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_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, 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
Michal Vaskoc3781c32020-10-06 14:04:08 +02001043 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001044 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001045 return LY_EVALID;
1046 }
1047
Michal Vaskoc3781c32020-10-06 14:04:08 +02001048 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001049 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001050 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001051 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 +02001052 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001053 free(buf);
1054 return LY_EVALID;
1055 }
1056 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001057
Radek Krejci33090f92020-12-17 20:12:46 +01001058 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001059 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001060 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001061 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001062 break;
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_BELONGS_TO, 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), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001068 return LY_EVALID;
1069 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001070 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001071 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001072checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073 /* mandatory substatements */
1074 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001075 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001076 return LY_EVALID;
1077 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078 return ret;
1079}
1080
Michal Vaskoea5abea2018-09-18 13:10:54 +02001081/**
1082 * @brief Parse the revision-date statement.
1083 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001084 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001085 * @param[in,out] rev Array to store the parsed value in.
1086 * @param[in,out] exts Extension instances to add to.
1087 *
1088 * @return LY_ERR values.
1089 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001090static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001091parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001092{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001093 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001095 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001096 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097
1098 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001099 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001100 return LY_EVALID;
1101 }
1102
1103 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001104 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001105
1106 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001107 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001108 free(buf);
1109 return LY_EVALID;
1110 }
1111
1112 /* store value and spend buf if allocated */
1113 strncpy(rev, word, word_len);
1114 free(buf);
1115
Radek Krejci33090f92020-12-17 20:12:46 +01001116 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001117 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001118 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001119 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001120 break;
1121 default:
David Sedlákb3077192019-06-19 10:55:37 +02001122 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001123 return LY_EVALID;
1124 }
1125 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126 return ret;
1127}
1128
Michal Vaskoea5abea2018-09-18 13:10:54 +02001129/**
1130 * @brief Parse the include statement.
1131 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001132 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001133 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001134 * @param[in,out] includes Parsed includes to add to.
1135 *
1136 * @return LY_ERR values.
1137 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001138static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001139parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001140{
Radek Krejcid33273d2018-10-25 14:55:52 +02001141 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001143 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001144 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145 struct lysp_include *inc;
1146
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001147 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001148
1149 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001150 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001151
Radek Krejci011e4aa2020-09-04 15:22:31 +02001152 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001153
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001154 /* submodules share the namespace with the module names, so there must not be
1155 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001156 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001157 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001158 return LY_EVALID;
1159 }
1160
Radek Krejci33090f92020-12-17 20:12:46 +01001161 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001162 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001163 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001164 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001165 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 +02001166 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001167 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001168 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001169 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 +02001170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001171 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001172 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001174 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001175 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001176 break;
1177 default:
David Sedlákb3077192019-06-19 10:55:37 +02001178 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001179 return LY_EVALID;
1180 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182 return ret;
1183}
1184
Michal Vaskoea5abea2018-09-18 13:10:54 +02001185/**
1186 * @brief Parse the import statement.
1187 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001188 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001189 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001190 * @param[in,out] imports Parsed imports to add to.
1191 *
1192 * @return LY_ERR values.
1193 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001195parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001197 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001199 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001200 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001201 struct lysp_import *imp;
1202
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001203 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001204
1205 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001206 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001207 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208
Radek Krejci33090f92020-12-17 20:12:46 +01001209 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001210 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001211 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001212 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 +01001213 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 +02001214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001215 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001216 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001217 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 +02001218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001219 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001220 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001221 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 +02001222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001223 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001224 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->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_IMPORT, 0, &imp->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), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231 return LY_EVALID;
1232 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001234 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001235checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001237 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001238
1239 return ret;
1240}
1241
Michal Vaskoea5abea2018-09-18 13:10:54 +02001242/**
1243 * @brief Parse the revision statement.
1244 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001245 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001246 * @param[in,out] revs Parsed revisions to add to.
1247 *
1248 * @return LY_ERR values.
1249 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001250static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001251parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001253 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001255 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001256 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 struct lysp_revision *rev;
1258
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001259 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, 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, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263
1264 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001265 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001266 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001267 return LY_EVALID;
1268 }
1269
Radek Krejcib7db73a2018-10-24 14:18:40 +02001270 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001271 free(buf);
1272
Radek Krejci33090f92020-12-17 20:12:46 +01001273 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001274 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001275 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001276 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 +02001277 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001278 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001279 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 +02001280 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001281 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001282 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001283 break;
1284 default:
David Sedlákb3077192019-06-19 10:55:37 +02001285 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001286 return LY_EVALID;
1287 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001288 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001289 return ret;
1290}
1291
Michal Vaskoea5abea2018-09-18 13:10:54 +02001292/**
1293 * @brief Parse a generic text field that can have more instances such as base.
1294 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001295 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001296 * @param[in] substmt Type of this substatement.
1297 * @param[in,out] texts Parsed values to add to.
1298 * @param[in] arg Type of the expected argument.
1299 * @param[in,out] exts Extension instances to add to.
1300 *
1301 * @return LY_ERR values.
1302 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001304parse_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 +02001305 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001306{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001307 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001308 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001309 const char **item;
1310 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001311 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001312
1313 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001314 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315
1316 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001317 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318
Radek Krejci011e4aa2020-09-04 15:22:31 +02001319 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Radek Krejci33090f92020-12-17 20:12:46 +01001320 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001321 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001322 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001323 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324 break;
1325 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001326 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327 return LY_EVALID;
1328 }
1329 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330 return ret;
1331}
1332
Michal Vaskoea5abea2018-09-18 13:10:54 +02001333/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001334 * @brief Parse a generic text field that can have more instances such as base.
1335 *
1336 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001337 * @param[in] substmt Type of this substatement.
1338 * @param[in,out] qnames Parsed qnames to add to.
1339 * @param[in] arg Type of the expected argument.
1340 * @param[in,out] exts Extension instances to add to.
1341 *
1342 * @return LY_ERR values.
1343 */
1344static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001345parse_qnames(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001346 enum yang_arg arg, struct lysp_ext_instance **exts)
1347{
1348 LY_ERR ret = LY_SUCCESS;
1349 char *buf, *word;
1350 struct lysp_qname *item;
1351 size_t word_len;
1352 enum ly_stmt kw;
1353
1354 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001355 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001356
1357 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001358 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001359
1360 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001361 item->mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001362 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001363 switch (kw) {
1364 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001365 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001366 break;
1367 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001368 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001369 return LY_EVALID;
1370 }
1371 }
1372 return ret;
1373}
1374
1375/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001376 * @brief Parse the config statement.
1377 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001378 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001379 * @param[in,out] flags Flags to add to.
1380 * @param[in,out] exts Extension instances to add to.
1381 *
1382 * @return LY_ERR values.
1383 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001384static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001385parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001387 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001389 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001390 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001391
1392 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001393 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001394 return LY_EVALID;
1395 }
1396
1397 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001398 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001399
Radek Krejcif13b87b2020-12-01 22:02:17 +01001400 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001402 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403 *flags |= LYS_CONFIG_R;
1404 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001405 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001406 free(buf);
1407 return LY_EVALID;
1408 }
1409 free(buf);
1410
Radek Krejci33090f92020-12-17 20:12:46 +01001411 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001412 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001413 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001414 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415 break;
1416 default:
David Sedlákb3077192019-06-19 10:55:37 +02001417 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001418 return LY_EVALID;
1419 }
1420 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001421 return ret;
1422}
1423
Michal Vaskoea5abea2018-09-18 13:10:54 +02001424/**
1425 * @brief Parse the mandatory statement.
1426 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001427 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001428 * @param[in,out] flags Flags to add to.
1429 * @param[in,out] exts Extension instances to add to.
1430 *
1431 * @return LY_ERR values.
1432 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001433static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001434parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001436 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001438 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001439 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440
1441 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001442 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443 return LY_EVALID;
1444 }
1445
1446 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001447 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448
Radek Krejcif13b87b2020-12-01 22:02:17 +01001449 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001451 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001452 *flags |= LYS_MAND_FALSE;
1453 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001454 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001455 free(buf);
1456 return LY_EVALID;
1457 }
1458 free(buf);
1459
Radek Krejci33090f92020-12-17 20:12:46 +01001460 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001462 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001463 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001464 break;
1465 default:
David Sedlákb3077192019-06-19 10:55:37 +02001466 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001467 return LY_EVALID;
1468 }
1469 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001470 return ret;
1471}
1472
Michal Vaskoea5abea2018-09-18 13:10:54 +02001473/**
1474 * @brief Parse a restriction such as range or length.
1475 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001476 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001477 * @param[in] restr_kw Type of this particular restriction.
1478 * @param[in,out] exts Extension instances to add to.
1479 *
1480 * @return LY_ERR values.
1481 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001482static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001483parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001485 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001487 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001488 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489
1490 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001491 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001492
Michal Vaskob36053d2020-03-26 15:49:30 +01001493 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001494 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001495 restr->arg.mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001496 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001497 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001498 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001499 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 +02001500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001501 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001502 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 +02001503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001504 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01001505 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 +02001506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001507 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001508 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 +02001509 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001510 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001511 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001512 break;
1513 default:
David Sedlákb3077192019-06-19 10:55:37 +02001514 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515 return LY_EVALID;
1516 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001518 return ret;
1519}
1520
Michal Vaskoea5abea2018-09-18 13:10:54 +02001521/**
1522 * @brief Parse a restriction that can have more instances such as must.
1523 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001524 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001525 * @param[in] restr_kw Type of this particular restriction.
1526 * @param[in,out] restrs Restrictions to add to.
1527 *
1528 * @return LY_ERR values.
1529 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001530static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001531parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001532{
1533 struct lysp_restr *restr;
1534
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001535 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001536 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537}
1538
Michal Vaskoea5abea2018-09-18 13:10:54 +02001539/**
1540 * @brief Parse the status statement.
1541 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001542 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001543 * @param[in,out] flags Flags to add to.
1544 * @param[in,out] exts Extension instances to add to.
1545 *
1546 * @return LY_ERR values.
1547 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001548static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001549parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001551 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001553 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001554 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555
1556 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001557 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001558 return LY_EVALID;
1559 }
1560
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 Vasko7fbc8162018-09-17 10:35:16 +02001563
Radek Krejcif13b87b2020-12-01 22:02:17 +01001564 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001566 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001568 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569 *flags |= LYS_STATUS_OBSLT;
1570 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001571 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572 free(buf);
1573 return LY_EVALID;
1574 }
1575 free(buf);
1576
Radek Krejci33090f92020-12-17 20:12:46 +01001577 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001578 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001579 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001580 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001581 break;
1582 default:
David Sedlákb3077192019-06-19 10:55:37 +02001583 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584 return LY_EVALID;
1585 }
1586 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001587 return ret;
1588}
1589
Michal Vaskoea5abea2018-09-18 13:10:54 +02001590/**
1591 * @brief Parse the when statement.
1592 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001593 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001594 * @param[in,out] when_p When pointer to parse to.
1595 *
1596 * @return LY_ERR values.
1597 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001598LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001599parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001601 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001602 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001603 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001604 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001605 struct lysp_when *when;
1606
1607 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001608 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001609 return LY_EVALID;
1610 }
1611
1612 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001613 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001614 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615
1616 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001617 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001618 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001619 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001620
Radek Krejci33090f92020-12-17 20:12:46 +01001621 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001623 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001624 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 +02001625 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001626 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001627 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 +02001628 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001629 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001630 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_WHEN, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631 break;
1632 default:
David Sedlákb3077192019-06-19 10:55:37 +02001633 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 return LY_EVALID;
1635 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001637 return ret;
1638}
1639
Michal Vaskoea5abea2018-09-18 13:10:54 +02001640/**
1641 * @brief Parse the anydata or anyxml statement.
1642 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001643 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001644 * @param[in] any_kw Type of this particular keyword.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001645 * @param[in,out] siblings Siblings to add to.
1646 *
1647 * @return LY_ERR values.
1648 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001649LY_ERR
Radek Krejci39b7fc22021-02-26 23:29:18 +01001650parse_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 +02001651{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001652 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001654 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001655 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001656 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001657
David Sedlák60adc092019-08-06 15:57:02 +02001658 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001659 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001660
Radek Krejci39b7fc22021-02-26 23:29:18 +01001661 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001662 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001665 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001666 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667
1668 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01001669 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001670 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001671 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001672 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001674 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001675 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 +02001676 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001677 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001678 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001680 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001681 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001683 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001684 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001685 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001686 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001687 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 +02001688 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001689 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001690 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001691 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001692 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001693 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001695 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001696 LY_CHECK_RET(parse_ext(ctx, word, word_len, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697 break;
1698 default:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001699 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001700 return LY_EVALID;
1701 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001702 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001703 return ret;
1704}
1705
Michal Vaskoea5abea2018-09-18 13:10:54 +02001706/**
1707 * @brief Parse the value or position statement. Substatement of type enum statement.
1708 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001709 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001710 * @param[in] val_kw Type of this particular keyword.
1711 * @param[in,out] value Value to write to.
1712 * @param[in,out] flags Flags to write to.
1713 * @param[in,out] exts Extension instances to add to.
1714 *
1715 * @return LY_ERR values.
1716 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001717LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001718parse_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 +02001719 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001720{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001721 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001722 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001723 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001724 long int num = 0;
1725 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001726 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001727
1728 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001729 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001730 return LY_EVALID;
1731 }
1732 *flags |= LYS_SET_VALUE;
1733
1734 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001735 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736
Radek Krejcid6b76452019-09-03 17:03:03 +02001737 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 +02001738 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001739 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740 }
1741
1742 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001743 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001744 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001745 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001746 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001747 goto error;
1748 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001749 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001750 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001751 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001752 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001753 goto error;
1754 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001755 }
1756 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001757 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001758 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001759 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001760 }
1761 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001762 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001763 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001764 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001765 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766 *value = num;
1767 } else {
1768 *value = unum;
1769 }
1770 free(buf);
1771
Radek Krejci33090f92020-12-17 20:12:46 +01001772 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001773 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001774 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001775 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 +02001776 break;
1777 default:
David Sedlákb3077192019-06-19 10:55:37 +02001778 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779 return LY_EVALID;
1780 }
1781 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001782 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001783
1784error:
1785 free(buf);
1786 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001787}
1788
Michal Vaskoea5abea2018-09-18 13:10:54 +02001789/**
1790 * @brief Parse the enum or bit statement. Substatement of type statement.
1791 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001792 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001793 * @param[in] enum_kw Type of this particular keyword.
1794 * @param[in,out] enums Enums or bits to add to.
1795 *
1796 * @return LY_ERR values.
1797 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001798static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001799parse_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 +02001800{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001801 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001802 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001803 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001804 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001805 struct lysp_type_enum *enm;
1806
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001807 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001808
1809 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001810 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 +02001811 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001812 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001813 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001814 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001815
Radek Krejci011e4aa2020-09-04 15:22:31 +02001816 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001817 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1818
Radek Krejci33090f92020-12-17 20:12:46 +01001819 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001820 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001821 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001822 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 +02001823 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001824 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001825 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001826 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001827 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001828 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001829 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 +02001830 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001831 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001832 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001833 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001834 case LY_STMT_VALUE:
1835 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 +02001836 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001837 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001838 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001839 case LY_STMT_POSITION:
1840 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 +02001841 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001842 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001844 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001845 LY_CHECK_RET(parse_ext(ctx, word, word_len, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846 break;
1847 default:
David Sedlákb3077192019-06-19 10:55:37 +02001848 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001849 return LY_EVALID;
1850 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001851 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001852 return ret;
1853}
1854
Michal Vaskoea5abea2018-09-18 13:10:54 +02001855/**
1856 * @brief Parse the fraction-digits statement. Substatement of type statement.
1857 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001858 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001859 * @param[in,out] fracdig Value to write to.
1860 * @param[in,out] exts Extension instances to add to.
1861 *
1862 * @return LY_ERR values.
1863 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001864static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001865parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001866{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001867 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001869 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001870 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001871 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001872
1873 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001874 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001875 return LY_EVALID;
1876 }
1877
1878 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001879 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001880
1881 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001882 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001883 free(buf);
1884 return LY_EVALID;
1885 }
1886
1887 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001888 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001889 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001890 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001891 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001892 free(buf);
1893 return LY_EVALID;
1894 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001895 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001896 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897 free(buf);
1898 return LY_EVALID;
1899 }
1900 *fracdig = num;
1901 free(buf);
1902
Radek Krejci33090f92020-12-17 20:12:46 +01001903 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001905 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001906 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FRACTION_DIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001907 break;
1908 default:
David Sedlákb3077192019-06-19 10:55:37 +02001909 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910 return LY_EVALID;
1911 }
1912 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001913 return ret;
1914}
1915
Michal Vaskoea5abea2018-09-18 13:10:54 +02001916/**
1917 * @brief Parse the require-instance statement. Substatement of type statement.
1918 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001919 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001920 * @param[in,out] reqinst Value to write to.
1921 * @param[in,out] flags Flags to write to.
1922 * @param[in,out] exts Extension instances to add to.
1923 *
1924 * @return LY_ERR values.
1925 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001927parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001928 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001930 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001932 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001933 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934
1935 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001936 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001937 return LY_EVALID;
1938 }
1939 *flags |= LYS_SET_REQINST;
1940
1941 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001942 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001943
Radek Krejcif13b87b2020-12-01 22:02:17 +01001944 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001945 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001946 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001947 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001948 free(buf);
1949 return LY_EVALID;
1950 }
1951 free(buf);
1952
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_REQUIRE_INSTANCE, 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), "require-instance");
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 modifier statement. Substatement of type pattern 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] pat Value to write to.
1971 * @param[in,out] exts Extension instances to add to.
1972 *
1973 * @return LY_ERR values.
1974 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001976parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001978 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001980 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001981 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001982
Radek Krejcif13b87b2020-12-01 22:02:17 +01001983 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001984 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001985 return LY_EVALID;
1986 }
1987
1988 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001989 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990
Radek Krejcif13b87b2020-12-01 22:02:17 +01001991 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001992 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001993 free(buf);
1994 return LY_EVALID;
1995 }
1996 free(buf);
1997
1998 /* replace the value in the dictionary */
1999 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002000 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002002 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003
Radek Krejcif13b87b2020-12-01 22:02:17 +01002004 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2005 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002006 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002007
Radek Krejci33090f92020-12-17 20:12:46 +01002008 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002009 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002010 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002011 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002012 break;
2013 default:
David Sedlákb3077192019-06-19 10:55:37 +02002014 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002015 return LY_EVALID;
2016 }
2017 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018 return ret;
2019}
2020
Michal Vaskoea5abea2018-09-18 13:10:54 +02002021/**
2022 * @brief Parse the pattern statement. Substatement of type statement.
2023 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002024 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002025 * @param[in,out] patterns Restrictions to add to.
2026 *
2027 * @return LY_ERR values.
2028 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002030parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002031{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002032 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002034 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002035 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002036 struct lysp_restr *restr;
2037
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002038 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039
2040 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002041 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042
2043 /* add special meaning first byte */
2044 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002045 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046 word = buf;
2047 } else {
2048 buf = malloc(word_len + 2);
2049 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002050 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002051 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002052 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002053 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002054 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
2055 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056
Radek Krejci33090f92020-12-17 20:12:46 +01002057 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002059 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002060 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 +02002061 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002062 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002063 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 +02002064 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002065 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01002066 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 +02002067 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002068 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002069 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 +02002070 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002071 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002072 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002073 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002075 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002076 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002077 break;
2078 default:
David Sedlákb3077192019-06-19 10:55:37 +02002079 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080 return LY_EVALID;
2081 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083 return ret;
2084}
2085
Michal Vaskoea5abea2018-09-18 13:10:54 +02002086/**
2087 * @brief Parse the type statement.
2088 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002089 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002090 * @param[in,out] type Type to wrote to.
2091 *
2092 * @return LY_ERR values.
2093 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002095parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002097 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002099 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002100 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002101 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 struct lysp_type *nest_type;
2103
2104 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002105 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106 return LY_EVALID;
2107 }
2108
2109 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002110 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002111 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002113 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002114 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002115
Radek Krejci33090f92020-12-17 20:12:46 +01002116 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002118 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002119 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 +01002120 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002122 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002123 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002124 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002126 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002127 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002128 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002130 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002131 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002132 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002134 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002136 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002137 return LY_EVALID;
2138 }
2139 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002140 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141
Radek Krejci33090f92020-12-17 20:12:46 +01002142 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002143 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002144 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002145 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002146 if (type->path) {
Radek Krejci3972b332021-03-02 16:34:31 +01002147 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002148 return LY_EVALID;
2149 }
2150
Radek Krejcifc596f92021-02-26 22:40:26 +01002151 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 +02002152 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 +02002153 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002154 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002155 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002156 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002158 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002159 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002160 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002162 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002164 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165 return LY_EVALID;
2166 }
2167 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002168 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169
Radek Krejci33090f92020-12-17 20:12:46 +01002170 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002171 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002173 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002174 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002175 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002177 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002178 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002179 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002180 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002182 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002183 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002184 break;
2185 default:
David Sedlákb3077192019-06-19 10:55:37 +02002186 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187 return LY_EVALID;
2188 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002190 return ret;
2191}
2192
Michal Vaskoea5abea2018-09-18 13:10:54 +02002193/**
2194 * @brief Parse the leaf statement.
2195 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002196 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002197 * @param[in,out] siblings Siblings to add to.
2198 *
2199 * @return LY_ERR values.
2200 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002201LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002202parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002203{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002204 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002205 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002206 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002207 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002208 struct lysp_node_leaf *leaf;
2209
David Sedlák60adc092019-08-06 15:57:02 +02002210 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002211 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002213 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214
Michal Vasko7fbc8162018-09-17 10:35:16 +02002215 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002216 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002217 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002218
2219 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002220 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002221 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002222 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002223 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002225 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002226 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 +02002227 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002229 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002230 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 +02002231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002232 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002233 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002235 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002236 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002238 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002239 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002241 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002242 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 +02002243 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002244 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002245 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002246 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002247 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002248 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002249 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002250 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002251 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 +02002252 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002253 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002254 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002256 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002257 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258 break;
2259 default:
David Sedlákb3077192019-06-19 10:55:37 +02002260 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002261 return LY_EVALID;
2262 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002264 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002265checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002266 /* mandatory substatements */
2267 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002268 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269 return LY_EVALID;
2270 }
2271
2272 return ret;
2273}
2274
Michal Vaskoea5abea2018-09-18 13:10:54 +02002275/**
2276 * @brief Parse the max-elements statement.
2277 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002278 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002279 * @param[in,out] max Value to write to.
2280 * @param[in,out] flags Flags to write to.
2281 * @param[in,out] exts Extension instances to add to.
2282 *
2283 * @return LY_ERR values.
2284 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002285LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002286parse_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 +02002287{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002288 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002289 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002290 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002291 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002292 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293
2294 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002295 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002296 return LY_EVALID;
2297 }
2298 *flags |= LYS_SET_MAX;
2299
2300 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002301 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302
2303 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002304 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002305 free(buf);
2306 return LY_EVALID;
2307 }
2308
Radek Krejci7f9b6512019-09-18 13:11:09 +02002309 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002311 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002313 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002314 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002315 free(buf);
2316 return LY_EVALID;
2317 }
2318 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002319 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002320 free(buf);
2321 return LY_EVALID;
2322 }
2323
2324 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002325 } else {
2326 /* unbounded */
2327 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 }
2329 free(buf);
2330
Radek Krejci33090f92020-12-17 20:12:46 +01002331 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002332 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002333 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002334 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MAX_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002335 break;
2336 default:
David Sedlákb3077192019-06-19 10:55:37 +02002337 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002338 return LY_EVALID;
2339 }
2340 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002341 return ret;
2342}
2343
Michal Vaskoea5abea2018-09-18 13:10:54 +02002344/**
2345 * @brief Parse the min-elements statement.
2346 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002347 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002348 * @param[in,out] min Value to write to.
2349 * @param[in,out] flags Flags to write to.
2350 * @param[in,out] exts Extension instances to add to.
2351 *
2352 * @return LY_ERR values.
2353 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002354LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002355parse_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 +02002356{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002357 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002358 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002359 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002361 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362
2363 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002364 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365 return LY_EVALID;
2366 }
2367 *flags |= LYS_SET_MIN;
2368
2369 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002370 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002371
2372 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002373 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002374 free(buf);
2375 return LY_EVALID;
2376 }
2377
2378 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002379 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002380 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002381 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002382 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383 free(buf);
2384 return LY_EVALID;
2385 }
2386 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002387 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388 free(buf);
2389 return LY_EVALID;
2390 }
2391 *min = num;
2392 free(buf);
2393
Radek Krejci33090f92020-12-17 20:12:46 +01002394 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002395 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002396 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002397 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MIN_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002398 break;
2399 default:
David Sedlákb3077192019-06-19 10:55:37 +02002400 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002401 return LY_EVALID;
2402 }
2403 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002404 return ret;
2405}
2406
Michal Vaskoea5abea2018-09-18 13:10:54 +02002407/**
2408 * @brief Parse the ordered-by statement.
2409 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002410 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002411 * @param[in,out] flags Flags to write to.
2412 * @param[in,out] exts Extension instances to add to.
2413 *
2414 * @return LY_ERR values.
2415 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002417parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002419 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002421 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002422 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423
2424 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002425 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426 return LY_EVALID;
2427 }
2428
2429 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002430 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002431
Radek Krejcif13b87b2020-12-01 22:02:17 +01002432 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002433 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002434 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435 *flags |= LYS_ORDBY_USER;
2436 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002437 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438 free(buf);
2439 return LY_EVALID;
2440 }
2441 free(buf);
2442
Radek Krejci33090f92020-12-17 20:12:46 +01002443 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002444 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002445 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002446 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ORDERED_BY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002447 break;
2448 default:
David Sedlákb3077192019-06-19 10:55:37 +02002449 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002450 return LY_EVALID;
2451 }
2452 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002453 return ret;
2454}
2455
Michal Vaskoea5abea2018-09-18 13:10:54 +02002456/**
2457 * @brief Parse the leaf-list statement.
2458 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002459 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002460 * @param[in,out] siblings Siblings to add to.
2461 *
2462 * @return LY_ERR values.
2463 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002464LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002465parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002466{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002467 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002468 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002469 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002470 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002471 struct lysp_node_leaflist *llist;
2472
David Sedlák60adc092019-08-06 15:57:02 +02002473 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002474 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002476 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002479 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002480 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002481
2482 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002483 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002484 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002485 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002486 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002488 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002489 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002490 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002491 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002492 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002493 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 +02002494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002495 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002496 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002498 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002499 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002501 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002502 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002504 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002505 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002507 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002508 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002509 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002510 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002511 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 +02002512 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002513 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002514 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002515 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002516 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002517 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002519 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002520 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 +02002521 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002522 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002523 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002524 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002525 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002526 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527 break;
2528 default:
David Sedlákb3077192019-06-19 10:55:37 +02002529 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002530 return LY_EVALID;
2531 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002532 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002533 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002534checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002535 /* mandatory substatements */
2536 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002537 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538 return LY_EVALID;
2539 }
2540
2541 return ret;
2542}
2543
Michal Vaskoea5abea2018-09-18 13:10:54 +02002544/**
2545 * @brief Parse the refine statement.
2546 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002547 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002548 * @param[in,out] refines Refines to add to.
2549 *
2550 * @return LY_ERR values.
2551 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002553parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002555 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002557 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002558 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002559 struct lysp_refine *rf;
2560
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002561 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002562
2563 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002564 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002565 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002566 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567
Radek Krejci33090f92020-12-17 20:12:46 +01002568 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002569 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002570 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002571 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002573 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002574 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002576 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002577 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 +02002578 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002579 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002580 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002581 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002582 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002583 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002584 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002585 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002586 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002587 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002588 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002589 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002590 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002591 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002592 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002593 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002595 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002596 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 +02002597 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002598 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002599 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 +02002600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002601 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002602 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 break;
2604 default:
David Sedlákb3077192019-06-19 10:55:37 +02002605 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 return LY_EVALID;
2607 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 return ret;
2610}
2611
Michal Vaskoea5abea2018-09-18 13:10:54 +02002612/**
2613 * @brief Parse the typedef statement.
2614 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002615 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002616 * @param[in,out] typedefs Typedefs to add to.
2617 *
2618 * @return LY_ERR values.
2619 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002621parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002623 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002625 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002626 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627 struct lysp_tpdf *tpdf;
2628
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002629 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630
2631 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002632 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002633 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002634
2635 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002636 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002637 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002638 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002639 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 +02002640 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002642 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002643 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 +02002644 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002645 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002646 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 +02002647 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002648 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002649 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002651 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002652 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002654 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002655 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 +02002656 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002657 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002658 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002659 break;
2660 default:
David Sedlákb3077192019-06-19 10:55:37 +02002661 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662 return LY_EVALID;
2663 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002664 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002665 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002666checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667 /* mandatory substatements */
2668 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002669 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002670 return LY_EVALID;
2671 }
2672
Radek Krejcibbe09a92018-11-08 09:36:54 +01002673 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002674 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002675 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002676 }
2677
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 return ret;
2679}
2680
Michal Vaskoea5abea2018-09-18 13:10:54 +02002681/**
2682 * @brief Parse the input or output statement.
2683 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002684 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002685 * @param[in] kw Type of this particular keyword
2686 * @param[in,out] inout_p Input/output pointer to write to.
2687 *
2688 * @return LY_ERR values.
2689 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002690static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002691parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002692 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002693{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002694 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002696 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002697 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002698 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002699
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002700 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002701 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702 return LY_EVALID;
2703 }
2704
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002705 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002706 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2707 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002708 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002709
2710 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002711 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002713 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002714 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002715 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002716 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002717 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002719 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002720 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002722 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002723 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002724 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002725 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002726 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002728 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002729 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002730 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002731 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002732 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002734 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002735 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002737 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002738 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002740 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002741 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002742 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002743 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002744 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002745 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002746 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002747 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002748 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749 break;
2750 default:
David Sedlákb3077192019-06-19 10:55:37 +02002751 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 return LY_EVALID;
2753 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002754 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002755 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002756
Radek Krejci7fc68292019-06-12 13:51:09 +02002757checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002758 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002759 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2760 return LY_EVALID;
2761 }
2762
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763 return ret;
2764}
2765
Michal Vaskoea5abea2018-09-18 13:10:54 +02002766/**
2767 * @brief Parse the action statement.
2768 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002769 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002770 * @param[in,out] actions Actions to add to.
2771 *
2772 * @return LY_ERR values.
2773 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002774LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002775parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002777 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002779 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002780 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002781 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002782
Radek Krejci2a9fc652021-01-22 17:44:34 +01002783 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784
2785 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002786 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002787 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002788 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002789 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002790
Radek Krejci33090f92020-12-17 20:12:46 +01002791 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002793 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002794 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 +02002795 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002796 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002797 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002799 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002800 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 +02002801 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002802 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002803 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002804 break;
2805
Radek Krejcid6b76452019-09-03 17:03:03 +02002806 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002807 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002809 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002810 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811 break;
2812
Radek Krejcid6b76452019-09-03 17:03:03 +02002813 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002814 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002815 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002816 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002817 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002819 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002820 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 +02002821 break;
2822 default:
David Sedlákb3077192019-06-19 10:55:37 +02002823 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002824 return LY_EVALID;
2825 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002827 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002828
Radek Krejci2a9fc652021-01-22 17:44:34 +01002829checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002830 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2831 if (!act->input.nodetype) {
2832 act->input.nodetype = LYS_INPUT;
2833 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002834 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002835 }
2836 if (!act->output.nodetype) {
2837 act->output.nodetype = LYS_OUTPUT;
2838 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002839 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002840 }
2841
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 return ret;
2843}
2844
Michal Vaskoea5abea2018-09-18 13:10:54 +02002845/**
2846 * @brief Parse the notification statement.
2847 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002848 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002849 * @param[in,out] notifs Notifications to add to.
2850 *
2851 * @return LY_ERR values.
2852 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002853LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002854parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002856 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002858 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002859 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002860 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861
Radek Krejci2a9fc652021-01-22 17:44:34 +01002862 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002863
2864 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002865 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002866 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002867 notif->nodetype = LYS_NOTIF;
2868 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869
Radek Krejci2a9fc652021-01-22 17:44:34 +01002870 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002871 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002872 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002873 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 +02002874 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002875 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002876 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002877 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002878 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002879 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 +02002880 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002881 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002882 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 break;
2884
Radek Krejcid6b76452019-09-03 17:03:03 +02002885 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002886 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002887 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002888 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002889 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002891 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002892 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002893 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002894 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002895 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002896 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002897 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002898 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002899 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002900 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002901 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002902 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002903 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002904 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002906 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002907 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002908 break;
2909
Radek Krejcid6b76452019-09-03 17:03:03 +02002910 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002911 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002912 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002914 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002915 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002917 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002918 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002920 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002921 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002922 break;
2923 default:
David Sedlákb3077192019-06-19 10:55:37 +02002924 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 return LY_EVALID;
2926 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002927 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002928
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929 return ret;
2930}
2931
Michal Vaskoea5abea2018-09-18 13:10:54 +02002932/**
2933 * @brief Parse the grouping statement.
2934 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002935 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002936 * @param[in,out] groupings Groupings to add to.
2937 *
2938 * @return LY_ERR values.
2939 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002940LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002941parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002942{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002943 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002945 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002946 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002947 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948
Radek Krejci2a9fc652021-01-22 17:44:34 +01002949 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002950
2951 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002952 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002953 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002954 grp->nodetype = LYS_GROUPING;
2955 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002956
Radek Krejci2a9fc652021-01-22 17:44:34 +01002957 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002959 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002960 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 +02002961 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002962 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002963 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 +02002964 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002965 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002966 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967 break;
2968
Radek Krejcid6b76452019-09-03 17:03:03 +02002969 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002970 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002971 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002972 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002973 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002975 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002976 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002978 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002979 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002980 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002981 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002982 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002984 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002985 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002986 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002987 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002988 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002989 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002990 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002991 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992 break;
2993
Radek Krejcid6b76452019-09-03 17:03:03 +02002994 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002995 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002996 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002997 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002998 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002999 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003001 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003002 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003004 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003005 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003006 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003008 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003009 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010 break;
3011 default:
David Sedlákb3077192019-06-19 10:55:37 +02003012 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 return LY_EVALID;
3014 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003016
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 return ret;
3018}
3019
Michal Vaskoea5abea2018-09-18 13:10:54 +02003020/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003021 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003022 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003023 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003024 * @param[in,out] augments Augments to add to.
3025 *
3026 * @return LY_ERR values.
3027 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003028LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003029parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003031 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003032 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003033 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003034 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003035 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003036
Radek Krejci2a9fc652021-01-22 17:44:34 +01003037 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003038
3039 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003040 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003041 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003042 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003043 aug->nodetype = LYS_AUGMENT;
3044 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045
Radek Krejci2a9fc652021-01-22 17:44:34 +01003046 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003048 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003049 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 +02003050 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003051 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003052 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003054 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003055 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 +02003056 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003057 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003058 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003059 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003060 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003061 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 break;
3063
Radek Krejcid6b76452019-09-03 17:03:03 +02003064 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003065 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003066 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003067 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003068 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003070 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003071 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003073 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003074 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003076 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003077 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003079 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003080 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003082 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003083 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003085 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003086 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003088 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003089 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003090 break;
3091
Radek Krejcid6b76452019-09-03 17:03:03 +02003092 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003093 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003094 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003096 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003097 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003098 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003100 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003101 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 break;
3103 default:
David Sedlákb3077192019-06-19 10:55:37 +02003104 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105 return LY_EVALID;
3106 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003108
Michal Vasko7fbc8162018-09-17 10:35:16 +02003109 return ret;
3110}
3111
Michal Vaskoea5abea2018-09-18 13:10:54 +02003112/**
3113 * @brief Parse the uses statement.
3114 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003115 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003116 * @param[in,out] siblings Siblings to add to.
3117 *
3118 * @return LY_ERR values.
3119 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003120LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003121parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003123 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003124 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003125 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003126 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003127 struct lysp_node_uses *uses;
3128
David Sedlák60adc092019-08-06 15:57:02 +02003129 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003130 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003131 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003132 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133
Michal Vasko7fbc8162018-09-17 10:35:16 +02003134 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003135 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003136 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137
3138 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003139 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003141 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003142 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 +02003143 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003144 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003145 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003147 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003148 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 +02003149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003150 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003151 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003153 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003154 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003155 break;
3156
Radek Krejcid6b76452019-09-03 17:03:03 +02003157 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003158 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003160 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003161 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003163 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003164 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 break;
3166 default:
David Sedlákb3077192019-06-19 10:55:37 +02003167 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003168 return LY_EVALID;
3169 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003171
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 return ret;
3173}
3174
Michal Vaskoea5abea2018-09-18 13:10:54 +02003175/**
3176 * @brief Parse the case statement.
3177 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003178 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003179 * @param[in,out] siblings Siblings to add to.
3180 *
3181 * @return LY_ERR values.
3182 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003183LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003184parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003186 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003187 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003188 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003189 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003190 struct lysp_node_case *cas;
3191
David Sedlák60adc092019-08-06 15:57:02 +02003192 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003193 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003195 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196
Michal Vasko7fbc8162018-09-17 10:35:16 +02003197 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003198 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003199 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003200
3201 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003202 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003204 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003205 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 +02003206 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003207 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003208 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003209 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003210 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003211 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 +02003212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003213 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003214 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003216 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003217 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003218 break;
3219
Radek Krejcid6b76452019-09-03 17:03:03 +02003220 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003221 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003222 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003223 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003224 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003226 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003227 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003229 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003230 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003232 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003233 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003234 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003235 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003236 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003238 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003239 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003241 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003242 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003244 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003245 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246 break;
3247 default:
David Sedlákb3077192019-06-19 10:55:37 +02003248 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249 return LY_EVALID;
3250 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252 return ret;
3253}
3254
Michal Vaskoea5abea2018-09-18 13:10:54 +02003255/**
3256 * @brief Parse the choice statement.
3257 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003258 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003259 * @param[in,out] siblings Siblings to add to.
3260 *
3261 * @return LY_ERR values.
3262 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003263LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003264parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003265{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003266 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003268 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003269 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003270 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271
David Sedlák60adc092019-08-06 15:57:02 +02003272 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003273 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003274 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003275 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003278 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003279 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280
3281 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003282 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003284 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003285 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003287 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003288 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 +02003289 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003290 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003291 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003293 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003294 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003296 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003297 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 +02003298 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003299 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003300 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003302 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003303 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003305 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01003306 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 +02003307 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003308 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003309 break;
3310
Radek Krejcid6b76452019-09-03 17:03:03 +02003311 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003312 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003313 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003314 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003315 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003317 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003318 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003320 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003321 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003322 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003324 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003325 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003328 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003331 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003333 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003334 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003336 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003337 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338 break;
3339 default:
David Sedlákb3077192019-06-19 10:55:37 +02003340 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341 return LY_EVALID;
3342 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 return ret;
3345}
3346
Michal Vaskoea5abea2018-09-18 13:10:54 +02003347/**
3348 * @brief Parse the container statement.
3349 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003350 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003351 * @param[in,out] siblings Siblings to add to.
3352 *
3353 * @return LY_ERR values.
3354 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003355LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003356parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357{
3358 LY_ERR ret = 0;
3359 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003360 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003361 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 struct lysp_node_container *cont;
3363
David Sedlák60adc092019-08-06 15:57:02 +02003364 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003365 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003367 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003370 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003371 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372
3373 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003374 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003375 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003376 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003377 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003379 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003380 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 +02003381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003382 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003383 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003385 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003386 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 +02003387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003388 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003389 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003391 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003392 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003394 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003395 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 +02003396 break;
3397
Radek Krejcid6b76452019-09-03 17:03:03 +02003398 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003399 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003400 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003401 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003402 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003404 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003405 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003407 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003408 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003410 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003411 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003414 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003416 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003417 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003419 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003420 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 break;
3422
Radek Krejcid6b76452019-09-03 17:03:03 +02003423 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003424 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003425 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003426 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003427 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003429 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003430 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003431 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003433 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003434 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003436 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003437 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003438 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003440 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003441 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442 break;
3443 default:
David Sedlákb3077192019-06-19 10:55:37 +02003444 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 return LY_EVALID;
3446 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003448
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 return ret;
3450}
3451
Michal Vaskoea5abea2018-09-18 13:10:54 +02003452/**
3453 * @brief Parse the list statement.
3454 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003455 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003456 * @param[in,out] siblings Siblings to add to.
3457 *
3458 * @return LY_ERR values.
3459 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003460LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003461parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003463 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003465 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003466 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 struct lysp_node_list *list;
3468
David Sedlák60adc092019-08-06 15:57:02 +02003469 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003470 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003472 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473
Michal Vasko7fbc8162018-09-17 10:35:16 +02003474 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003475 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003476 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003477
3478 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003479 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003481 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003482 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003484 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003485 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 +02003486 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003487 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003488 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003490 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003491 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 +02003492 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003493 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003494 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003495 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003496 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003497 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003498 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003499 case LY_STMT_KEY:
Radek Krejcifc596f92021-02-26 22:40:26 +01003500 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 +02003501 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003502 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003503 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003504 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003505 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003506 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003508 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003509 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003510 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003511 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003512 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513 break;
3514
Radek Krejcid6b76452019-09-03 17:03:03 +02003515 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003516 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003517 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003518 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003519 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003521 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003522 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003524 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003525 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003527 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003528 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003530 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003531 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003533 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003534 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003536 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003537 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538 break;
3539
Radek Krejcid6b76452019-09-03 17:03:03 +02003540 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003541 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003543 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003544 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003546 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003547 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003548 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003549 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003550 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003551 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003552 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003553 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003554 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003555 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003557 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003558 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003559 break;
3560 default:
David Sedlákb3077192019-06-19 10:55:37 +02003561 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003562 return LY_EVALID;
3563 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003565
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 return ret;
3567}
3568
Michal Vaskoea5abea2018-09-18 13:10:54 +02003569/**
3570 * @brief Parse the yin-element statement.
3571 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003572 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003573 * @param[in,out] flags Flags to write to.
3574 * @param[in,out] exts Extension instances to add to.
3575 *
3576 * @return LY_ERR values.
3577 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003579parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003581 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003583 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003584 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003585
3586 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003587 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588 return LY_EVALID;
3589 }
3590
3591 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003592 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593
Radek Krejcif13b87b2020-12-01 22:02:17 +01003594 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003596 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 *flags |= LYS_YINELEM_FALSE;
3598 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003599 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 free(buf);
3601 return LY_EVALID;
3602 }
3603 free(buf);
3604
Radek Krejci33090f92020-12-17 20:12:46 +01003605 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003606 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003607 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003608 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003609 LY_CHECK_RET(ret);
3610 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003611 default:
David Sedlákb3077192019-06-19 10:55:37 +02003612 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613 return LY_EVALID;
3614 }
3615 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003616 return ret;
3617}
3618
Michal Vaskoea5abea2018-09-18 13:10:54 +02003619/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003620 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003621 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003622 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003623 * @param[in,out] argument Value to write to.
3624 * @param[in,out] flags Flags to write to.
3625 * @param[in,out] exts Extension instances to add to.
3626 *
3627 * @return LY_ERR values.
3628 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003630parse_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 +02003631{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003632 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003634 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003635 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003636
3637 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003638 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639 return LY_EVALID;
3640 }
3641
3642 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003643 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003644 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003645
Radek Krejci33090f92020-12-17 20:12:46 +01003646 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003648 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003649 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003651 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003652 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653 break;
3654 default:
David Sedlákb3077192019-06-19 10:55:37 +02003655 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003656 return LY_EVALID;
3657 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003659 return ret;
3660}
3661
Michal Vaskoea5abea2018-09-18 13:10:54 +02003662/**
3663 * @brief Parse the extension statement.
3664 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003665 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003666 * @param[in,out] extensions Extensions to add to.
3667 *
3668 * @return LY_ERR values.
3669 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003670static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003671parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003673 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003675 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003676 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003677 struct lysp_ext *ex;
3678
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003679 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003680
3681 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003682 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003683 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003684
Radek Krejci33090f92020-12-17 20:12:46 +01003685 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003686 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003687 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003688 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 +02003689 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003690 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003691 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 +02003692 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003693 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003694 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003695 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003696 case LY_STMT_ARGUMENT:
Radek Krejci9f87b0c2021-03-05 14:45:26 +01003697 LY_CHECK_RET(parse_argument(ctx, &ex->argname, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003698 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003699 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003700 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701 break;
3702 default:
David Sedlákb3077192019-06-19 10:55:37 +02003703 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704 return LY_EVALID;
3705 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003706 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003707 return ret;
3708}
3709
Michal Vaskoea5abea2018-09-18 13:10:54 +02003710/**
3711 * @brief Parse the deviate statement.
3712 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003713 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003714 * @param[in,out] deviates Deviates to add to.
3715 *
3716 * @return LY_ERR values.
3717 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003718LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003719parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003721 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003722 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003723 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003724 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003725 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726 struct lysp_deviate_add *d_add = NULL;
3727 struct lysp_deviate_rpl *d_rpl = NULL;
3728 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003729 const char **d_units = NULL;
3730 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003731 struct lysp_restr **d_musts = NULL;
3732 uint16_t *d_flags = 0;
3733 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734
3735 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003736 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003737
Radek Krejcif13b87b2020-12-01 22:02:17 +01003738 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003740 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003741 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003742 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003744 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 dev_mod = LYS_DEV_DELETE;
3746 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003747 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748 free(buf);
3749 return LY_EVALID;
3750 }
3751 free(buf);
3752
3753 /* create structure */
3754 switch (dev_mod) {
3755 case LYS_DEV_NOT_SUPPORTED:
3756 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003757 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003758 break;
3759 case LYS_DEV_ADD:
3760 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003761 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762 d = (struct lysp_deviate *)d_add;
3763 d_units = &d_add->units;
3764 d_uniques = &d_add->uniques;
3765 d_dflts = &d_add->dflts;
3766 d_musts = &d_add->musts;
3767 d_flags = &d_add->flags;
3768 d_min = &d_add->min;
3769 d_max = &d_add->max;
3770 break;
3771 case LYS_DEV_REPLACE:
3772 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003773 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003774 d = (struct lysp_deviate *)d_rpl;
3775 d_units = &d_rpl->units;
3776 d_flags = &d_rpl->flags;
3777 d_min = &d_rpl->min;
3778 d_max = &d_rpl->max;
3779 break;
3780 case LYS_DEV_DELETE:
3781 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003782 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 d = (struct lysp_deviate *)d_del;
3784 d_units = &d_del->units;
3785 d_uniques = &d_del->uniques;
3786 d_dflts = &d_del->dflts;
3787 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 break;
3789 default:
3790 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003791 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003792 }
3793 d->mod = dev_mod;
3794
3795 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003796 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003797
Radek Krejci33090f92020-12-17 20:12:46 +01003798 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003800 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 switch (dev_mod) {
3802 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003803 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003804 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805 return LY_EVALID;
3806 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003807 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 break;
3809 }
3810 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003811 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 switch (dev_mod) {
3813 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003814 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003815 return LY_EVALID;
3816 case LYS_DEV_REPLACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003817 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 +02003818 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 break;
3820 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003821 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003822 break;
3823 }
3824 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003825 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 switch (dev_mod) {
3827 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003828 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003829 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 return LY_EVALID;
3831 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003832 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003833 break;
3834 }
3835 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003836 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003837 switch (dev_mod) {
3838 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003839 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003840 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 return LY_EVALID;
3842 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003843 LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844 break;
3845 }
3846 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003847 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 switch (dev_mod) {
3849 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003850 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003851 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 return LY_EVALID;
3853 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003854 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855 break;
3856 }
3857 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003858 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003859 switch (dev_mod) {
3860 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003861 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003862 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863 return LY_EVALID;
3864 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003865 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 break;
3867 }
3868 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003869 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 switch (dev_mod) {
3871 case LYS_DEV_NOT_SUPPORTED:
3872 case LYS_DEV_ADD:
3873 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003874 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003875 return LY_EVALID;
3876 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003877 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003878 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003879 return LY_EVALID;
3880 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003882 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003883 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 break;
3885 }
3886 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003887 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888 switch (dev_mod) {
3889 case LYS_DEV_NOT_SUPPORTED:
3890 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003891 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 return LY_EVALID;
3893 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003894 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 break;
3896 }
3897 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003898 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003899 switch (dev_mod) {
3900 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003901 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 return LY_EVALID;
3903 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003904 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 +02003905 break;
3906 }
3907 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003908 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003909 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATE, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910 break;
3911 default:
David Sedlákb3077192019-06-19 10:55:37 +02003912 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003913 return LY_EVALID;
3914 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003915 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003916 return ret;
3917}
3918
Michal Vaskoea5abea2018-09-18 13:10:54 +02003919/**
3920 * @brief Parse the deviation statement.
3921 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003922 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003923 * @param[in,out] deviations Deviations to add to.
3924 *
3925 * @return LY_ERR values.
3926 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003927LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003928parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003930 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003932 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003933 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934 struct lysp_deviation *dev;
3935
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003936 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937
3938 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003939 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003940 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003941 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942
Radek Krejci33090f92020-12-17 20:12:46 +01003943 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003945 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003946 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 +02003947 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003948 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003949 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003951 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003952 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 +02003953 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003954 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003955 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATION, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956 break;
3957 default:
David Sedlákb3077192019-06-19 10:55:37 +02003958 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959 return LY_EVALID;
3960 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003962 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003963checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964 /* mandatory substatements */
3965 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003966 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967 return LY_EVALID;
3968 }
3969
3970 return ret;
3971}
3972
Michal Vaskoea5abea2018-09-18 13:10:54 +02003973/**
3974 * @brief Parse the feature statement.
3975 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003976 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003977 * @param[in,out] features Features to add to.
3978 *
3979 * @return LY_ERR values.
3980 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003981LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003982parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003984 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003986 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003987 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003988 struct lysp_feature *feat;
3989
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003990 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991
3992 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003993 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003994 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003995
Radek Krejci33090f92020-12-17 20:12:46 +01003996 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003998 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003999 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 +02004000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004001 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004002 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004004 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004005 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004007 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004008 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004010 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004011 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004012 break;
4013 default:
David Sedlákb3077192019-06-19 10:55:37 +02004014 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004015 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004018 return ret;
4019}
4020
Michal Vaskoea5abea2018-09-18 13:10:54 +02004021/**
4022 * @brief Parse the identity statement.
4023 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004024 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004025 * @param[in,out] identities Identities to add to.
4026 *
4027 * @return LY_ERR values.
4028 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004029LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004030parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004031{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004032 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004034 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004035 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004036 struct lysp_ident *ident;
4037
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004038 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004039
4040 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004041 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004042 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004043
Radek Krejci33090f92020-12-17 20:12:46 +01004044 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004045 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004046 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004047 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 +02004048 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004049 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004050 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004051 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004052 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004053 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004054 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 +02004055 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004056 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004057 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004058 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004059 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004060 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004061 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 +01004062 return LY_EVALID;
4063 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004064 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 +02004065 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004066 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004067 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004068 break;
4069 default:
David Sedlákb3077192019-06-19 10:55:37 +02004070 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004071 return LY_EVALID;
4072 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004074 return ret;
4075}
4076
Michal Vaskoea5abea2018-09-18 13:10:54 +02004077/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004078 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004079 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004080 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004081 * @param[in,out] mod Module to write to.
4082 *
4083 * @return LY_ERR values.
4084 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004085LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004086parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004087{
4088 LY_ERR ret = 0;
4089 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004090 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004091 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004092 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004093 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004094
Michal Vaskoc3781c32020-10-06 14:04:08 +02004095 mod->is_submod = 0;
4096
4097 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004098 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004099 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004100
Radek Krejci33090f92020-12-17 20:12:46 +01004101 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102
Radek Krejcie3846472018-10-15 15:24:51 +02004103#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004104 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 +02004105
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106 switch (kw) {
4107 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004108 case LY_STMT_NAMESPACE:
4109 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004110 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004112 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004113 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004114 break;
4115 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004116 case LY_STMT_INCLUDE:
4117 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004118 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004119 break;
4120 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004121 case LY_STMT_ORGANIZATION:
4122 case LY_STMT_CONTACT:
4123 case LY_STMT_DESCRIPTION:
4124 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004125 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004126 break;
4127
4128 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004129 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004130 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004132 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004133 case LY_STMT_ANYDATA:
4134 case LY_STMT_ANYXML:
4135 case LY_STMT_AUGMENT:
4136 case LY_STMT_CHOICE:
4137 case LY_STMT_CONTAINER:
4138 case LY_STMT_DEVIATION:
4139 case LY_STMT_EXTENSION:
4140 case LY_STMT_FEATURE:
4141 case LY_STMT_GROUPING:
4142 case LY_STMT_IDENTITY:
4143 case LY_STMT_LEAF:
4144 case LY_STMT_LEAF_LIST:
4145 case LY_STMT_LIST:
4146 case LY_STMT_NOTIFICATION:
4147 case LY_STMT_RPC:
4148 case LY_STMT_TYPEDEF:
4149 case LY_STMT_USES:
4150 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151 mod_stmt = Y_MOD_BODY;
4152 break;
4153 default:
4154 /* error handled in the next switch */
4155 break;
4156 }
Radek Krejcie3846472018-10-15 15:24:51 +02004157#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004158
Radek Krejcie3846472018-10-15 15:24:51 +02004159 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004160 switch (kw) {
4161 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004162 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004163 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004164 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004165 case LY_STMT_NAMESPACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004166 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 +02004167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004168 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01004169 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 +02004170 break;
4171
4172 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004173 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004174 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004176 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004177 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004178 break;
4179
4180 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004181 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004182 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 +02004183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004184 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004185 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 +02004186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004187 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004188 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 +02004189 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004190 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004191 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 +02004192 break;
4193
4194 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004195 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004196 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004197 break;
4198
4199 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004200 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004201 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004202 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004203 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004204 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004206 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004207 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004209 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004210 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004212 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004213 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004215 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004216 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004218 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004219 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004221 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004222 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004223 break;
4224
Radek Krejcid6b76452019-09-03 17:03:03 +02004225 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004226 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004228 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004229 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004231 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004232 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004234 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004235 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004237 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004238 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004240 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004241 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004243 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004244 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004246 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004247 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004249 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004250 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004252 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004253 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004254 break;
4255
4256 default:
David Sedlákb3077192019-06-19 10:55:37 +02004257 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004258 return LY_EVALID;
4259 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004260 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004261 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004262
Radek Krejci6d6556c2018-11-08 09:37:45 +01004263checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004264 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004265 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004266 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004267 return LY_EVALID;
4268 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004269 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004270 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004271 }
4272
Radek Krejcie9e987e2018-10-31 12:50:27 +01004273 /* submodules share the namespace with the module names, so there must not be
4274 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004275 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004276 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004277 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004278 return LY_EVALID;
4279 }
4280
4281 return ret;
4282}
4283
4284/**
4285 * @brief Parse submodule substatements.
4286 *
4287 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004288 * @param[out] submod Parsed submodule structure.
4289 *
4290 * @return LY_ERR values.
4291 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004292LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004293parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004294{
4295 LY_ERR ret = 0;
4296 char *buf, *word;
4297 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004298 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004299 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004300 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004301
Michal Vaskoc3781c32020-10-06 14:04:08 +02004302 submod->is_submod = 1;
4303
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004304 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004305 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004306 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004307
Radek Krejci33090f92020-12-17 20:12:46 +01004308 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004309
4310#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004311 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 +01004312
4313 switch (kw) {
4314 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004315 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004316 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4317 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004318 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004319 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4320 break;
4321 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004322 case LY_STMT_INCLUDE:
4323 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004324 CHECK_ORDER(Y_MOD_LINKAGE);
4325 break;
4326 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004327 case LY_STMT_ORGANIZATION:
4328 case LY_STMT_CONTACT:
4329 case LY_STMT_DESCRIPTION:
4330 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004331 CHECK_ORDER(Y_MOD_META);
4332 break;
4333
4334 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004336 CHECK_ORDER(Y_MOD_REVISION);
4337 break;
4338 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004339 case LY_STMT_ANYDATA:
4340 case LY_STMT_ANYXML:
4341 case LY_STMT_AUGMENT:
4342 case LY_STMT_CHOICE:
4343 case LY_STMT_CONTAINER:
4344 case LY_STMT_DEVIATION:
4345 case LY_STMT_EXTENSION:
4346 case LY_STMT_FEATURE:
4347 case LY_STMT_GROUPING:
4348 case LY_STMT_IDENTITY:
4349 case LY_STMT_LEAF:
4350 case LY_STMT_LEAF_LIST:
4351 case LY_STMT_LIST:
4352 case LY_STMT_NOTIFICATION:
4353 case LY_STMT_RPC:
4354 case LY_STMT_TYPEDEF:
4355 case LY_STMT_USES:
4356 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004357 mod_stmt = Y_MOD_BODY;
4358 break;
4359 default:
4360 /* error handled in the next switch */
4361 break;
4362 }
4363#undef CHECK_ORDER
4364
4365 prev_kw = kw;
4366 switch (kw) {
4367 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004368 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004369 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004371 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004372 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004373 break;
4374
4375 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004376 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004377 if (submod->version == LYS_VERSION_1_1) {
4378 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4379 submod->name);
4380 }
Radek Krejci33090f92020-12-17 20:12:46 +01004381 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004383 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004384 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004385 break;
4386
4387 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004389 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 +01004390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004392 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 +01004393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004395 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 +01004396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004397 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004398 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 +01004399 break;
4400
4401 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004402 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004403 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004404 break;
4405
4406 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004407 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004408 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004409 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004410 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004411 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004413 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004414 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004416 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004417 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004419 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004420 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004422 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004423 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004425 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004426 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004428 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004429 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004430 break;
4431
Radek Krejcid6b76452019-09-03 17:03:03 +02004432 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004433 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004435 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004436 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004438 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004439 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004441 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004442 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004444 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004445 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004447 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004448 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004450 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004451 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004453 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004454 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004456 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004457 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004459 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004460 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004461 break;
4462
4463 default:
David Sedlákb3077192019-06-19 10:55:37 +02004464 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004465 return LY_EVALID;
4466 }
4467 }
4468 LY_CHECK_RET(ret);
4469
4470checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004471 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004472 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004473 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004474 return LY_EVALID;
4475 }
4476
4477 /* submodules share the namespace with the module names, so there must not be
4478 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004479 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004480 /* main modules may have different revisions */
4481 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004482 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004483 return LY_EVALID;
4484 }
4485
Michal Vasko7fbc8162018-09-17 10:35:16 +02004486 return ret;
4487}
4488
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004489/**
4490 * @brief Skip any redundant characters, namely whitespaces and comments.
4491 *
4492 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004493 * @return LY_SUCCESS on success.
4494 * @return LY_EVALID on invalid comment.
4495 */
4496static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004497skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004498{
4499 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004500 while (ctx->in->current[0]) {
4501 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004502 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004503 ly_in_skip(ctx->in, 2);
4504 LY_CHECK_RET(skip_comment(ctx, 1));
4505 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004506 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004507 ly_in_skip(ctx->in, 2);
4508 LY_CHECK_RET(skip_comment(ctx, 2));
4509 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004510 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004511 if (ctx->in->current[0] == '\n') {
4512 LY_IN_NEW_LINE(ctx->in);
4513 }
Radek Krejci33090f92020-12-17 20:12:46 +01004514 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004515 } else {
4516 break;
4517 }
4518 }
4519
4520 return LY_SUCCESS;
4521}
4522
Radek Krejcid4557c62018-09-17 11:42:09 +02004523LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004524yang_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 +02004525 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004526{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004527 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004528 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004529 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004530 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004531 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004532
David Sedlák1b623122019-08-05 15:27:49 +02004533 /* create context */
4534 *context = calloc(1, sizeof **context);
4535 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004536 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004537 (*context)->unres = main_ctx->unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004538 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004539
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004540 mod_p = calloc(1, sizeof *mod_p);
4541 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4542 mod_p->mod = main_ctx->parsed_mod->mod;
4543 mod_p->parsing = 1;
4544 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4545
Radek Krejciddace2c2021-01-08 11:30:56 +01004546 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004547
David Sedlák1b623122019-08-05 15:27:49 +02004548 /* map the typedefs and groupings list from main context to the submodule's context */
4549 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4550 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4551
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004552 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004553 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004554 LY_CHECK_GOTO(ret, cleanup);
4555
Michal Vasko7fbc8162018-09-17 10:35:16 +02004556 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004557 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004558 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004559
Radek Krejcid6b76452019-09-03 17:03:03 +02004560 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004561 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004562 ret = LY_EINVAL;
4563 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004564 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004565 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004566 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004567 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004568 }
4569
Michal Vasko7fbc8162018-09-17 10:35:16 +02004570 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004571 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004572 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004573
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004574 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004575 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004576 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004577 if (in->current[0]) {
4578 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004579 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004580 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004581 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004582
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004583 mod_p->parsing = 0;
4584 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004585
Radek Krejcibbe09a92018-11-08 09:36:54 +01004586cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004587 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004588 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004589 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004590 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004591 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004592 }
4593
4594 return ret;
4595}
4596
4597LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004598yang_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 +01004599{
4600 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004601 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004602 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004603 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004604 struct lysp_module *mod_p = NULL;
4605
David Sedlák1b623122019-08-05 15:27:49 +02004606 /* create context */
4607 *context = calloc(1, sizeof **context);
4608 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004609 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004610 (*context)->unres = unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004611 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004612
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004613 mod_p = calloc(1, sizeof *mod_p);
4614 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4615 mod_p->mod = mod;
4616 mod_p->parsing = 1;
4617 (*context)->parsed_mod = mod_p;
4618
Radek Krejciddace2c2021-01-08 11:30:56 +01004619 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004620
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004621 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004622 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004623 LY_CHECK_GOTO(ret, cleanup);
4624
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004625 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004626 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004627 LY_CHECK_GOTO(ret, cleanup);
4628
Radek Krejcid6b76452019-09-03 17:03:03 +02004629 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004630 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 +01004631 ret = LY_EINVAL;
4632 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004633 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004634 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004635 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004636 goto cleanup;
4637 }
4638
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004639 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004640 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004641 LY_CHECK_GOTO(ret, cleanup);
4642
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004643 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004644 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004645 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004646 if (in->current[0]) {
4647 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004648 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004649 goto cleanup;
4650 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004651
4652 mod_p->parsing = 0;
4653 mod->parsed = mod_p;
4654
4655cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004656 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004657 if (ret) {
4658 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004659 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004660 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004661 }
4662
Michal Vasko7fbc8162018-09-17 10:35:16 +02004663 return ret;
4664}