blob: d92bc878d6d7b7364658316497a22711b8ac3b99 [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"
33#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020034#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020035
Radek Krejciceaf2122019-01-02 15:03:26 +010036/**
37 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
38 * @param[in] CTX yang parser context to access libyang context.
39 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
40 * @param[out] TARGET variable where to store the pointer to the inserted value.
41 * @param[in] WORD string to store.
42 * @param[in] LEN length of the string in WORD to store.
43 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020044#define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \
Michal Vasko5d24f6c2020-10-13 13:49:06 +020045 if (BUF) {LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)));}\
46 else {LY_CHECK_RET(lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)));}
Radek Krejci44ceedc2018-10-02 15:54:31 +020047
Radek Krejciceaf2122019-01-02 15:03:26 +010048/**
Michal Vasko63f3d842020-07-08 10:10:14 +020049 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Radek Krejciceaf2122019-01-02 15:03:26 +010050 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010051 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
52 */
Radek Krejcid54412f2020-12-17 20:25:35 +010053#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020054
Michal Vaskoea5abea2018-09-18 13:10:54 +020055/**
56 * @brief Loop through all substatements providing, return if there are none.
57 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020058 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020059 * @param[out] KW YANG keyword read.
60 * @param[out] WORD Pointer to the keyword itself.
61 * @param[out] WORD_LEN Length of the keyword.
62 * @param[out] ERR Variable for error storing.
63 *
64 * @return In case there are no substatements or a fatal error encountered.
65 */
Radek Krejci33090f92020-12-17 20:12:46 +010066#define YANG_READ_SUBSTMT_FOR(CTX, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
67 LY_CHECK_RET(get_keyword(CTX, &KW, &WORD, &WORD_LEN)); \
Radek Krejcid6b76452019-09-03 17:03:03 +020068 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010069 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010070 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020071 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020072 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020073 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020074 return LY_EVALID; \
75 } \
Radek Krejci33090f92020-12-17 20:12:46 +010076 for (ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020077 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Radek Krejci33090f92020-12-17 20:12:46 +010078 ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020079
Radek Krejci33090f92020-12-17 20:12:46 +010080LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020081 struct lysp_node **siblings);
Radek Krejci33090f92020-12-17 20:12:46 +010082LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
83LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
84LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
85LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +010086LY_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 +020087
Michal Vaskoea5abea2018-09-18 13:10:54 +020088/**
89 * @brief Add another character to dynamic buffer, a low-level function.
90 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020091 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020092 *
Radek Krejci404251e2018-10-09 12:06:44 +020093 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +020094 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +020095 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020096 * @param[in,out] buf Buffer to use, can be moved by realloc().
97 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +020098 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020099 *
100 * @return LY_ERR values.
101 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200102LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200103buf_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 +0200104{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100105#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200106 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100107 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200108 *buf = ly_realloc(*buf, *buf_len);
109 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
110 }
Radek Krejcic0917392019-04-10 13:04:04 +0200111 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200112 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200113 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200114 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200115 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200116
Radek Krejci44ceedc2018-10-02 15:54:31 +0200117 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200118 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100119#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200120}
121
Michal Vaskoea5abea2018-09-18 13:10:54 +0200122/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200123 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
124 *
125 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 * @param[in] arg Type of the input string to select method of checking character validity.
127 * @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 +0200128 * 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 +0200129 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
130 * @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 +0200131 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200133 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
134 * 0 - colon not yet found (no prefix)
135 * 1 - \p c is the colon character
136 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200137 *
138 * @return LY_ERR values.
139 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200140LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100141buf_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 +0200142 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200144 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200145 size_t len;
146
Radek Krejcif29b7c32019-04-09 16:17:49 +0200147 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
148 assert(!need_buf || (need_buf && word_b));
149
Radek Krejci44ceedc2018-10-02 15:54:31 +0200150 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100151 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
152 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
153 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200154 if (c == '\n') {
155 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100156 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200157 } else {
158 /* note - even the multibyte character is count as 1 */
159 ++ctx->indent;
160 }
161
Radek Krejci44ceedc2018-10-02 15:54:31 +0200162 /* check character validity */
163 switch (arg) {
164 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100165 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166 break;
167 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100168 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200169 break;
170 case Y_STR_ARG:
171 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100172 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200173 break;
174 }
175
Michal Vasko7fbc8162018-09-17 10:35:16 +0200176 if (word_b && *word_b) {
177 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100178 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200179 return LY_EMEM;
180 }
181
182 /* in case of realloc */
183 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200184 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200185 /* first time we need a buffer, copy everything read up to now */
186 if (*word_len) {
187 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200188 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200189 *buf_len = *word_len;
190 memcpy(*word_b, *word_p, *word_len);
191 }
192
193 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100194 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200195 return LY_EMEM;
196 }
197
198 /* in case of realloc */
199 *word_p = *word_b;
200 } else {
201 /* just remember the first character pointer */
202 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100203 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200204 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200205 /* ... and update the word's length */
206 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100207 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200208 }
209
210 return LY_SUCCESS;
211}
212
Michal Vaskoea5abea2018-09-18 13:10:54 +0200213/**
214 * @brief Skip YANG comment in data.
215 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200216 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200217 * @param[in] comment Type of the comment to process:
218 * 1 for a one-line comment,
219 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200220 * @return LY_ERR values.
221 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200222LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100223skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200224{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100225 /* internal statuses: */
226#define COMMENT_NO 0 /* comment ended */
227#define COMMENT_LINE 1 /* in line comment */
228#define COMMENT_BLOCK 2 /* in block comment */
229#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
230
Radek Krejci33090f92020-12-17 20:12:46 +0100231 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200232 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100233 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100234 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100235 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100236 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200237 }
238 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100239 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100240 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100241 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100242 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100243 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200244 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200245 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100246 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100247 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100248 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100249 } else if (ctx->in->current[0] != '*') {
250 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100251 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200252 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100253 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200254 }
255 break;
256 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200257 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200258 }
259
Radek Krejci33090f92020-12-17 20:12:46 +0100260 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200261 ctx->indent = 0;
262 } else {
263 ++ctx->indent;
264 }
Radek Krejci33090f92020-12-17 20:12:46 +0100265 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200266 }
267
Radek Krejci33090f92020-12-17 20:12:46 +0100268 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200269 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200270 return LY_EVALID;
271 }
272
273 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100274
275#undef COMMENT_NO
276#undef COMMENT_LINE
277#undef COMMENT_BLOCK
278#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200279}
280
Michal Vaskoea5abea2018-09-18 13:10:54 +0200281/**
282 * @brief Read a quoted string from data.
283 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200284 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200285 * @param[in] arg Type of YANG keyword argument expected.
286 * @param[out] word_p Pointer to the read quoted string.
287 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
288 * set to NULL. Otherwise equal to \p word_p.
289 * @param[out] word_len Length of the read quoted string.
290 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
291 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
292 * indenation in the final quoted string.
293 *
294 * @return LY_ERR values.
295 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200296static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100297read_qstring(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200298 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200299{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100300 /* string parsing status: */
301#define STRING_ENDED 0 /* string ended */
302#define STRING_SINGLE_QUOTED 1 /* string with ' */
303#define STRING_DOUBLE_QUOTED 2 /* string with " */
304#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
305#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
306#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
307
Radek Krejci1deb5be2020-08-26 16:43:36 +0200308 uint8_t string;
309 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200310 ly_bool need_buf = 0;
311 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200312 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200313 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200314
Radek Krejci33090f92020-12-17 20:12:46 +0100315 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100316 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200317 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200318 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100319 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100320 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 }
Radek Krejci33090f92020-12-17 20:12:46 +0100322 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200323
Radek Krejci33090f92020-12-17 20:12:46 +0100324 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200325 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100326 case STRING_SINGLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100327 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200328 case '\'':
329 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100330 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100331 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200332 break;
333 default:
334 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100335 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 +0200336 break;
337 }
338 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100339 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100340 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200341 case '\"':
342 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100343 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100344 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200345 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200346 break;
347 case '\\':
348 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100349 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200350
351 /* the backslash sequence is substituted, so we will need a buffer to store the result */
352 need_buf = 1;
353
354 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100355 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200356
357 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
358 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
359 trailing_ws = 0;
360
361 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
362 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200363 break;
364 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200365 if (current_indent < block_indent) {
366 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100367 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200368 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100369 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100370 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 +0100371 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200372 }
373 break;
374 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200375 if (current_indent < block_indent) {
376 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100377 current_indent += Y_TAB_SPACES;
378 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200379 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200380 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100381 c = ctx->in->current;
382 ctx->in->current = " ";
383 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
384 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100385 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200386 }
Radek Krejci33090f92020-12-17 20:12:46 +0100387 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200388 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100389 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100390 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 +0100391 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200392 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100393 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200394 }
395 break;
396 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200397 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200398 /* we will be removing the indents so we need our own buffer */
399 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200400
401 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200402 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200403
Radek Krejciff13cd12019-10-25 15:34:24 +0200404 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200405 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200406 }
407
408 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100409 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 +0200410
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200411 /* reset context indentation counter for possible string after this one */
412 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200413 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200414 break;
415 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200416 /* first non-whitespace character, stop eating indentation */
417 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200418
419 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100420 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 +0200421 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200422 break;
423 }
424 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100425 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200426 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100427 c = ctx->in->current;
428 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100430 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100431 /* fix false newline count in buf_store_char() */
432 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433 break;
434 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100435 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200436 break;
437 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200439 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200440 break;
441 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200442 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100443 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200444 return LY_EVALID;
445 }
446
447 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100448 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 +0200449
Radek Krejcif13b87b2020-12-01 22:02:17 +0100450 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100451 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200452 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100453 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100454 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200455 case '+':
456 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100457 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200458 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200459 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200460 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100461 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100462 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200463 case ' ':
464 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200465 /* just skip */
466 break;
467 default:
468 /* string is finished */
469 goto string_end;
470 }
Radek Krejci33090f92020-12-17 20:12:46 +0100471 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200472 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100473 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100474 switch (ctx->in->current[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200475 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100476 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100477 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200478 case ' ':
479 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 /* skip */
481 break;
482 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100483 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200484 break;
485 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100486 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 break;
488 default:
489 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200490 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200491 return LY_EVALID;
492 }
Radek Krejci33090f92020-12-17 20:12:46 +0100493 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200494 break;
495 default:
496 return LY_EINT;
497 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200498 }
499
500string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200501 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200502 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200503 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200504 return LY_EVALID;
505 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200506 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100507
508#undef STRING_ENDED
509#undef STRING_SINGLE_QUOTED
510#undef STRING_DOUBLE_QUOTED
511#undef STRING_DOUBLE_QUOTED_ESCAPED
512#undef STRING_PAUSED_NEXTSTRING
513#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200514}
515
Michal Vaskoea5abea2018-09-18 13:10:54 +0200516/**
517 * @brief Get another YANG string from the raw data.
518 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200519 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200520 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200521 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QOUTED - see [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200522 * @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 +0200523 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
524 * set to NULL. Otherwise equal to \p word_p.
525 * @param[out] word_len Length of the read string.
526 *
527 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200528 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200529LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100530get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200531 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200532{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200533 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200534 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200535
Michal Vasko7fbc8162018-09-17 10:35:16 +0200536 /* word buffer - dynamically allocated */
537 *word_b = NULL;
538
539 /* word pointer - just a pointer to data */
540 *word_p = NULL;
541
542 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100543 while (ctx->in->current[0]) {
544 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200545 case '\'':
546 case '\"':
547 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200548 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100549 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200550 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200551 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200552 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200553 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100554 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200555 }
Radek Krejci33090f92020-12-17 20:12:46 +0100556 LY_CHECK_RET(read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200557 goto str_end;
558 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100559 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200560 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100561 MOVE_INPUT(ctx, 2);
562 LY_CHECK_RET(skip_comment(ctx, 1));
563 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200564 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100565 MOVE_INPUT(ctx, 2);
566 LY_CHECK_RET(skip_comment(ctx, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200567 } else {
568 /* not a comment after all */
Radek Krejci33090f92020-12-17 20:12:46 +0100569 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200570 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200571 break;
572 case ' ':
573 if (*word_len) {
574 /* word is finished */
575 goto str_end;
576 }
Radek Krejci33090f92020-12-17 20:12:46 +0100577 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200578 break;
579 case '\t':
580 if (*word_len) {
581 /* word is finished */
582 goto str_end;
583 }
584 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100585 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200586
Radek Krejci33090f92020-12-17 20:12:46 +0100587 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200588 break;
589 case '\n':
590 if (*word_len) {
591 /* word is finished */
592 goto str_end;
593 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100594 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100595 MOVE_INPUT(ctx, 1);
596
Michal Vasko7fbc8162018-09-17 10:35:16 +0200597 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200598 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200599 break;
600 case ';':
601 case '{':
602 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
603 /* word is finished */
604 goto str_end;
605 }
606
Radek Krejci33090f92020-12-17 20:12:46 +0100607 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200608 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200609 case '}':
610 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100611 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200612 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200613 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200614 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100615 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200616 break;
617 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200618 }
619
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200620 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200621 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200622 return LY_EVALID;
623
Michal Vasko7fbc8162018-09-17 10:35:16 +0200624str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200625 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200626 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200627 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200628 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200629 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200630 *word_p = *word_b;
631 }
632
633 return LY_SUCCESS;
634}
635
Michal Vaskoea5abea2018-09-18 13:10:54 +0200636/**
637 * @brief Get another YANG keyword from the raw data.
638 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200639 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200640 * @param[out] kw YANG keyword read.
641 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
642 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
643 *
644 * @return LY_ERR values.
645 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200646LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100647get_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 +0200648{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200649 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200650 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200651 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200652
653 if (word_p) {
654 *word_p = NULL;
655 *word_len = 0;
656 }
657
658 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100659 while (ctx->in->current[0]) {
660 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200661 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100662 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200663 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100664 MOVE_INPUT(ctx, 2);
665 LY_CHECK_RET(skip_comment(ctx, 1));
666 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200667 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100668 MOVE_INPUT(ctx, 2);
669 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200671 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200672 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200673 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200674 }
Radek Krejci13028282019-06-11 14:56:48 +0200675 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200676 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200677 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100678 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200679 ctx->indent = 0;
680 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200681 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200682 /* skip whitespaces (optsep) */
683 ++ctx->indent;
684 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200685 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100687 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 break;
689 default:
690 /* either a keyword start or an invalid character */
691 goto keyword_start;
692 }
693
Radek Krejci33090f92020-12-17 20:12:46 +0100694 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 }
696
697keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100698 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100699 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200700
Michal Vasko69730152020-10-09 16:30:07 +0200701 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 +0200702 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200703 }
704
Radek Krejcid6b76452019-09-03 17:03:03 +0200705 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200706 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100707 switch (ctx->in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200708 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200709 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200710 case ' ':
711 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200712 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200713 case ':':
714 /* keyword is not actually a keyword, but prefix of an extension.
715 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
716 * and we will be checking the keyword (extension instance) itself */
717 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100718 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200719 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200720 case '{':
721 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200722 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200723 break;
724 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100725 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200726 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100727 MOVE_INPUT(ctx, 1);
728 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200729 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 return LY_EVALID;
731 }
732 } else {
733 /* still can be an extension */
734 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200735extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100736 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
737 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '{') && (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200738 uint32_t c = 0;
739
Radek Krejci33090f92020-12-17 20:12:46 +0100740 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
741 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200742 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200743 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200744 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100745 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200746 }
Radek Krejci33090f92020-12-17 20:12:46 +0100747 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200748 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200749 return LY_EVALID;
750 }
751
752 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200753 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100754 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200755 return LY_EVALID;
756 }
757
Radek Krejcid6b76452019-09-03 17:03:03 +0200758 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 }
Radek Krejci626df482018-10-11 15:06:31 +0200760success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200761 if (word_p) {
762 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100763 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200764 }
765
766 return LY_SUCCESS;
767}
768
Michal Vaskoea5abea2018-09-18 13:10:54 +0200769/**
770 * @brief Parse extension instance substatements.
771 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200772 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200773 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200774 * @param[in] word Extension instance substatement name (keyword).
775 * @param[in] word_len Extension instance substatement name length.
776 * @param[in,out] child Children of this extension instance to add to.
777 *
778 * @return LY_ERR values.
779 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200780static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100781parse_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 +0200782 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200783{
784 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100785 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200786 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200787 struct lysp_stmt *stmt, *par_child;
788
789 stmt = calloc(1, sizeof *stmt);
790 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
791
Radek Krejcibb9b1982019-04-08 14:24:59 +0200792 /* insert into parent statements */
793 if (!*child) {
794 *child = stmt;
795 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200796 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200797 par_child->next = stmt;
798 }
799
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200800 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Radek Krejci335332a2019-09-05 13:03:35 +0200801 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200802
803 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100804 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200805
Radek Krejci0ae092d2018-09-20 16:43:19 +0200806 if (word) {
807 if (buf) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200808 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200809 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200810 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200811 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200812 }
813
Radek Krejci33090f92020-12-17 20:12:46 +0100814 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, ) {
815 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200816 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200817 return ret;
818}
819
Michal Vaskoea5abea2018-09-18 13:10:54 +0200820/**
821 * @brief Parse extension instance.
822 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200823 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200824 * @param[in] ext_name Extension instance substatement name (keyword).
825 * @param[in] ext_name_len Extension instance substatement name length.
826 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
827 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
828 * @param[in,out] exts Extension instances to add to.
829 *
830 * @return LY_ERR values.
831 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200832static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100833parse_ext(struct lys_yang_parser_ctx *ctx, const char *ext_name, size_t ext_name_len, LYEXT_SUBSTMT insubstmt,
Radek Krejci0f969882020-08-21 16:56:47 +0200834 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200835{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100836 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200838 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200839 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200840 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200842 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843
844 /* store name and insubstmt info */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200845 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200846 e->insubstmt = insubstmt;
847 e->insubstmt_index = insubstmt_index;
848
849 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100850 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200851
Radek Krejci0ae092d2018-09-20 16:43:19 +0200852 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200853 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200854 }
855
Radek Krejci33090f92020-12-17 20:12:46 +0100856 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
857 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200858 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200859 return ret;
860}
861
Michal Vaskoea5abea2018-09-18 13:10:54 +0200862/**
863 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
864 * description, etc...
865 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200866 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200867 * @param[in] substmt Type of this substatement.
868 * @param[in] substmt_index Index of this substatement.
869 * @param[in,out] value Place to store the parsed value.
870 * @param[in] arg Type of the YANG keyword argument (of the value).
871 * @param[in,out] exts Extension instances to add to.
872 *
873 * @return LY_ERR values.
874 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200875static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100876parse_text_field(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200877 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100879 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200880 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200881 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200882 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200883
884 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200885 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 return LY_EVALID;
887 }
888
889 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100890 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200891
892 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200893 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894
Radek Krejci33090f92020-12-17 20:12:46 +0100895 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200896 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200897 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100898 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200899 break;
900 default:
David Sedlákb3077192019-06-19 10:55:37 +0200901 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200902 return LY_EVALID;
903 }
904 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905 return ret;
906}
907
Michal Vaskoea5abea2018-09-18 13:10:54 +0200908/**
909 * @brief Parse the yang-version statement.
910 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200911 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100912 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200913 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200914 *
915 * @return LY_ERR values.
916 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200917static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100918parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200919{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100920 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200921 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200922 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200923 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200924
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100925 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200926 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927 return LY_EVALID;
928 }
929
930 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100931 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932
Radek Krejci96e48da2020-09-04 13:18:06 +0200933 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100934 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100935 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100936 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200937 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200938 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939 free(buf);
940 return LY_EVALID;
941 }
942 free(buf);
943
Radek Krejci33090f92020-12-17 20:12:46 +0100944 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200945 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200946 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcieccf6602021-02-05 19:42:54 +0100947 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200948 break;
949 default:
David Sedlákb3077192019-06-19 10:55:37 +0200950 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951 return LY_EVALID;
952 }
953 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200954 return ret;
955}
956
Michal Vaskoea5abea2018-09-18 13:10:54 +0200957/**
958 * @brief Parse the belongs-to statement.
959 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200960 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200961 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
962 * @param[in,out] exts Extension instances to add to.
963 *
964 * @return LY_ERR values.
965 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200966static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100967parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100969 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200970 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200971 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200972 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973
Michal Vaskoc3781c32020-10-06 14:04:08 +0200974 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200975 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200976 return LY_EVALID;
977 }
978
Michal Vaskoc3781c32020-10-06 14:04:08 +0200979 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +0100980 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200981 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +0200982 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 +0200983 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +0200984 free(buf);
985 return LY_EVALID;
986 }
987 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200988
Radek Krejci33090f92020-12-17 20:12:46 +0100989 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200991 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +0100992 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200994 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcieccf6602021-02-05 19:42:54 +0100995 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_BELONGS_TO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996 break;
997 default:
David Sedlákb3077192019-06-19 10:55:37 +0200998 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200999 return LY_EVALID;
1000 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001002 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001003checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001004 /* mandatory substatements */
1005 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001006 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007 return LY_EVALID;
1008 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009 return ret;
1010}
1011
Michal Vaskoea5abea2018-09-18 13:10:54 +02001012/**
1013 * @brief Parse the revision-date statement.
1014 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001015 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001016 * @param[in,out] rev Array to store the parsed value in.
1017 * @param[in,out] exts Extension instances to add to.
1018 *
1019 * @return LY_ERR values.
1020 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001021static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001022parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001024 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001026 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001027 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001028
1029 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001030 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031 return LY_EVALID;
1032 }
1033
1034 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001035 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036
1037 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001038 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039 free(buf);
1040 return LY_EVALID;
1041 }
1042
1043 /* store value and spend buf if allocated */
1044 strncpy(rev, word, word_len);
1045 free(buf);
1046
Radek Krejci33090f92020-12-17 20:12:46 +01001047 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001048 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001049 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcieccf6602021-02-05 19:42:54 +01001050 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051 break;
1052 default:
David Sedlákb3077192019-06-19 10:55:37 +02001053 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001054 return LY_EVALID;
1055 }
1056 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001057 return ret;
1058}
1059
Michal Vaskoea5abea2018-09-18 13:10:54 +02001060/**
1061 * @brief Parse the include statement.
1062 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001063 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001064 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001065 * @param[in,out] includes Parsed includes to add to.
1066 *
1067 * @return LY_ERR values.
1068 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001070parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071{
Radek Krejcid33273d2018-10-25 14:55:52 +02001072 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001074 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001075 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001076 struct lysp_include *inc;
1077
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001078 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079
1080 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001081 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001082
Radek Krejci011e4aa2020-09-04 15:22:31 +02001083 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001084
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001085 /* submodules share the namespace with the module names, so there must not be
1086 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001087 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001088 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001089 return LY_EVALID;
1090 }
1091
Radek Krejci33090f92020-12-17 20:12:46 +01001092 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001093 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001094 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001095 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci33090f92020-12-17 20:12:46 +01001096 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001098 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001099 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci33090f92020-12-17 20:12:46 +01001100 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001102 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001103 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001105 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001106 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 break;
1108 default:
David Sedlákb3077192019-06-19 10:55:37 +02001109 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 return LY_EVALID;
1111 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113 return ret;
1114}
1115
Michal Vaskoea5abea2018-09-18 13:10:54 +02001116/**
1117 * @brief Parse the import statement.
1118 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001119 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001120 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001121 * @param[in,out] imports Parsed imports to add to.
1122 *
1123 * @return LY_ERR values.
1124 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001125static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001126parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001128 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001129 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001130 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001131 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132 struct lysp_import *imp;
1133
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001134 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135
1136 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001137 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001138 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139
Radek Krejci33090f92020-12-17 20:12:46 +01001140 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001142 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +01001143 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskob36053d2020-03-26 15:49:30 +01001144 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 +02001145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001146 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001147 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci33090f92020-12-17 20:12:46 +01001148 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001150 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001151 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci33090f92020-12-17 20:12:46 +01001152 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001154 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001155 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001157 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001158 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001159 break;
1160 default:
David Sedlákb3077192019-06-19 10:55:37 +02001161 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001162 return LY_EVALID;
1163 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001165 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001166checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001167 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001168 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169
1170 return ret;
1171}
1172
Michal Vaskoea5abea2018-09-18 13:10:54 +02001173/**
1174 * @brief Parse the revision statement.
1175 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001176 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001177 * @param[in,out] revs Parsed revisions to add to.
1178 *
1179 * @return LY_ERR values.
1180 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001182parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001184 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001186 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001187 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001188 struct lysp_revision *rev;
1189
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001190 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191
1192 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001193 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194
1195 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001196 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001197 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 return LY_EVALID;
1199 }
1200
Radek Krejcib7db73a2018-10-24 14:18:40 +02001201 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001202 free(buf);
1203
Radek Krejci33090f92020-12-17 20:12:46 +01001204 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001205 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001206 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001207 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001209 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001210 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001212 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001213 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 break;
1215 default:
David Sedlákb3077192019-06-19 10:55:37 +02001216 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 return LY_EVALID;
1218 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220 return ret;
1221}
1222
Michal Vaskoea5abea2018-09-18 13:10:54 +02001223/**
1224 * @brief Parse a generic text field that can have more instances such as base.
1225 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001226 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001227 * @param[in] substmt Type of this substatement.
1228 * @param[in,out] texts Parsed values to add to.
1229 * @param[in] arg Type of the expected argument.
1230 * @param[in,out] exts Extension instances to add to.
1231 *
1232 * @return LY_ERR values.
1233 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001235parse_text_fields(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
Radek Krejci0f969882020-08-21 16:56:47 +02001236 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001238 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001240 const char **item;
1241 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001242 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001243
1244 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001245 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246
1247 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001248 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249
Radek Krejci011e4aa2020-09-04 15:22:31 +02001250 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Radek Krejci33090f92020-12-17 20:12:46 +01001251 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001253 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001254 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255 break;
1256 default:
David Sedlákb3077192019-06-19 10:55:37 +02001257 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001258 return LY_EVALID;
1259 }
1260 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261 return ret;
1262}
1263
Michal Vaskoea5abea2018-09-18 13:10:54 +02001264/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001265 * @brief Parse a generic text field that can have more instances such as base.
1266 *
1267 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001268 * @param[in] substmt Type of this substatement.
1269 * @param[in,out] qnames Parsed qnames to add to.
1270 * @param[in] arg Type of the expected argument.
1271 * @param[in,out] exts Extension instances to add to.
1272 *
1273 * @return LY_ERR values.
1274 */
1275static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001276parse_qnames(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001277 enum yang_arg arg, struct lysp_ext_instance **exts)
1278{
1279 LY_ERR ret = LY_SUCCESS;
1280 char *buf, *word;
1281 struct lysp_qname *item;
1282 size_t word_len;
1283 enum ly_stmt kw;
1284
1285 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001286 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001287
1288 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001289 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001290
1291 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001292 item->mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001293 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001294 switch (kw) {
1295 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001296 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001297 break;
1298 default:
1299 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
1300 return LY_EVALID;
1301 }
1302 }
1303 return ret;
1304}
1305
1306/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001307 * @brief Parse the config statement.
1308 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001309 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001310 * @param[in,out] flags Flags to add to.
1311 * @param[in,out] exts Extension instances to add to.
1312 *
1313 * @return LY_ERR values.
1314 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001316parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001317{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001318 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001320 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001321 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001322
1323 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001324 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325 return LY_EVALID;
1326 }
1327
1328 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001329 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330
Radek Krejcif13b87b2020-12-01 22:02:17 +01001331 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001333 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 *flags |= LYS_CONFIG_R;
1335 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001336 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337 free(buf);
1338 return LY_EVALID;
1339 }
1340 free(buf);
1341
Radek Krejci33090f92020-12-17 20:12:46 +01001342 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001343 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001344 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001345 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346 break;
1347 default:
David Sedlákb3077192019-06-19 10:55:37 +02001348 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001349 return LY_EVALID;
1350 }
1351 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001352 return ret;
1353}
1354
Michal Vaskoea5abea2018-09-18 13:10:54 +02001355/**
1356 * @brief Parse the mandatory statement.
1357 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001358 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001359 * @param[in,out] flags Flags to add to.
1360 * @param[in,out] exts Extension instances to add to.
1361 *
1362 * @return LY_ERR values.
1363 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001365parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001367 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001369 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001370 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371
1372 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001373 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374 return LY_EVALID;
1375 }
1376
1377 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001378 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001379
Radek Krejcif13b87b2020-12-01 22:02:17 +01001380 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001382 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383 *flags |= LYS_MAND_FALSE;
1384 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001385 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 free(buf);
1387 return LY_EVALID;
1388 }
1389 free(buf);
1390
Radek Krejci33090f92020-12-17 20:12:46 +01001391 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001392 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001393 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001394 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001395 break;
1396 default:
David Sedlákb3077192019-06-19 10:55:37 +02001397 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001398 return LY_EVALID;
1399 }
1400 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401 return ret;
1402}
1403
Michal Vaskoea5abea2018-09-18 13:10:54 +02001404/**
1405 * @brief Parse a restriction such as range or length.
1406 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001407 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001408 * @param[in] restr_kw Type of this particular restriction.
1409 * @param[in,out] exts Extension instances to add to.
1410 *
1411 * @return LY_ERR values.
1412 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001413static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001414parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001416 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001418 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001419 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420
1421 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001422 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001423
Michal Vaskob36053d2020-03-26 15:49:30 +01001424 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001425 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001426 restr->arg.mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001427 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001428 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001429 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001430 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001432 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001433 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001435 case LY_STMT_ERROR_APP_TAG:
Radek Krejcieccf6602021-02-05 19:42:54 +01001436 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001438 case LY_STMT_ERROR_MESSAGE:
Radek Krejcieccf6602021-02-05 19:42:54 +01001439 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001441 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001442 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443 break;
1444 default:
David Sedlákb3077192019-06-19 10:55:37 +02001445 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001446 return LY_EVALID;
1447 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449 return ret;
1450}
1451
Michal Vaskoea5abea2018-09-18 13:10:54 +02001452/**
1453 * @brief Parse a restriction that can have more instances such as must.
1454 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001455 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001456 * @param[in] restr_kw Type of this particular restriction.
1457 * @param[in,out] restrs Restrictions to add to.
1458 *
1459 * @return LY_ERR values.
1460 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001462parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463{
1464 struct lysp_restr *restr;
1465
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001466 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001467 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468}
1469
Michal Vaskoea5abea2018-09-18 13:10:54 +02001470/**
1471 * @brief Parse the status statement.
1472 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001473 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001474 * @param[in,out] flags Flags to add to.
1475 * @param[in,out] exts Extension instances to add to.
1476 *
1477 * @return LY_ERR values.
1478 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001479static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001480parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001482 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001484 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001485 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486
1487 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001488 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489 return LY_EVALID;
1490 }
1491
1492 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001493 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001494
Radek Krejcif13b87b2020-12-01 22:02:17 +01001495 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001497 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001499 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500 *flags |= LYS_STATUS_OBSLT;
1501 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001502 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 free(buf);
1504 return LY_EVALID;
1505 }
1506 free(buf);
1507
Radek Krejci33090f92020-12-17 20:12:46 +01001508 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001509 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001510 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001511 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_STATUS, 0, 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), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515 return LY_EVALID;
1516 }
1517 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001518 return ret;
1519}
1520
Michal Vaskoea5abea2018-09-18 13:10:54 +02001521/**
1522 * @brief Parse the when statement.
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,out] when_p When pointer to parse to.
1526 *
1527 * @return LY_ERR values.
1528 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001529LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001530parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001532 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001533 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001534 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001535 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536 struct lysp_when *when;
1537
1538 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001539 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001540 return LY_EVALID;
1541 }
1542
1543 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001544 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001545 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546
1547 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001548 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001549 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001550 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551
Radek Krejci33090f92020-12-17 20:12:46 +01001552 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001554 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001555 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001557 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001558 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001560 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001561 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001562 break;
1563 default:
David Sedlákb3077192019-06-19 10:55:37 +02001564 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 return LY_EVALID;
1566 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 return ret;
1569}
1570
Michal Vaskoea5abea2018-09-18 13:10:54 +02001571/**
1572 * @brief Parse the anydata or anyxml statement.
1573 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001574 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001575 * @param[in] kw Type of this particular keyword.
1576 * @param[in,out] siblings Siblings to add to.
1577 *
1578 * @return LY_ERR values.
1579 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001580LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001581parse_any(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001582{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001583 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001585 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 struct lysp_node_anydata *any;
1587
David Sedlák60adc092019-08-06 15:57:02 +02001588 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001589 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001590
Radek Krejcid6b76452019-09-03 17:03:03 +02001591 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001592 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001593
Michal Vasko7fbc8162018-09-17 10:35:16 +02001594 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001595 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001596 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001597
1598 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01001599 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001601 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001602 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001604 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001605 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001607 case LY_STMT_IF_FEATURE:
Radek Krejcieccf6602021-02-05 19:42:54 +01001608 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001610 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001611 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001613 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001614 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001616 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001617 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001619 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001620 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001622 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001623 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001626 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627 break;
1628 default:
David Sedlákb3077192019-06-19 10:55:37 +02001629 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejci0f969882020-08-21 16:56:47 +02001630 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631 return LY_EVALID;
1632 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 return ret;
1635}
1636
Michal Vaskoea5abea2018-09-18 13:10:54 +02001637/**
1638 * @brief Parse the value or position statement. Substatement of type enum statement.
1639 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001640 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001641 * @param[in] val_kw Type of this particular keyword.
1642 * @param[in,out] value Value to write to.
1643 * @param[in,out] flags Flags to write to.
1644 * @param[in,out] exts Extension instances to add to.
1645 *
1646 * @return LY_ERR values.
1647 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001648LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001649parse_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 +02001650 struct lysp_ext_instance **exts)
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, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001654 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001655 long int num = 0;
1656 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001657 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658
1659 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001660 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 return LY_EVALID;
1662 }
1663 *flags |= LYS_SET_VALUE;
1664
1665 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001666 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667
Radek Krejcid6b76452019-09-03 17:03:03 +02001668 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 +02001669 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001670 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671 }
1672
1673 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001674 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001675 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001676 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001677 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001678 goto error;
1679 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001680 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001681 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001682 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001683 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001684 goto error;
1685 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686 }
1687 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001688 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001689 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001690 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001691 }
1692 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001693 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001694 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001695 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001696 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697 *value = num;
1698 } else {
1699 *value = unum;
1700 }
1701 free(buf);
1702
Radek Krejci33090f92020-12-17 20:12:46 +01001703 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001704 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001705 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001706 LY_CHECK_RET(parse_ext(ctx, word, word_len, val_kw == LY_STMT_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707 break;
1708 default:
David Sedlákb3077192019-06-19 10:55:37 +02001709 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710 return LY_EVALID;
1711 }
1712 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001714
1715error:
1716 free(buf);
1717 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718}
1719
Michal Vaskoea5abea2018-09-18 13:10:54 +02001720/**
1721 * @brief Parse the enum or bit statement. Substatement of type statement.
1722 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001723 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001724 * @param[in] enum_kw Type of this particular keyword.
1725 * @param[in,out] enums Enums or bits to add to.
1726 *
1727 * @return LY_ERR values.
1728 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001729static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001730parse_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 +02001731{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001732 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001734 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001735 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736 struct lysp_type_enum *enm;
1737
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001738 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739
1740 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001741 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 +02001742 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001743 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001744 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001745 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001746
Radek Krejci011e4aa2020-09-04 15:22:31 +02001747 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001748 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1749
Radek Krejci33090f92020-12-17 20:12:46 +01001750 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001751 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001752 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001753 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001755 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001756 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcieccf6602021-02-05 19:42:54 +01001757 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001759 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001760 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001762 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001763 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001764 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001765 case LY_STMT_VALUE:
1766 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 +02001767 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001768 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001770 case LY_STMT_POSITION:
1771 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 +02001772 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001773 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001775 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001776 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 break;
1778 default:
David Sedlákb3077192019-06-19 10:55:37 +02001779 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001780 return LY_EVALID;
1781 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001782 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001783 return ret;
1784}
1785
Michal Vaskoea5abea2018-09-18 13:10:54 +02001786/**
1787 * @brief Parse the fraction-digits statement. Substatement of type statement.
1788 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001789 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001790 * @param[in,out] fracdig Value to write to.
1791 * @param[in,out] exts Extension instances to add to.
1792 *
1793 * @return LY_ERR values.
1794 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001795static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001796parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001798 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001800 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001801 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001802 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001803
1804 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001805 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001806 return LY_EVALID;
1807 }
1808
1809 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001810 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811
1812 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001813 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814 free(buf);
1815 return LY_EVALID;
1816 }
1817
1818 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001819 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001820 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001821 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001822 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001823 free(buf);
1824 return LY_EVALID;
1825 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001826 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001827 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 free(buf);
1829 return LY_EVALID;
1830 }
1831 *fracdig = num;
1832 free(buf);
1833
Radek Krejci33090f92020-12-17 20:12:46 +01001834 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001836 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcieccf6602021-02-05 19:42:54 +01001837 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_FRACTION_DIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001838 break;
1839 default:
David Sedlákb3077192019-06-19 10:55:37 +02001840 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001841 return LY_EVALID;
1842 }
1843 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844 return ret;
1845}
1846
Michal Vaskoea5abea2018-09-18 13:10:54 +02001847/**
1848 * @brief Parse the require-instance statement. Substatement of type statement.
1849 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001850 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001851 * @param[in,out] reqinst Value to write to.
1852 * @param[in,out] flags Flags to write to.
1853 * @param[in,out] exts Extension instances to add to.
1854 *
1855 * @return LY_ERR values.
1856 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001857static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001858parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001859 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001860{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001861 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001863 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001864 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001865
1866 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001867 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868 return LY_EVALID;
1869 }
1870 *flags |= LYS_SET_REQINST;
1871
1872 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001873 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874
Radek Krejcif13b87b2020-12-01 22:02:17 +01001875 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001877 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001878 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879 free(buf);
1880 return LY_EVALID;
1881 }
1882 free(buf);
1883
Radek Krejci33090f92020-12-17 20:12:46 +01001884 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001886 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcieccf6602021-02-05 19:42:54 +01001887 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_REQUIRE_INSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888 break;
1889 default:
David Sedlákb3077192019-06-19 10:55:37 +02001890 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891 return LY_EVALID;
1892 }
1893 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001894 return ret;
1895}
1896
Michal Vaskoea5abea2018-09-18 13:10:54 +02001897/**
1898 * @brief Parse the modifier statement. Substatement of type pattern statement.
1899 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001900 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001901 * @param[in,out] pat Value to write to.
1902 * @param[in,out] exts Extension instances to add to.
1903 *
1904 * @return LY_ERR values.
1905 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001907parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001909 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001911 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001912 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001913
Radek Krejcif13b87b2020-12-01 22:02:17 +01001914 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001915 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001916 return LY_EVALID;
1917 }
1918
1919 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001920 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921
Radek Krejcif13b87b2020-12-01 22:02:17 +01001922 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001923 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924 free(buf);
1925 return LY_EVALID;
1926 }
1927 free(buf);
1928
1929 /* replace the value in the dictionary */
1930 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001931 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001933 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934
Radek Krejcif13b87b2020-12-01 22:02:17 +01001935 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
1936 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001937 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001938
Radek Krejci33090f92020-12-17 20:12:46 +01001939 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001940 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001941 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001942 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001943 break;
1944 default:
David Sedlákb3077192019-06-19 10:55:37 +02001945 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946 return LY_EVALID;
1947 }
1948 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949 return ret;
1950}
1951
Michal Vaskoea5abea2018-09-18 13:10:54 +02001952/**
1953 * @brief Parse the pattern statement. Substatement of type statement.
1954 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001955 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001956 * @param[in,out] patterns Restrictions to add to.
1957 *
1958 * @return LY_ERR values.
1959 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001961parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001963 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001964 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001965 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001966 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001967 struct lysp_restr *restr;
1968
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001969 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001970
1971 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001972 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973
1974 /* add special meaning first byte */
1975 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001976 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977 word = buf;
1978 } else {
1979 buf = malloc(word_len + 2);
1980 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001981 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001982 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001983 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02001984 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001985 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
1986 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987
Radek Krejci33090f92020-12-17 20:12:46 +01001988 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001991 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001993 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001994 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001996 case LY_STMT_ERROR_APP_TAG:
Radek Krejcieccf6602021-02-05 19:42:54 +01001997 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001999 case LY_STMT_ERROR_MESSAGE:
Radek Krejcieccf6602021-02-05 19:42:54 +01002000 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002002 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002003 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002004 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002006 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002007 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008 break;
2009 default:
David Sedlákb3077192019-06-19 10:55:37 +02002010 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 return LY_EVALID;
2012 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002013 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 return ret;
2015}
2016
Michal Vaskoea5abea2018-09-18 13:10:54 +02002017/**
2018 * @brief Parse the type statement.
2019 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002020 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002021 * @param[in,out] type Type to wrote to.
2022 *
2023 * @return LY_ERR values.
2024 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002026parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002028 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002030 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002031 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002032 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 struct lysp_type *nest_type;
2034
2035 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002036 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037 return LY_EVALID;
2038 }
2039
2040 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002041 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002042 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002044 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002045 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002046
Radek Krejci33090f92020-12-17 20:12:46 +01002047 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002048 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002049 case LY_STMT_BASE:
Radek Krejci33090f92020-12-17 20:12:46 +01002050 LY_CHECK_RET(parse_text_fields(ctx, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002051 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002053 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002054 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002055 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002057 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002058 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002059 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002060 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002061 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002062 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002063 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002065 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002067 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 return LY_EVALID;
2069 }
2070 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002071 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002072
Radek Krejci33090f92020-12-17 20:12:46 +01002073 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002074 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002075 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002076 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002077 if (type->path) {
2078 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2079 return LY_EVALID;
2080 }
2081
Radek Krejci33090f92020-12-17 20:12:46 +01002082 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002083 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 +02002084 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002085 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002086 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002087 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002089 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002090 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002091 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002093 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002095 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096 return LY_EVALID;
2097 }
2098 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002099 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100
Radek Krejci33090f92020-12-17 20:12:46 +01002101 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002102 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002104 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002105 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002106 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002108 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002109 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002110 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002111 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002113 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002114 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 break;
2116 default:
David Sedlákb3077192019-06-19 10:55:37 +02002117 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002118 return LY_EVALID;
2119 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121 return ret;
2122}
2123
Michal Vaskoea5abea2018-09-18 13:10:54 +02002124/**
2125 * @brief Parse the leaf statement.
2126 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002127 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002128 * @param[in,out] siblings Siblings to add to.
2129 *
2130 * @return LY_ERR values.
2131 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002132LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002133parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002135 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002136 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002137 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002138 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 struct lysp_node_leaf *leaf;
2140
David Sedlák60adc092019-08-06 15:57:02 +02002141 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002142 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002144 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002147 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002148 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002149
2150 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002151 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002153 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002154 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002156 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002157 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002158 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002160 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002161 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002163 case LY_STMT_IF_FEATURE:
Radek Krejcieccf6602021-02-05 19:42:54 +01002164 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002166 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002167 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002169 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002170 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002172 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002173 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002175 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002176 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002178 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002179 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002181 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002182 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002184 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002185 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002187 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002188 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 break;
2190 default:
David Sedlákb3077192019-06-19 10:55:37 +02002191 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 return LY_EVALID;
2193 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002195 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002196checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 /* mandatory substatements */
2198 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002199 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200 return LY_EVALID;
2201 }
2202
2203 return ret;
2204}
2205
Michal Vaskoea5abea2018-09-18 13:10:54 +02002206/**
2207 * @brief Parse the max-elements statement.
2208 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002209 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002210 * @param[in,out] max Value to write to.
2211 * @param[in,out] flags Flags to write to.
2212 * @param[in,out] exts Extension instances to add to.
2213 *
2214 * @return LY_ERR values.
2215 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002216LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002217parse_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 +02002218{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002219 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002221 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002223 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224
2225 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002226 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002227 return LY_EVALID;
2228 }
2229 *flags |= LYS_SET_MAX;
2230
2231 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002232 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233
2234 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002235 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236 free(buf);
2237 return LY_EVALID;
2238 }
2239
Radek Krejci7f9b6512019-09-18 13:11:09 +02002240 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002242 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002243 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002244 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002245 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002246 free(buf);
2247 return LY_EVALID;
2248 }
2249 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002250 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 free(buf);
2252 return LY_EVALID;
2253 }
2254
2255 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002256 } else {
2257 /* unbounded */
2258 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002259 }
2260 free(buf);
2261
Radek Krejci33090f92020-12-17 20:12:46 +01002262 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002264 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcieccf6602021-02-05 19:42:54 +01002265 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002266 break;
2267 default:
David Sedlákb3077192019-06-19 10:55:37 +02002268 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269 return LY_EVALID;
2270 }
2271 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002272 return ret;
2273}
2274
Michal Vaskoea5abea2018-09-18 13:10:54 +02002275/**
2276 * @brief Parse the min-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] min 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_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 +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_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002295 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002296 return LY_EVALID;
2297 }
2298 *flags |= LYS_SET_MIN;
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 || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002304 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002305 free(buf);
2306 return LY_EVALID;
2307 }
2308
2309 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002310 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002312 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002313 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314 free(buf);
2315 return LY_EVALID;
2316 }
2317 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002318 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319 free(buf);
2320 return LY_EVALID;
2321 }
2322 *min = num;
2323 free(buf);
2324
Radek Krejci33090f92020-12-17 20:12:46 +01002325 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002327 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcieccf6602021-02-05 19:42:54 +01002328 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MIN_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002329 break;
2330 default:
David Sedlákb3077192019-06-19 10:55:37 +02002331 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002332 return LY_EVALID;
2333 }
2334 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002335 return ret;
2336}
2337
Michal Vaskoea5abea2018-09-18 13:10:54 +02002338/**
2339 * @brief Parse the ordered-by statement.
2340 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002341 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002342 * @param[in,out] flags Flags to write to.
2343 * @param[in,out] exts Extension instances to add to.
2344 *
2345 * @return LY_ERR values.
2346 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002348parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002350 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002351 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002352 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002353 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002354
2355 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002356 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357 return LY_EVALID;
2358 }
2359
2360 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002361 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362
Radek Krejcif13b87b2020-12-01 22:02:17 +01002363 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002365 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 *flags |= LYS_ORDBY_USER;
2367 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002368 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 free(buf);
2370 return LY_EVALID;
2371 }
2372 free(buf);
2373
Radek Krejci33090f92020-12-17 20:12:46 +01002374 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002376 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcieccf6602021-02-05 19:42:54 +01002377 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_ORDERED_BY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378 break;
2379 default:
David Sedlákb3077192019-06-19 10:55:37 +02002380 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002381 return LY_EVALID;
2382 }
2383 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002384 return ret;
2385}
2386
Michal Vaskoea5abea2018-09-18 13:10:54 +02002387/**
2388 * @brief Parse the leaf-list statement.
2389 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002390 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002391 * @param[in,out] siblings Siblings to add to.
2392 *
2393 * @return LY_ERR values.
2394 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002395LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002396parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002398 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002400 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002401 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002402 struct lysp_node_leaflist *llist;
2403
David Sedlák60adc092019-08-06 15:57:02 +02002404 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002405 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002407 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002410 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002411 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412
2413 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002414 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002416 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002417 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002419 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002420 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci33090f92020-12-17 20:12:46 +01002421 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002423 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002424 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002425 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002426 case LY_STMT_IF_FEATURE:
Radek Krejcieccf6602021-02-05 19:42:54 +01002427 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002429 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002430 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002432 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002433 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002435 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002436 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002438 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002439 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002441 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002442 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002444 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002445 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002447 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002448 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002450 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002451 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002453 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002454 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002456 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002457 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 break;
2459 default:
David Sedlákb3077192019-06-19 10:55:37 +02002460 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002461 return LY_EVALID;
2462 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002464 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002465checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002466 /* mandatory substatements */
2467 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002468 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469 return LY_EVALID;
2470 }
2471
2472 return ret;
2473}
2474
Michal Vaskoea5abea2018-09-18 13:10:54 +02002475/**
2476 * @brief Parse the refine statement.
2477 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002478 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002479 * @param[in,out] refines Refines to add to.
2480 *
2481 * @return LY_ERR values.
2482 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002484parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002485{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002486 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002488 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002489 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490 struct lysp_refine *rf;
2491
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002492 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493
2494 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002495 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002496 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002497 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002498
Radek Krejci33090f92020-12-17 20:12:46 +01002499 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002501 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002502 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002504 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002505 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002507 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002508 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002509 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002510 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002511 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcieccf6602021-02-05 19:42:54 +01002512 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002514 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002515 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002517 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002518 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002520 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002521 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002523 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002524 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002526 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002527 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002529 case LY_STMT_PRESENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002530 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002532 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002533 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 break;
2535 default:
David Sedlákb3077192019-06-19 10:55:37 +02002536 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 return LY_EVALID;
2538 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540 return ret;
2541}
2542
Michal Vaskoea5abea2018-09-18 13:10:54 +02002543/**
2544 * @brief Parse the typedef statement.
2545 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002546 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002547 * @param[in,out] typedefs Typedefs to add to.
2548 *
2549 * @return LY_ERR values.
2550 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002551static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002552parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002553{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002554 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002556 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002557 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 struct lysp_tpdf *tpdf;
2559
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002560 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002561
2562 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002563 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002564 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002565
2566 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002567 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002568 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002569 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002570 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002571 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002573 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002574 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002576 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002577 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002579 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002580 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002582 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002583 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002585 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002586 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002588 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002589 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 break;
2591 default:
David Sedlákb3077192019-06-19 10:55:37 +02002592 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 return LY_EVALID;
2594 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002595 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002596 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002597checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002598 /* mandatory substatements */
2599 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002600 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002601 return LY_EVALID;
2602 }
2603
Radek Krejcibbe09a92018-11-08 09:36:54 +01002604 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002605 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002606 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002607 }
2608
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 return ret;
2610}
2611
Michal Vaskoea5abea2018-09-18 13:10:54 +02002612/**
2613 * @brief Parse the input or output 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] kw Type of this particular keyword
2617 * @param[in,out] inout_p Input/output pointer to write to.
2618 *
2619 * @return LY_ERR values.
2620 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002622parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002623 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002625 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002627 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002629 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002631 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002632 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 return LY_EVALID;
2634 }
2635
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002636 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002637 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2638 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002639 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640
2641 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002642 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002643 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002644 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002645 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002646 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002647 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002648 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002650 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002651 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002653 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002654 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002656 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002657 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002659 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002660 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002662 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002663 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002664 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002665 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002666 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002668 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002669 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002670 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002671 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002672 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002673 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002674 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002675 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002676 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002677 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002678 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002679 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680 break;
2681 default:
David Sedlákb3077192019-06-19 10:55:37 +02002682 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002683 return LY_EVALID;
2684 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002686 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002687
Radek Krejci7fc68292019-06-12 13:51:09 +02002688checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002689 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002690 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2691 return LY_EVALID;
2692 }
2693
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 return ret;
2695}
2696
Michal Vaskoea5abea2018-09-18 13:10:54 +02002697/**
2698 * @brief Parse the action statement.
2699 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002700 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002701 * @param[in,out] actions Actions to add to.
2702 *
2703 * @return LY_ERR values.
2704 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002705LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002706parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002707{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002708 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002709 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002710 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002711 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002712 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002713
Radek Krejci2a9fc652021-01-22 17:44:34 +01002714 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002715
2716 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002717 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002718 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002719 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002720 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721
Radek Krejci33090f92020-12-17 20:12:46 +01002722 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002723 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002724 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002725 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002726 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002727 case LY_STMT_IF_FEATURE:
Radek Krejcieccf6602021-02-05 19:42:54 +01002728 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002730 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002731 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002733 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002734 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 break;
2736
Radek Krejcid6b76452019-09-03 17:03:03 +02002737 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002738 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002740 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002741 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002742 break;
2743
Radek Krejcid6b76452019-09-03 17:03:03 +02002744 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002745 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002746 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002747 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002748 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002750 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002751 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 break;
2753 default:
David Sedlákb3077192019-06-19 10:55:37 +02002754 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755 return LY_EVALID;
2756 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002758 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002759
Radek Krejci2a9fc652021-01-22 17:44:34 +01002760checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002761 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2762 if (!act->input.nodetype) {
2763 act->input.nodetype = LYS_INPUT;
2764 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002765 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002766 }
2767 if (!act->output.nodetype) {
2768 act->output.nodetype = LYS_OUTPUT;
2769 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002770 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002771 }
2772
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773 return ret;
2774}
2775
Michal Vaskoea5abea2018-09-18 13:10:54 +02002776/**
2777 * @brief Parse the notification statement.
2778 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002779 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002780 * @param[in,out] notifs Notifications to add to.
2781 *
2782 * @return LY_ERR values.
2783 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002784LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002785parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002786{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002787 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002789 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002790 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002791 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792
Radek Krejci2a9fc652021-01-22 17:44:34 +01002793 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794
2795 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002796 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002797 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002798 notif->nodetype = LYS_NOTIF;
2799 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800
Radek Krejci2a9fc652021-01-22 17:44:34 +01002801 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002803 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002804 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002805 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002806 case LY_STMT_IF_FEATURE:
Radek Krejcieccf6602021-02-05 19:42:54 +01002807 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002809 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002810 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002812 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002813 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814 break;
2815
Radek Krejcid6b76452019-09-03 17:03:03 +02002816 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002817 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002818 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002819 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002820 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002821 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002822 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002823 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002824 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002825 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002826 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002828 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002829 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002831 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002832 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002833 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002834 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002835 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002837 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002838 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839 break;
2840
Radek Krejcid6b76452019-09-03 17:03:03 +02002841 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002842 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002843 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002845 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002846 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002847 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002848 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002849 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002850 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002851 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002852 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853 break;
2854 default:
David Sedlákb3077192019-06-19 10:55:37 +02002855 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856 return LY_EVALID;
2857 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002859
Michal Vasko7fbc8162018-09-17 10:35:16 +02002860 return ret;
2861}
2862
Michal Vaskoea5abea2018-09-18 13:10:54 +02002863/**
2864 * @brief Parse the grouping statement.
2865 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002866 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002867 * @param[in,out] groupings Groupings to add to.
2868 *
2869 * @return LY_ERR values.
2870 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002871LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002872parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002874 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002875 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002876 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002877 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002878 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002879
Radek Krejci2a9fc652021-01-22 17:44:34 +01002880 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881
2882 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002883 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002884 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002885 grp->nodetype = LYS_GROUPING;
2886 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002887
Radek Krejci2a9fc652021-01-22 17:44:34 +01002888 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002890 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002891 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002893 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002894 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002896 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002897 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 break;
2899
Radek Krejcid6b76452019-09-03 17:03:03 +02002900 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002901 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002902 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002903 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002904 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002906 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002907 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002908 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002909 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002910 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002911 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002912 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002913 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002914 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002915 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002916 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002918 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002919 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002920 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002921 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002922 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 break;
2924
Radek Krejcid6b76452019-09-03 17:03:03 +02002925 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002926 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002927 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002928 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002929 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002930 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002932 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002933 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002934 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002935 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002936 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002937 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002938 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002939 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002940 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941 break;
2942 default:
David Sedlákb3077192019-06-19 10:55:37 +02002943 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944 return LY_EVALID;
2945 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002947
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948 return ret;
2949}
2950
Michal Vaskoea5abea2018-09-18 13:10:54 +02002951/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002952 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002953 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002954 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002955 * @param[in,out] augments Augments to add to.
2956 *
2957 * @return LY_ERR values.
2958 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002959LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002960parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002962 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002963 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002964 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002965 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002966 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967
Radek Krejci2a9fc652021-01-22 17:44:34 +01002968 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002969
2970 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002971 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002972 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002973 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002974 aug->nodetype = LYS_AUGMENT;
2975 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976
Radek Krejci2a9fc652021-01-22 17:44:34 +01002977 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002978 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002979 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002980 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002982 case LY_STMT_IF_FEATURE:
Radek Krejcieccf6602021-02-05 19:42:54 +01002983 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002984 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002985 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002986 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002988 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002989 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002991 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002992 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002993 break;
2994
Radek Krejcid6b76452019-09-03 17:03:03 +02002995 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002996 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02002997 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002998 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01002999 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003001 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003002 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003004 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003005 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003007 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003008 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003010 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003011 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003013 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003014 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003016 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003017 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003019 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003020 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021 break;
3022
Radek Krejcid6b76452019-09-03 17:03:03 +02003023 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003024 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003025 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003027 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003028 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003029 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003031 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003032 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 break;
3034 default:
David Sedlákb3077192019-06-19 10:55:37 +02003035 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003036 return LY_EVALID;
3037 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003038 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003039
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040 return ret;
3041}
3042
Michal Vaskoea5abea2018-09-18 13:10:54 +02003043/**
3044 * @brief Parse the uses statement.
3045 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003046 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003047 * @param[in,out] siblings Siblings to add to.
3048 *
3049 * @return LY_ERR values.
3050 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003051LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003052parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003054 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003055 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003056 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003057 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058 struct lysp_node_uses *uses;
3059
David Sedlák60adc092019-08-06 15:57:02 +02003060 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003061 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003063 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003066 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003067 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003068
3069 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003070 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003072 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003073 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003074 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003075 case LY_STMT_IF_FEATURE:
Radek Krejcieccf6602021-02-05 19:42:54 +01003076 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003078 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003079 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003080 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003081 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003082 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003084 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003085 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 break;
3087
Radek Krejcid6b76452019-09-03 17:03:03 +02003088 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003089 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003090 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003091 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003092 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003094 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003095 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096 break;
3097 default:
David Sedlákb3077192019-06-19 10:55:37 +02003098 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099 return LY_EVALID;
3100 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003102
Michal Vasko7fbc8162018-09-17 10:35:16 +02003103 return ret;
3104}
3105
Michal Vaskoea5abea2018-09-18 13:10:54 +02003106/**
3107 * @brief Parse the case statement.
3108 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003109 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003110 * @param[in,out] siblings Siblings to add to.
3111 *
3112 * @return LY_ERR values.
3113 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003114LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003115parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003116{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003117 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003118 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003119 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003120 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121 struct lysp_node_case *cas;
3122
David Sedlák60adc092019-08-06 15:57:02 +02003123 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003124 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003126 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003127
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003129 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003130 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003131
3132 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003133 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003134 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003135 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003136 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003138 case LY_STMT_IF_FEATURE:
Radek Krejcieccf6602021-02-05 19:42:54 +01003139 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003141 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003142 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003144 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003145 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003147 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003148 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003149 break;
3150
Radek Krejcid6b76452019-09-03 17:03:03 +02003151 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003152 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003153 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003154 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003155 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003157 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003158 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003160 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003161 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003163 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003164 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003166 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003167 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003169 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003170 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003172 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003173 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003175 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003176 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177 break;
3178 default:
David Sedlákb3077192019-06-19 10:55:37 +02003179 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180 return LY_EVALID;
3181 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 return ret;
3184}
3185
Michal Vaskoea5abea2018-09-18 13:10:54 +02003186/**
3187 * @brief Parse the choice statement.
3188 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003189 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003190 * @param[in,out] siblings Siblings to add to.
3191 *
3192 * @return LY_ERR values.
3193 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003194LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003195parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003197 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003198 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003199 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003200 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003201 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202
David Sedlák60adc092019-08-06 15:57:02 +02003203 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003204 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003205 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003206 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003209 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003210 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211
3212 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003213 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003216 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003218 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003219 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003221 case LY_STMT_IF_FEATURE:
Radek Krejcieccf6602021-02-05 19:42:54 +01003222 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003224 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003225 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003226 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003227 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003228 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003230 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003231 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003232 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003233 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003234 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003236 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01003237 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
Michal Vasko7f45cf22020-10-01 12:49:44 +02003238 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003239 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 break;
3241
Radek Krejcid6b76452019-09-03 17:03:03 +02003242 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003243 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003244 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003245 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003246 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003248 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003249 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003250 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003251 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003252 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003253 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003255 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003256 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003258 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003259 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003261 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003262 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003263 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003264 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003265 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003267 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003268 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003269 break;
3270 default:
David Sedlákb3077192019-06-19 10:55:37 +02003271 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272 return LY_EVALID;
3273 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275 return ret;
3276}
3277
Michal Vaskoea5abea2018-09-18 13:10:54 +02003278/**
3279 * @brief Parse the container statement.
3280 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003281 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003282 * @param[in,out] siblings Siblings to add to.
3283 *
3284 * @return LY_ERR values.
3285 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003286LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003287parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288{
3289 LY_ERR ret = 0;
3290 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003291 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003292 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003293 struct lysp_node_container *cont;
3294
David Sedlák60adc092019-08-06 15:57:02 +02003295 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003296 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003298 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003299
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003301 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003302 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003303
3304 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003305 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003307 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003308 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003309 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003310 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003311 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003313 case LY_STMT_IF_FEATURE:
Radek Krejcieccf6602021-02-05 19:42:54 +01003314 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003316 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003317 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003319 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003320 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003322 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003323 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003325 case LY_STMT_PRESENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003326 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003327 break;
3328
Radek Krejcid6b76452019-09-03 17:03:03 +02003329 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003330 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003331 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003332 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003333 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003335 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003336 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003338 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003339 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003341 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003342 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003344 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003345 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003347 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003348 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003350 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003351 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352 break;
3353
Radek Krejcid6b76452019-09-03 17:03:03 +02003354 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003355 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003357 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003358 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003360 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003361 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003362 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003364 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003365 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003367 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003368 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003369 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003371 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003372 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003373 break;
3374 default:
David Sedlákb3077192019-06-19 10:55:37 +02003375 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003376 return LY_EVALID;
3377 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003379
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380 return ret;
3381}
3382
Michal Vaskoea5abea2018-09-18 13:10:54 +02003383/**
3384 * @brief Parse the list statement.
3385 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003386 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003387 * @param[in,out] siblings Siblings to add to.
3388 *
3389 * @return LY_ERR values.
3390 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003391LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003392parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003394 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003396 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003397 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 struct lysp_node_list *list;
3399
David Sedlák60adc092019-08-06 15:57:02 +02003400 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003401 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003403 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003404
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003406 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003407 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408
3409 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003410 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003412 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003413 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003415 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003416 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003418 case LY_STMT_IF_FEATURE:
Radek Krejcieccf6602021-02-05 19:42:54 +01003419 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003421 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003422 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003424 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003425 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003427 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003428 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003430 case LY_STMT_KEY:
Radek Krejci33090f92020-12-17 20:12:46 +01003431 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003433 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003434 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003436 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003437 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003439 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003440 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003442 case LY_STMT_UNIQUE:
Radek Krejci33090f92020-12-17 20:12:46 +01003443 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 break;
3445
Radek Krejcid6b76452019-09-03 17:03:03 +02003446 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003447 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003448 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003449 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003450 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003452 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003453 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003455 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003456 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003458 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003459 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003461 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003462 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003464 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003465 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003467 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003468 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003469 break;
3470
Radek Krejcid6b76452019-09-03 17:03:03 +02003471 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003472 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003474 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003475 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003477 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003478 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003479 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003481 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003482 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003484 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003485 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003486 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003488 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003489 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490 break;
3491 default:
David Sedlákb3077192019-06-19 10:55:37 +02003492 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 return LY_EVALID;
3494 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003495 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003496
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497 return ret;
3498}
3499
Michal Vaskoea5abea2018-09-18 13:10:54 +02003500/**
3501 * @brief Parse the yin-element statement.
3502 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003503 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003504 * @param[in,out] flags Flags to write to.
3505 * @param[in,out] exts Extension instances to add to.
3506 *
3507 * @return LY_ERR values.
3508 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003510parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003512 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003514 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003515 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003516
3517 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003518 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003519 return LY_EVALID;
3520 }
3521
3522 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003523 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003524
Radek Krejcif13b87b2020-12-01 22:02:17 +01003525 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003527 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 *flags |= LYS_YINELEM_FALSE;
3529 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003530 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 free(buf);
3532 return LY_EVALID;
3533 }
3534 free(buf);
3535
Radek Krejci33090f92020-12-17 20:12:46 +01003536 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003538 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcieccf6602021-02-05 19:42:54 +01003539 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003540 LY_CHECK_RET(ret);
3541 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 default:
David Sedlákb3077192019-06-19 10:55:37 +02003543 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 return LY_EVALID;
3545 }
3546 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 return ret;
3548}
3549
Michal Vaskoea5abea2018-09-18 13:10:54 +02003550/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003551 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003552 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003553 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003554 * @param[in,out] argument Value to write to.
3555 * @param[in,out] flags Flags to write to.
3556 * @param[in,out] exts Extension instances to add to.
3557 *
3558 * @return LY_ERR values.
3559 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003561parse_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 +02003562{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003563 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003565 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003566 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567
3568 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003569 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570 return LY_EVALID;
3571 }
3572
3573 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003574 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003575 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576
Radek Krejci33090f92020-12-17 20:12:46 +01003577 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003579 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003580 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003582 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003583 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 break;
3585 default:
David Sedlákb3077192019-06-19 10:55:37 +02003586 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587 return LY_EVALID;
3588 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 return ret;
3591}
3592
Michal Vaskoea5abea2018-09-18 13:10:54 +02003593/**
3594 * @brief Parse the extension statement.
3595 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003596 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003597 * @param[in,out] extensions Extensions to add to.
3598 *
3599 * @return LY_ERR values.
3600 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003602parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003603{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003604 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003605 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003606 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003607 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003608 struct lysp_ext *ex;
3609
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003610 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003611
3612 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003613 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003614 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615
Radek Krejci33090f92020-12-17 20:12:46 +01003616 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003618 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003619 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003621 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003622 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003624 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003625 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003627 case LY_STMT_ARGUMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003628 LY_CHECK_RET(parse_argument(ctx, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003630 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003631 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 break;
3633 default:
David Sedlákb3077192019-06-19 10:55:37 +02003634 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 return LY_EVALID;
3636 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 return ret;
3639}
3640
Michal Vaskoea5abea2018-09-18 13:10:54 +02003641/**
3642 * @brief Parse the deviate statement.
3643 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003644 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003645 * @param[in,out] deviates Deviates to add to.
3646 *
3647 * @return LY_ERR values.
3648 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003649LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003650parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003651{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003652 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003654 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003655 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003656 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 struct lysp_deviate_add *d_add = NULL;
3658 struct lysp_deviate_rpl *d_rpl = NULL;
3659 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003660 const char **d_units = NULL;
3661 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003662 struct lysp_restr **d_musts = NULL;
3663 uint16_t *d_flags = 0;
3664 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665
3666 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003667 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668
Radek Krejcif13b87b2020-12-01 22:02:17 +01003669 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003670 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003671 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003673 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003675 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676 dev_mod = LYS_DEV_DELETE;
3677 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003678 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679 free(buf);
3680 return LY_EVALID;
3681 }
3682 free(buf);
3683
3684 /* create structure */
3685 switch (dev_mod) {
3686 case LYS_DEV_NOT_SUPPORTED:
3687 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003688 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003689 break;
3690 case LYS_DEV_ADD:
3691 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003692 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003693 d = (struct lysp_deviate *)d_add;
3694 d_units = &d_add->units;
3695 d_uniques = &d_add->uniques;
3696 d_dflts = &d_add->dflts;
3697 d_musts = &d_add->musts;
3698 d_flags = &d_add->flags;
3699 d_min = &d_add->min;
3700 d_max = &d_add->max;
3701 break;
3702 case LYS_DEV_REPLACE:
3703 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003704 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003705 d = (struct lysp_deviate *)d_rpl;
3706 d_units = &d_rpl->units;
3707 d_flags = &d_rpl->flags;
3708 d_min = &d_rpl->min;
3709 d_max = &d_rpl->max;
3710 break;
3711 case LYS_DEV_DELETE:
3712 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003713 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714 d = (struct lysp_deviate *)d_del;
3715 d_units = &d_del->units;
3716 d_uniques = &d_del->uniques;
3717 d_dflts = &d_del->dflts;
3718 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719 break;
3720 default:
3721 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003722 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003723 }
3724 d->mod = dev_mod;
3725
3726 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003727 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728
Radek Krejci33090f92020-12-17 20:12:46 +01003729 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003730 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003731 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732 switch (dev_mod) {
3733 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003734 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003735 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736 return LY_EVALID;
3737 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003738 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739 break;
3740 }
3741 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003742 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743 switch (dev_mod) {
3744 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003745 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003746 return LY_EVALID;
3747 case LYS_DEV_REPLACE:
Radek Krejci33090f92020-12-17 20:12:46 +01003748 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003749 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 break;
3751 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003752 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003753 break;
3754 }
3755 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003756 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 switch (dev_mod) {
3758 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003759 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003760 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 return LY_EVALID;
3762 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003763 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764 break;
3765 }
3766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003767 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 switch (dev_mod) {
3769 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003770 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003771 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 return LY_EVALID;
3773 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003774 LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 break;
3776 }
3777 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003778 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 switch (dev_mod) {
3780 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003781 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003782 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 return LY_EVALID;
3784 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003785 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786 break;
3787 }
3788 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003789 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003790 switch (dev_mod) {
3791 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003792 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003793 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794 return LY_EVALID;
3795 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003796 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003797 break;
3798 }
3799 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003800 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 switch (dev_mod) {
3802 case LYS_DEV_NOT_SUPPORTED:
3803 case LYS_DEV_ADD:
3804 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003805 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003806 return LY_EVALID;
3807 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003808 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003809 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003810 return LY_EVALID;
3811 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003813 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003814 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003815 break;
3816 }
3817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003818 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 switch (dev_mod) {
3820 case LYS_DEV_NOT_SUPPORTED:
3821 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003822 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 return LY_EVALID;
3824 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003825 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 break;
3827 }
3828 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003829 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 switch (dev_mod) {
3831 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003832 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003833 return LY_EVALID;
3834 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003835 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003836 break;
3837 }
3838 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003839 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003840 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 break;
3842 default:
David Sedlákb3077192019-06-19 10:55:37 +02003843 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844 return LY_EVALID;
3845 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847 return ret;
3848}
3849
Michal Vaskoea5abea2018-09-18 13:10:54 +02003850/**
3851 * @brief Parse the deviation statement.
3852 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003853 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003854 * @param[in,out] deviations Deviations to add to.
3855 *
3856 * @return LY_ERR values.
3857 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003858LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003859parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003860{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003861 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003863 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003864 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865 struct lysp_deviation *dev;
3866
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003867 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003868
3869 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003870 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003871 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003872 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003873
Radek Krejci33090f92020-12-17 20:12:46 +01003874 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003875 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003876 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003877 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003878 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003879 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003880 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003882 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003883 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003885 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003886 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 break;
3888 default:
David Sedlákb3077192019-06-19 10:55:37 +02003889 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003890 return LY_EVALID;
3891 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003893 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003894checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 /* mandatory substatements */
3896 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003897 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 return LY_EVALID;
3899 }
3900
3901 return ret;
3902}
3903
Michal Vaskoea5abea2018-09-18 13:10:54 +02003904/**
3905 * @brief Parse the feature statement.
3906 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003907 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003908 * @param[in,out] features Features to add to.
3909 *
3910 * @return LY_ERR values.
3911 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003912LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003913parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003915 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003916 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003917 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003918 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919 struct lysp_feature *feat;
3920
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003921 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003922
3923 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003924 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003925 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003926
Radek Krejci33090f92020-12-17 20:12:46 +01003927 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003929 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003930 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003932 case LY_STMT_IF_FEATURE:
Radek Krejcieccf6602021-02-05 19:42:54 +01003933 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003935 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003936 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003938 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003939 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003941 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003942 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003943 break;
3944 default:
David Sedlákb3077192019-06-19 10:55:37 +02003945 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003946 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003948 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 return ret;
3950}
3951
Michal Vaskoea5abea2018-09-18 13:10:54 +02003952/**
3953 * @brief Parse the identity statement.
3954 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003955 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003956 * @param[in,out] identities Identities to add to.
3957 *
3958 * @return LY_ERR values.
3959 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003960LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003961parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003963 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003965 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003966 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967 struct lysp_ident *ident;
3968
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003969 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003970
3971 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003972 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003973 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003974
Radek Krejci33090f92020-12-17 20:12:46 +01003975 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003977 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003978 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003979 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003980 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02003981 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcieccf6602021-02-05 19:42:54 +01003982 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003984 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003985 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003986 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003987 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003988 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003989 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003990 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003991 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02003992 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 +01003993 return LY_EVALID;
3994 }
Radek Krejci33090f92020-12-17 20:12:46 +01003995 LY_CHECK_RET(parse_text_fields(ctx, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003997 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003998 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003999 break;
4000 default:
David Sedlákb3077192019-06-19 10:55:37 +02004001 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004002 return LY_EVALID;
4003 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004004 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004005 return ret;
4006}
4007
Michal Vaskoea5abea2018-09-18 13:10:54 +02004008/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004009 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004010 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004011 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004012 * @param[in,out] mod Module to write to.
4013 *
4014 * @return LY_ERR values.
4015 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004016LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004017parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004018{
4019 LY_ERR ret = 0;
4020 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004021 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004022 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004023 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004024 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004025
Michal Vaskoc3781c32020-10-06 14:04:08 +02004026 mod->is_submod = 0;
4027
4028 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004029 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004030 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004031
Radek Krejci33090f92020-12-17 20:12:46 +01004032 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033
Radek Krejcie3846472018-10-15 15:24:51 +02004034#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004035 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 +02004036
Michal Vasko7fbc8162018-09-17 10:35:16 +02004037 switch (kw) {
4038 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004039 case LY_STMT_NAMESPACE:
4040 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004041 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4042 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004043 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004044 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004045 break;
4046 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004047 case LY_STMT_INCLUDE:
4048 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004049 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004050 break;
4051 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004052 case LY_STMT_ORGANIZATION:
4053 case LY_STMT_CONTACT:
4054 case LY_STMT_DESCRIPTION:
4055 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004056 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 break;
4058
4059 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004060 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004061 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004062 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004063 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004064 case LY_STMT_ANYDATA:
4065 case LY_STMT_ANYXML:
4066 case LY_STMT_AUGMENT:
4067 case LY_STMT_CHOICE:
4068 case LY_STMT_CONTAINER:
4069 case LY_STMT_DEVIATION:
4070 case LY_STMT_EXTENSION:
4071 case LY_STMT_FEATURE:
4072 case LY_STMT_GROUPING:
4073 case LY_STMT_IDENTITY:
4074 case LY_STMT_LEAF:
4075 case LY_STMT_LEAF_LIST:
4076 case LY_STMT_LIST:
4077 case LY_STMT_NOTIFICATION:
4078 case LY_STMT_RPC:
4079 case LY_STMT_TYPEDEF:
4080 case LY_STMT_USES:
4081 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004082 mod_stmt = Y_MOD_BODY;
4083 break;
4084 default:
4085 /* error handled in the next switch */
4086 break;
4087 }
Radek Krejcie3846472018-10-15 15:24:51 +02004088#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004089
Radek Krejcie3846472018-10-15 15:24:51 +02004090 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004091 switch (kw) {
4092 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004093 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004094 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004096 case LY_STMT_NAMESPACE:
Radek Krejci33090f92020-12-17 20:12:46 +01004097 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004099 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +01004100 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004101 break;
4102
4103 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004104 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004105 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004107 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004108 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004109 break;
4110
4111 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004112 case LY_STMT_ORGANIZATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004113 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004115 case LY_STMT_CONTACT:
Radek Krejci33090f92020-12-17 20:12:46 +01004116 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004118 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01004119 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004121 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004122 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004123 break;
4124
4125 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004126 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004127 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004128 break;
4129
4130 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004131 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004132 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004133 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004134 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004135 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004137 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004138 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004140 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004141 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004143 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004144 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004146 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004147 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004149 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004150 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004152 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004153 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004154 break;
4155
Radek Krejcid6b76452019-09-03 17:03:03 +02004156 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004157 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004159 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004160 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004162 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004163 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004164 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004165 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004166 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004168 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004169 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004171 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004172 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004174 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004175 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004177 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004178 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004180 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004181 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004183 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004184 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004185 break;
4186
4187 default:
David Sedlákb3077192019-06-19 10:55:37 +02004188 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004189 return LY_EVALID;
4190 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004191 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004192 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004193
Radek Krejci6d6556c2018-11-08 09:37:45 +01004194checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004195 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004196 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004197 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004198 return LY_EVALID;
4199 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004200 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004201 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004202 }
4203
Radek Krejcie9e987e2018-10-31 12:50:27 +01004204 /* submodules share the namespace with the module names, so there must not be
4205 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004206 dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, mod->mod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004207 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004208 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004209 return LY_EVALID;
4210 }
4211
4212 return ret;
4213}
4214
4215/**
4216 * @brief Parse submodule substatements.
4217 *
4218 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004219 * @param[out] submod Parsed submodule structure.
4220 *
4221 * @return LY_ERR values.
4222 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004223LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004224parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004225{
4226 LY_ERR ret = 0;
4227 char *buf, *word;
4228 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004229 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004230 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4231 struct lysp_submodule *dup;
4232
Michal Vaskoc3781c32020-10-06 14:04:08 +02004233 submod->is_submod = 1;
4234
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004235 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004236 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004237 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004238
Radek Krejci33090f92020-12-17 20:12:46 +01004239 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004240
4241#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004242 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 +01004243
4244 switch (kw) {
4245 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004246 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004247 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004249 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004250 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4251 break;
4252 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004253 case LY_STMT_INCLUDE:
4254 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004255 CHECK_ORDER(Y_MOD_LINKAGE);
4256 break;
4257 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004258 case LY_STMT_ORGANIZATION:
4259 case LY_STMT_CONTACT:
4260 case LY_STMT_DESCRIPTION:
4261 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004262 CHECK_ORDER(Y_MOD_META);
4263 break;
4264
4265 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004266 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004267 CHECK_ORDER(Y_MOD_REVISION);
4268 break;
4269 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004270 case LY_STMT_ANYDATA:
4271 case LY_STMT_ANYXML:
4272 case LY_STMT_AUGMENT:
4273 case LY_STMT_CHOICE:
4274 case LY_STMT_CONTAINER:
4275 case LY_STMT_DEVIATION:
4276 case LY_STMT_EXTENSION:
4277 case LY_STMT_FEATURE:
4278 case LY_STMT_GROUPING:
4279 case LY_STMT_IDENTITY:
4280 case LY_STMT_LEAF:
4281 case LY_STMT_LEAF_LIST:
4282 case LY_STMT_LIST:
4283 case LY_STMT_NOTIFICATION:
4284 case LY_STMT_RPC:
4285 case LY_STMT_TYPEDEF:
4286 case LY_STMT_USES:
4287 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004288 mod_stmt = Y_MOD_BODY;
4289 break;
4290 default:
4291 /* error handled in the next switch */
4292 break;
4293 }
4294#undef CHECK_ORDER
4295
4296 prev_kw = kw;
4297 switch (kw) {
4298 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004299 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004300 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004301 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004302 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004303 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004304 break;
4305
4306 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004307 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004308 if (submod->version == LYS_VERSION_1_1) {
4309 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4310 submod->name);
4311 }
Radek Krejci33090f92020-12-17 20:12:46 +01004312 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004313 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004314 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004315 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004316 break;
4317
4318 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004319 case LY_STMT_ORGANIZATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004320 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004322 case LY_STMT_CONTACT:
Radek Krejci33090f92020-12-17 20:12:46 +01004323 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004325 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01004326 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004328 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004329 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004330 break;
4331
4332 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004333 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004334 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004335 break;
4336
4337 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004339 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004340 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004341 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004342 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004344 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004345 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004347 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004348 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004350 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004351 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004353 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004354 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004356 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004357 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004359 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004360 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004361 break;
4362
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004364 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004367 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004370 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004373 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004376 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004379 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004381 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004382 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004385 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004388 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004390 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004391 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004392 break;
4393
4394 default:
David Sedlákb3077192019-06-19 10:55:37 +02004395 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004396 return LY_EVALID;
4397 }
4398 }
4399 LY_CHECK_RET(ret);
4400
4401checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004402 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004403 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004404 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004405 return LY_EVALID;
4406 }
4407
4408 /* submodules share the namespace with the module names, so there must not be
4409 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004410 dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, submod->name, NULL);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004411 /* main modules may have different revisions */
4412 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004413 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004414 return LY_EVALID;
4415 }
4416
Michal Vasko7fbc8162018-09-17 10:35:16 +02004417 return ret;
4418}
4419
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004420/**
4421 * @brief Skip any redundant characters, namely whitespaces and comments.
4422 *
4423 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004424 * @return LY_SUCCESS on success.
4425 * @return LY_EVALID on invalid comment.
4426 */
4427static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004428skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004429{
4430 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004431 while (ctx->in->current[0]) {
4432 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004433 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004434 ly_in_skip(ctx->in, 2);
4435 LY_CHECK_RET(skip_comment(ctx, 1));
4436 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004437 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004438 ly_in_skip(ctx->in, 2);
4439 LY_CHECK_RET(skip_comment(ctx, 2));
4440 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004441 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004442 if (ctx->in->current[0] == '\n') {
4443 LY_IN_NEW_LINE(ctx->in);
4444 }
Radek Krejci33090f92020-12-17 20:12:46 +01004445 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004446 } else {
4447 break;
4448 }
4449 }
4450
4451 return LY_SUCCESS;
4452}
4453
Radek Krejcid4557c62018-09-17 11:42:09 +02004454LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004455yang_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 +02004456 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004457{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004458 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004459 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004460 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004461 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004462 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004463
David Sedlák1b623122019-08-05 15:27:49 +02004464 /* create context */
4465 *context = calloc(1, sizeof **context);
4466 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004467 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004468 (*context)->unres = main_ctx->unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004469 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004470
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004471 mod_p = calloc(1, sizeof *mod_p);
4472 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4473 mod_p->mod = main_ctx->parsed_mod->mod;
4474 mod_p->parsing = 1;
4475 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4476
Radek Krejciddace2c2021-01-08 11:30:56 +01004477 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004478
David Sedlák1b623122019-08-05 15:27:49 +02004479 /* map the typedefs and groupings list from main context to the submodule's context */
4480 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4481 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4482
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004483 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004484 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004485 LY_CHECK_GOTO(ret, cleanup);
4486
Michal Vasko7fbc8162018-09-17 10:35:16 +02004487 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004488 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004489 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004490
Radek Krejcid6b76452019-09-03 17:03:03 +02004491 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004492 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004493 ret = LY_EINVAL;
4494 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004495 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004496 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004497 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004498 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004499 }
4500
Michal Vasko7fbc8162018-09-17 10:35:16 +02004501 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004502 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004503 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004504
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004505 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004506 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004507 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004508 if (in->current[0]) {
4509 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004510 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004511 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004512 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004513
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004514 mod_p->parsing = 0;
4515 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004516
Radek Krejcibbe09a92018-11-08 09:36:54 +01004517cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004518 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004519 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004520 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004521 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004522 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004523 }
4524
4525 return ret;
4526}
4527
4528LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004529yang_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 +01004530{
4531 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004532 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004533 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004534 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004535 struct lysp_module *mod_p = NULL;
4536
David Sedlák1b623122019-08-05 15:27:49 +02004537 /* create context */
4538 *context = calloc(1, sizeof **context);
4539 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004540 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004541 (*context)->unres = unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004542 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004543
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004544 mod_p = calloc(1, sizeof *mod_p);
4545 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4546 mod_p->mod = mod;
4547 mod_p->parsing = 1;
4548 (*context)->parsed_mod = mod_p;
4549
Radek Krejciddace2c2021-01-08 11:30:56 +01004550 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004551
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
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004556 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004557 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004558 LY_CHECK_GOTO(ret, cleanup);
4559
Radek Krejcid6b76452019-09-03 17:03:03 +02004560 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004561 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 +01004562 ret = LY_EINVAL;
4563 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004564 } else if (kw != LY_STMT_MODULE) {
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 Krejci0bcdaed2019-01-10 10:21:34 +01004567 goto cleanup;
4568 }
4569
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004570 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004571 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004572 LY_CHECK_GOTO(ret, cleanup);
4573
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_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004579 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004580 goto cleanup;
4581 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004582
4583 mod_p->parsing = 0;
4584 mod->parsed = mod_p;
4585
4586cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004587 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004588 if (ret) {
4589 lysp_module_free(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 Krejcibbe09a92018-11-08 09:36:54 +01004592 }
4593
Michal Vasko7fbc8162018-09-17 10:35:16 +02004594 return ret;
4595}