blob: da537a044747c48b016006db67ca78e21b4b9e88 [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 Krejci33090f92020-12-17 20:12:46 +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);
86LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_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;
156 } else {
157 /* note - even the multibyte character is count as 1 */
158 ++ctx->indent;
159 }
160
Radek Krejci44ceedc2018-10-02 15:54:31 +0200161 /* check character validity */
162 switch (arg) {
163 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100164 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200165 break;
166 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100167 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200168 break;
169 case Y_STR_ARG:
170 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100171 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200172 break;
173 }
174
Michal Vasko7fbc8162018-09-17 10:35:16 +0200175 if (word_b && *word_b) {
176 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100177 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200178 return LY_EMEM;
179 }
180
181 /* in case of realloc */
182 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200183 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200184 /* first time we need a buffer, copy everything read up to now */
185 if (*word_len) {
186 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200187 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200188 *buf_len = *word_len;
189 memcpy(*word_b, *word_p, *word_len);
190 }
191
192 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100193 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200194 return LY_EMEM;
195 }
196
197 /* in case of realloc */
198 *word_p = *word_b;
199 } else {
200 /* just remember the first character pointer */
201 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100202 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200203 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200204 /* ... and update the word's length */
205 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100206 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200207 }
208
209 return LY_SUCCESS;
210}
211
Michal Vaskoea5abea2018-09-18 13:10:54 +0200212/**
213 * @brief Skip YANG comment in data.
214 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200215 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200216 * @param[in] comment Type of the comment to process:
217 * 1 for a one-line comment,
218 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200219 * @return LY_ERR values.
220 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200221LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100222skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200223{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100224 /* internal statuses: */
225#define COMMENT_NO 0 /* comment ended */
226#define COMMENT_LINE 1 /* in line comment */
227#define COMMENT_BLOCK 2 /* in block comment */
228#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
229
Radek Krejci33090f92020-12-17 20:12:46 +0100230 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200231 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100232 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100233 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100234 comment = COMMENT_NO;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200235 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200236 }
237 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100238 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100239 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100240 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100241 } else if (ctx->in->current[0] == '\n') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200242 ++ctx->line;
243 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200244 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100245 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100246 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100247 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100248 } else if (ctx->in->current[0] != '*') {
249 if (ctx->in->current[0] == '\n') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200250 ++ctx->line;
251 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100252 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200253 }
254 break;
255 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200256 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200257 }
258
Radek Krejci33090f92020-12-17 20:12:46 +0100259 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200260 ctx->indent = 0;
261 } else {
262 ++ctx->indent;
263 }
Radek Krejci33090f92020-12-17 20:12:46 +0100264 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200265 }
266
Radek Krejci33090f92020-12-17 20:12:46 +0100267 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200268 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200269 return LY_EVALID;
270 }
271
272 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100273
274#undef COMMENT_NO
275#undef COMMENT_LINE
276#undef COMMENT_BLOCK
277#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200278}
279
Michal Vaskoea5abea2018-09-18 13:10:54 +0200280/**
281 * @brief Read a quoted string from data.
282 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200283 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200284 * @param[in] arg Type of YANG keyword argument expected.
285 * @param[out] word_p Pointer to the read quoted string.
286 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
287 * set to NULL. Otherwise equal to \p word_p.
288 * @param[out] word_len Length of the read quoted string.
289 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
290 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
291 * indenation in the final quoted string.
292 *
293 * @return LY_ERR values.
294 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200295static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100296read_qstring(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200297 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200298{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100299 /* string parsing status: */
300#define STRING_ENDED 0 /* string ended */
301#define STRING_SINGLE_QUOTED 1 /* string with ' */
302#define STRING_DOUBLE_QUOTED 2 /* string with " */
303#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
304#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
305#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
306
Radek Krejci1deb5be2020-08-26 16:43:36 +0200307 uint8_t string;
308 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200309 ly_bool need_buf = 0;
310 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200311 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200312 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200313
Radek Krejci33090f92020-12-17 20:12:46 +0100314 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100315 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200316 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200317 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100318 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100319 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200320 }
Radek Krejci33090f92020-12-17 20:12:46 +0100321 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200322
Radek Krejci33090f92020-12-17 20:12:46 +0100323 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200324 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100325 case STRING_SINGLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100326 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200327 case '\'':
328 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100329 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100330 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200331 break;
332 default:
333 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100334 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 +0200335 break;
336 }
337 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100338 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100339 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200340 case '\"':
341 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100342 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100343 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200344 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200345 break;
346 case '\\':
347 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100348 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200349
350 /* the backslash sequence is substituted, so we will need a buffer to store the result */
351 need_buf = 1;
352
353 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100354 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200355
356 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
357 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
358 trailing_ws = 0;
359
360 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
361 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200362 break;
363 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200364 if (current_indent < block_indent) {
365 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100366 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200367 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100368 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100369 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 +0100370 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200371 }
372 break;
373 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200374 if (current_indent < block_indent) {
375 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100376 current_indent += Y_TAB_SPACES;
377 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200378 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200379 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100380 c = ctx->in->current;
381 ctx->in->current = " ";
382 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
383 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100384 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200385 }
Radek Krejci33090f92020-12-17 20:12:46 +0100386 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200387 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100388 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100389 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 +0100390 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200391 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100392 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200393 }
394 break;
395 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200396 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200397 /* we will be removing the indents so we need our own buffer */
398 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200399
400 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200401 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200402
Radek Krejciff13cd12019-10-25 15:34:24 +0200403 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200404 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200405 }
406
407 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100408 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 +0200409
410 /* maintain line number */
411 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200412
413 /* reset context indentation counter for possible string after this one */
414 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200415 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200416 break;
417 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200418 /* first non-whitespace character, stop eating indentation */
419 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200420
421 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100422 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 +0200423 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200424 break;
425 }
426 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100427 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200428 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100429 c = ctx->in->current;
430 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200431 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100432 ctx->in->current = "\n";
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':
461 ++ctx->line;
Radek Krejcif13b87b2020-12-01 22:02:17 +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':
476 ++ctx->line;
Radek Krejcif13b87b2020-12-01 22:02:17 +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 }
594 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200595 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200596
597 /* track line numbers */
598 ++ctx->line;
599
Radek Krejci33090f92020-12-17 20:12:46 +0100600 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200601 break;
602 case ';':
603 case '{':
604 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
605 /* word is finished */
606 goto str_end;
607 }
608
Radek Krejci33090f92020-12-17 20:12:46 +0100609 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200610 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200611 case '}':
612 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100613 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200614 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200615 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200616 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100617 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 +0200618 break;
619 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200620 }
621
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200622 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200623 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200624 return LY_EVALID;
625
Michal Vasko7fbc8162018-09-17 10:35:16 +0200626str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200627 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200628 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200629 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200630 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200631 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200632 *word_p = *word_b;
633 }
634
635 return LY_SUCCESS;
636}
637
Michal Vaskoea5abea2018-09-18 13:10:54 +0200638/**
639 * @brief Get another YANG keyword from the raw data.
640 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200641 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200642 * @param[out] kw YANG keyword read.
643 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
644 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
645 *
646 * @return LY_ERR values.
647 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200648LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100649get_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 +0200650{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200651 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200652 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200653 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200654
655 if (word_p) {
656 *word_p = NULL;
657 *word_len = 0;
658 }
659
660 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100661 while (ctx->in->current[0]) {
662 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200663 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100664 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200665 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100666 MOVE_INPUT(ctx, 2);
667 LY_CHECK_RET(skip_comment(ctx, 1));
668 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200669 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100670 MOVE_INPUT(ctx, 2);
671 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200673 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200674 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200675 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200676 }
Radek Krejci13028282019-06-11 14:56:48 +0200677 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200678 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200679 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200680 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200681 ctx->indent = 0;
682 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200683 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200684 /* skip whitespaces (optsep) */
685 ++ctx->indent;
686 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200687 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100689 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200690 break;
691 default:
692 /* either a keyword start or an invalid character */
693 goto keyword_start;
694 }
695
Radek Krejci33090f92020-12-17 20:12:46 +0100696 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200697 }
698
699keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100700 word_start = ctx->in->current;
701 *kw = lysp_match_kw(ctx, ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200702
Michal Vasko69730152020-10-09 16:30:07 +0200703 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 +0200704 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200705 }
706
Radek Krejcid6b76452019-09-03 17:03:03 +0200707 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200708 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100709 switch (ctx->in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200710 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200711 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200712 case ' ':
713 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200714 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200715 case ':':
716 /* keyword is not actually a keyword, but prefix of an extension.
717 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
718 * and we will be checking the keyword (extension instance) itself */
719 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100720 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200721 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200722 case '{':
723 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200724 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200725 break;
726 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100727 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200728 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100729 MOVE_INPUT(ctx, 1);
730 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200731 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200732 return LY_EVALID;
733 }
734 } else {
735 /* still can be an extension */
736 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200737extension:
Radek Krejci33090f92020-12-17 20:12:46 +0100738 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') && (ctx->in->current[0] != '\n') &&
739 (ctx->in->current[0] != '{') && (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200740 uint32_t c = 0;
741
Radek Krejci33090f92020-12-17 20:12:46 +0100742 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
743 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200744 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200745 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200746 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100747 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 }
Radek Krejci33090f92020-12-17 20:12:46 +0100749 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200750 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200751 return LY_EVALID;
752 }
753
754 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200755 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100756 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200757 return LY_EVALID;
758 }
759
Radek Krejcid6b76452019-09-03 17:03:03 +0200760 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200761 }
Radek Krejci626df482018-10-11 15:06:31 +0200762success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200763 if (word_p) {
764 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100765 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200766 }
767
768 return LY_SUCCESS;
769}
770
Michal Vaskoea5abea2018-09-18 13:10:54 +0200771/**
772 * @brief Parse extension instance substatements.
773 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200774 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200775 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200776 * @param[in] word Extension instance substatement name (keyword).
777 * @param[in] word_len Extension instance substatement name length.
778 * @param[in,out] child Children of this extension instance to add to.
779 *
780 * @return LY_ERR values.
781 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200782static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100783parse_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 +0200784 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200785{
786 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100787 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200788 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200789 struct lysp_stmt *stmt, *par_child;
790
791 stmt = calloc(1, sizeof *stmt);
792 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
793
Radek Krejcibb9b1982019-04-08 14:24:59 +0200794 /* insert into parent statements */
795 if (!*child) {
796 *child = stmt;
797 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200798 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200799 par_child->next = stmt;
800 }
801
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200802 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Radek Krejci335332a2019-09-05 13:03:35 +0200803 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200804
805 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100806 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200807
Radek Krejci0ae092d2018-09-20 16:43:19 +0200808 if (word) {
809 if (buf) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200810 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200811 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200812 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200813 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200814 }
815
Radek Krejci33090f92020-12-17 20:12:46 +0100816 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, ) {
817 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200819 return ret;
820}
821
Michal Vaskoea5abea2018-09-18 13:10:54 +0200822/**
823 * @brief Parse extension instance.
824 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200825 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200826 * @param[in] ext_name Extension instance substatement name (keyword).
827 * @param[in] ext_name_len Extension instance substatement name length.
828 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
829 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
830 * @param[in,out] exts Extension instances to add to.
831 *
832 * @return LY_ERR values.
833 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200834static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100835parse_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 +0200836 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100838 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200839 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200840 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200842 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200844 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845
846 /* store name and insubstmt info */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200847 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200848 e->insubstmt = insubstmt;
849 e->insubstmt_index = insubstmt_index;
850
851 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100852 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200853
Radek Krejci0ae092d2018-09-20 16:43:19 +0200854 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200855 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200856 }
857
Radek Krejci33090f92020-12-17 20:12:46 +0100858 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
859 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200860 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200861 return ret;
862}
863
Michal Vaskoea5abea2018-09-18 13:10:54 +0200864/**
865 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
866 * description, etc...
867 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200868 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200869 * @param[in] substmt Type of this substatement.
870 * @param[in] substmt_index Index of this substatement.
871 * @param[in,out] value Place to store the parsed value.
872 * @param[in] arg Type of the YANG keyword argument (of the value).
873 * @param[in,out] exts Extension instances to add to.
874 *
875 * @return LY_ERR values.
876 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200877static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100878parse_text_field(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200879 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200880{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100881 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200882 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200883 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200884 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200885
886 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200887 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200888 return LY_EVALID;
889 }
890
891 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100892 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200893
894 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200895 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200896
Radek Krejci33090f92020-12-17 20:12:46 +0100897 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200898 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200899 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100900 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200901 break;
902 default:
David Sedlákb3077192019-06-19 10:55:37 +0200903 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200904 return LY_EVALID;
905 }
906 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200907 return ret;
908}
909
Michal Vaskoea5abea2018-09-18 13:10:54 +0200910/**
911 * @brief Parse the yang-version statement.
912 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200913 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100914 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200915 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200916 *
917 * @return LY_ERR values.
918 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200919static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100920parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200921{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100922 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200923 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200924 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200925 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200926
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100927 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200928 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200929 return LY_EVALID;
930 }
931
932 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100933 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200934
Radek Krejci96e48da2020-09-04 13:18:06 +0200935 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100936 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100937 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100938 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200940 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200941 free(buf);
942 return LY_EVALID;
943 }
944 free(buf);
945
Radek Krejci33090f92020-12-17 20:12:46 +0100946 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200947 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200948 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100949 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200950 break;
951 default:
David Sedlákb3077192019-06-19 10:55:37 +0200952 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200953 return LY_EVALID;
954 }
955 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200956 return ret;
957}
958
Michal Vaskoea5abea2018-09-18 13:10:54 +0200959/**
960 * @brief Parse the belongs-to statement.
961 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200962 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200963 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
964 * @param[in,out] exts Extension instances to add to.
965 *
966 * @return LY_ERR values.
967 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100969parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200970{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100971 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200972 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200973 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200974 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200975
Michal Vaskoc3781c32020-10-06 14:04:08 +0200976 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200977 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200978 return LY_EVALID;
979 }
980
Michal Vaskoc3781c32020-10-06 14:04:08 +0200981 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +0100982 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200983 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +0200984 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 +0200985 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +0200986 free(buf);
987 return LY_EVALID;
988 }
989 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200990
Radek Krejci33090f92020-12-17 20:12:46 +0100991 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200993 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +0100994 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200996 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100997 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200998 break;
999 default:
David Sedlákb3077192019-06-19 10:55:37 +02001000 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001 return LY_EVALID;
1002 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001003 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001004 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001005checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006 /* mandatory substatements */
1007 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001008 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009 return LY_EVALID;
1010 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011 return ret;
1012}
1013
Michal Vaskoea5abea2018-09-18 13:10:54 +02001014/**
1015 * @brief Parse the revision-date statement.
1016 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001017 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001018 * @param[in,out] rev Array to store the parsed value in.
1019 * @param[in,out] exts Extension instances to add to.
1020 *
1021 * @return LY_ERR values.
1022 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001024parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001026 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001027 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001028 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001029 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001030
1031 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001032 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001033 return LY_EVALID;
1034 }
1035
1036 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001037 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001038
1039 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001040 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001041 free(buf);
1042 return LY_EVALID;
1043 }
1044
1045 /* store value and spend buf if allocated */
1046 strncpy(rev, word, word_len);
1047 free(buf);
1048
Radek Krejci33090f92020-12-17 20:12:46 +01001049 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001050 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001051 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001052 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053 break;
1054 default:
David Sedlákb3077192019-06-19 10:55:37 +02001055 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056 return LY_EVALID;
1057 }
1058 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001059 return ret;
1060}
1061
Michal Vaskoea5abea2018-09-18 13:10:54 +02001062/**
1063 * @brief Parse the include statement.
1064 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001065 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001066 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001067 * @param[in,out] includes Parsed includes to add to.
1068 *
1069 * @return LY_ERR values.
1070 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001072parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073{
Radek Krejcid33273d2018-10-25 14:55:52 +02001074 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001076 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001077 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078 struct lysp_include *inc;
1079
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001080 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081
1082 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001083 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001084
Radek Krejci011e4aa2020-09-04 15:22:31 +02001085 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001086
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001087 /* submodules share the namespace with the module names, so there must not be
1088 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001089 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001090 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001091 return LY_EVALID;
1092 }
1093
Radek Krejci33090f92020-12-17 20:12:46 +01001094 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001095 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001096 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001097 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci33090f92020-12-17 20:12:46 +01001098 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 +02001099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001100 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001101 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci33090f92020-12-17 20:12:46 +01001102 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 +02001103 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001104 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001105 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001106 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001107 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001108 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001109 break;
1110 default:
David Sedlákb3077192019-06-19 10:55:37 +02001111 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112 return LY_EVALID;
1113 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001114 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001115 return ret;
1116}
1117
Michal Vaskoea5abea2018-09-18 13:10:54 +02001118/**
1119 * @brief Parse the import statement.
1120 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001121 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001122 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001123 * @param[in,out] imports Parsed imports to add to.
1124 *
1125 * @return LY_ERR values.
1126 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001128parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001129{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001130 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001131 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001132 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001133 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001134 struct lysp_import *imp;
1135
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001136 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137
1138 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001139 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001140 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141
Radek Krejci33090f92020-12-17 20:12:46 +01001142 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001144 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +01001145 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 +01001146 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 +02001147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001148 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001149 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci33090f92020-12-17 20:12:46 +01001150 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 +02001151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001152 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001153 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci33090f92020-12-17 20:12:46 +01001154 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 +02001155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001156 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001157 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001159 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001160 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161 break;
1162 default:
David Sedlákb3077192019-06-19 10:55:37 +02001163 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164 return LY_EVALID;
1165 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001166 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001167 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001168checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001170 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001171
1172 return ret;
1173}
1174
Michal Vaskoea5abea2018-09-18 13:10:54 +02001175/**
1176 * @brief Parse the revision statement.
1177 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001178 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001179 * @param[in,out] revs Parsed revisions to add to.
1180 *
1181 * @return LY_ERR values.
1182 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001184parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001186 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001187 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001188 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001189 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190 struct lysp_revision *rev;
1191
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001192 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001193
1194 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001195 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196
1197 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001198 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001199 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200 return LY_EVALID;
1201 }
1202
Radek Krejcib7db73a2018-10-24 14:18:40 +02001203 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001204 free(buf);
1205
Radek Krejci33090f92020-12-17 20:12:46 +01001206 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001207 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001208 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001209 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 +02001210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001211 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001212 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 +02001213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001214 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001215 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001216 break;
1217 default:
David Sedlákb3077192019-06-19 10:55:37 +02001218 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219 return LY_EVALID;
1220 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001222 return ret;
1223}
1224
Michal Vaskoea5abea2018-09-18 13:10:54 +02001225/**
1226 * @brief Parse a generic text field that can have more instances such as base.
1227 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001228 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001229 * @param[in] substmt Type of this substatement.
1230 * @param[in,out] texts Parsed values to add to.
1231 * @param[in] arg Type of the expected argument.
1232 * @param[in,out] exts Extension instances to add to.
1233 *
1234 * @return LY_ERR values.
1235 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001237parse_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 +02001238 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001240 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001242 const char **item;
1243 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001244 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245
1246 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001247 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248
1249 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001250 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251
Radek Krejci011e4aa2020-09-04 15:22:31 +02001252 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Radek Krejci33090f92020-12-17 20:12:46 +01001253 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001255 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001256 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 break;
1258 default:
David Sedlákb3077192019-06-19 10:55:37 +02001259 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001260 return LY_EVALID;
1261 }
1262 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263 return ret;
1264}
1265
Michal Vaskoea5abea2018-09-18 13:10:54 +02001266/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001267 * @brief Parse a generic text field that can have more instances such as base.
1268 *
1269 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001270 * @param[in] substmt Type of this substatement.
1271 * @param[in,out] qnames Parsed qnames to add to.
1272 * @param[in] arg Type of the expected argument.
1273 * @param[in,out] exts Extension instances to add to.
1274 *
1275 * @return LY_ERR values.
1276 */
1277static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001278parse_qnames(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001279 enum yang_arg arg, struct lysp_ext_instance **exts)
1280{
1281 LY_ERR ret = LY_SUCCESS;
1282 char *buf, *word;
1283 struct lysp_qname *item;
1284 size_t word_len;
1285 enum ly_stmt kw;
1286
1287 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001288 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001289
1290 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001291 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001292
1293 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001294 item->mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001295 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001296 switch (kw) {
1297 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001298 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001299 break;
1300 default:
1301 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
1302 return LY_EVALID;
1303 }
1304 }
1305 return ret;
1306}
1307
1308/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001309 * @brief Parse the config statement.
1310 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001311 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001312 * @param[in,out] flags Flags to add to.
1313 * @param[in,out] exts Extension instances to add to.
1314 *
1315 * @return LY_ERR values.
1316 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001317static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001318parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001320 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001321 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001322 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001323 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324
1325 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001326 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327 return LY_EVALID;
1328 }
1329
1330 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001331 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332
Radek Krejcif13b87b2020-12-01 22:02:17 +01001333 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001335 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336 *flags |= LYS_CONFIG_R;
1337 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001338 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 free(buf);
1340 return LY_EVALID;
1341 }
1342 free(buf);
1343
Radek Krejci33090f92020-12-17 20:12:46 +01001344 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001346 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001347 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 break;
1349 default:
David Sedlákb3077192019-06-19 10:55:37 +02001350 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001351 return LY_EVALID;
1352 }
1353 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001354 return ret;
1355}
1356
Michal Vaskoea5abea2018-09-18 13:10:54 +02001357/**
1358 * @brief Parse the mandatory statement.
1359 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001360 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001361 * @param[in,out] flags Flags to add to.
1362 * @param[in,out] exts Extension instances to add to.
1363 *
1364 * @return LY_ERR values.
1365 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001367parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001369 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001370 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001371 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001372 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001373
1374 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001375 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001376 return LY_EVALID;
1377 }
1378
1379 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001380 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381
Radek Krejcif13b87b2020-12-01 22:02:17 +01001382 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001384 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001385 *flags |= LYS_MAND_FALSE;
1386 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001387 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388 free(buf);
1389 return LY_EVALID;
1390 }
1391 free(buf);
1392
Radek Krejci33090f92020-12-17 20:12:46 +01001393 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001394 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001395 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001396 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001397 break;
1398 default:
David Sedlákb3077192019-06-19 10:55:37 +02001399 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001400 return LY_EVALID;
1401 }
1402 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403 return ret;
1404}
1405
Michal Vaskoea5abea2018-09-18 13:10:54 +02001406/**
1407 * @brief Parse a restriction such as range or length.
1408 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001409 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001410 * @param[in] restr_kw Type of this particular restriction.
1411 * @param[in,out] exts Extension instances to add to.
1412 *
1413 * @return LY_ERR values.
1414 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001416parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001418 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001419 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001420 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001421 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001422
1423 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001424 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001425
Michal Vaskob36053d2020-03-26 15:49:30 +01001426 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001427 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001428 restr->arg.mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001429 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001430 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001431 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001432 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 +02001433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001434 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001435 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 +02001436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001437 case LY_STMT_ERROR_APP_TAG:
Radek Krejci33090f92020-12-17 20:12:46 +01001438 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001440 case LY_STMT_ERROR_MESSAGE:
Radek Krejci33090f92020-12-17 20:12:46 +01001441 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001443 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001444 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001445 break;
1446 default:
David Sedlákb3077192019-06-19 10:55:37 +02001447 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448 return LY_EVALID;
1449 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001451 return ret;
1452}
1453
Michal Vaskoea5abea2018-09-18 13:10:54 +02001454/**
1455 * @brief Parse a restriction that can have more instances such as must.
1456 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001457 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001458 * @param[in] restr_kw Type of this particular restriction.
1459 * @param[in,out] restrs Restrictions to add to.
1460 *
1461 * @return LY_ERR values.
1462 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001464parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465{
1466 struct lysp_restr *restr;
1467
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001468 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001469 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001470}
1471
Michal Vaskoea5abea2018-09-18 13:10:54 +02001472/**
1473 * @brief Parse the status statement.
1474 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001475 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001476 * @param[in,out] flags Flags to add to.
1477 * @param[in,out] exts Extension instances to add to.
1478 *
1479 * @return LY_ERR values.
1480 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001482parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001484 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001485 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001486 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001487 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488
1489 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001490 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491 return LY_EVALID;
1492 }
1493
1494 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001495 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496
Radek Krejcif13b87b2020-12-01 22:02:17 +01001497 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001499 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001501 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001502 *flags |= LYS_STATUS_OBSLT;
1503 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001504 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505 free(buf);
1506 return LY_EVALID;
1507 }
1508 free(buf);
1509
Radek Krejci33090f92020-12-17 20:12:46 +01001510 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001511 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001512 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001513 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 break;
1515 default:
David Sedlákb3077192019-06-19 10:55:37 +02001516 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 return LY_EVALID;
1518 }
1519 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 return ret;
1521}
1522
Michal Vaskoea5abea2018-09-18 13:10:54 +02001523/**
1524 * @brief Parse the when statement.
1525 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001526 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001527 * @param[in,out] when_p When pointer to parse to.
1528 *
1529 * @return LY_ERR values.
1530 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001531LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001532parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001533{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001534 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001536 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001537 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538 struct lysp_when *when;
1539
1540 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001541 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001542 return LY_EVALID;
1543 }
1544
1545 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001546 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001547 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001548
1549 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001550 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001551 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001552 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553
Radek Krejci33090f92020-12-17 20:12:46 +01001554 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001556 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001557 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 +02001558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001559 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001560 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 +02001561 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001562 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001563 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564 break;
1565 default:
David Sedlákb3077192019-06-19 10:55:37 +02001566 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 return LY_EVALID;
1568 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001570 return ret;
1571}
1572
Michal Vaskoea5abea2018-09-18 13:10:54 +02001573/**
1574 * @brief Parse the anydata or anyxml statement.
1575 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001576 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001577 * @param[in] kw Type of this particular keyword.
1578 * @param[in,out] siblings Siblings to add to.
1579 *
1580 * @return LY_ERR values.
1581 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001582LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001583parse_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 +02001584{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001585 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001587 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588 struct lysp_node_anydata *any;
1589
David Sedlák60adc092019-08-06 15:57:02 +02001590 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001591 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001592
Radek Krejcid6b76452019-09-03 17:03:03 +02001593 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001594 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001595
Michal Vasko7fbc8162018-09-17 10:35:16 +02001596 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001597 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001598 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001599
1600 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01001601 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001602 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001603 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001604 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001606 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001607 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 +02001608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001609 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01001610 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001611 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001612 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001613 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001615 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001616 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001618 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001619 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 +02001620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001621 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001622 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001624 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001625 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001627 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001628 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001629 break;
1630 default:
David Sedlákb3077192019-06-19 10:55:37 +02001631 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejci0f969882020-08-21 16:56:47 +02001632 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 return LY_EVALID;
1634 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001635 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 return ret;
1637}
1638
Michal Vaskoea5abea2018-09-18 13:10:54 +02001639/**
1640 * @brief Parse the value or position statement. Substatement of type enum statement.
1641 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001642 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001643 * @param[in] val_kw Type of this particular keyword.
1644 * @param[in,out] value Value to write to.
1645 * @param[in,out] flags Flags to write to.
1646 * @param[in,out] exts Extension instances to add to.
1647 *
1648 * @return LY_ERR values.
1649 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001650LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001651parse_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 +02001652 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001654 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001655 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001656 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001657 long int num = 0;
1658 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001659 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001660
1661 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001662 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663 return LY_EVALID;
1664 }
1665 *flags |= LYS_SET_VALUE;
1666
1667 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001668 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001669
Radek Krejcid6b76452019-09-03 17:03:03 +02001670 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 +02001671 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001672 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673 }
1674
1675 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001676 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001677 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001678 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001679 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001680 goto error;
1681 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001682 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001683 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001684 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001685 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001686 goto error;
1687 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001688 }
1689 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001690 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001691 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001692 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693 }
1694 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001695 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001696 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001698 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 *value = num;
1700 } else {
1701 *value = unum;
1702 }
1703 free(buf);
1704
Radek Krejci33090f92020-12-17 20:12:46 +01001705 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001706 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001707 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001708 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 +02001709 break;
1710 default:
David Sedlákb3077192019-06-19 10:55:37 +02001711 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001712 return LY_EVALID;
1713 }
1714 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001716
1717error:
1718 free(buf);
1719 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001720}
1721
Michal Vaskoea5abea2018-09-18 13:10:54 +02001722/**
1723 * @brief Parse the enum or bit statement. Substatement of type statement.
1724 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001725 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001726 * @param[in] enum_kw Type of this particular keyword.
1727 * @param[in,out] enums Enums or bits to add to.
1728 *
1729 * @return LY_ERR values.
1730 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001731static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001732parse_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 +02001733{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001734 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001736 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001737 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738 struct lysp_type_enum *enm;
1739
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001740 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001741
1742 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001743 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 +02001744 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001745 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001746 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001747 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001748
Radek Krejci011e4aa2020-09-04 15:22:31 +02001749 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001750 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1751
Radek Krejci33090f92020-12-17 20:12:46 +01001752 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001754 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001755 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 +02001756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001757 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001758 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01001759 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001760 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001761 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001762 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 +02001763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001764 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001765 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001767 case LY_STMT_VALUE:
1768 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 +02001769 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001770 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001771 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001772 case LY_STMT_POSITION:
1773 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 +02001774 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001775 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001776 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001777 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001778 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779 break;
1780 default:
David Sedlákb3077192019-06-19 10:55:37 +02001781 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001782 return LY_EVALID;
1783 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001784 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001785 return ret;
1786}
1787
Michal Vaskoea5abea2018-09-18 13:10:54 +02001788/**
1789 * @brief Parse the fraction-digits statement. Substatement of type statement.
1790 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001791 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001792 * @param[in,out] fracdig Value to write to.
1793 * @param[in,out] exts Extension instances to add to.
1794 *
1795 * @return LY_ERR values.
1796 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001798parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001800 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001801 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001802 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001803 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001804 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001805
1806 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001807 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001808 return LY_EVALID;
1809 }
1810
1811 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001812 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001813
1814 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001815 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816 free(buf);
1817 return LY_EVALID;
1818 }
1819
1820 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001821 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001822 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001823 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001824 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825 free(buf);
1826 return LY_EVALID;
1827 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001828 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001829 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001830 free(buf);
1831 return LY_EVALID;
1832 }
1833 *fracdig = num;
1834 free(buf);
1835
Radek Krejci33090f92020-12-17 20:12:46 +01001836 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001837 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001838 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001839 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 break;
1841 default:
David Sedlákb3077192019-06-19 10:55:37 +02001842 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 return LY_EVALID;
1844 }
1845 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846 return ret;
1847}
1848
Michal Vaskoea5abea2018-09-18 13:10:54 +02001849/**
1850 * @brief Parse the require-instance statement. Substatement of type statement.
1851 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001852 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001853 * @param[in,out] reqinst Value to write to.
1854 * @param[in,out] flags Flags to write to.
1855 * @param[in,out] exts Extension instances to add to.
1856 *
1857 * @return LY_ERR values.
1858 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001859static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001860parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001861 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001863 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001864 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001865 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001866 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867
1868 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001869 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001870 return LY_EVALID;
1871 }
1872 *flags |= LYS_SET_REQINST;
1873
1874 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001875 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876
Radek Krejcif13b87b2020-12-01 22:02:17 +01001877 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001878 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001879 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001880 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881 free(buf);
1882 return LY_EVALID;
1883 }
1884 free(buf);
1885
Radek Krejci33090f92020-12-17 20:12:46 +01001886 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001887 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001888 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001889 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001890 break;
1891 default:
David Sedlákb3077192019-06-19 10:55:37 +02001892 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001893 return LY_EVALID;
1894 }
1895 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 return ret;
1897}
1898
Michal Vaskoea5abea2018-09-18 13:10:54 +02001899/**
1900 * @brief Parse the modifier statement. Substatement of type pattern statement.
1901 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001902 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001903 * @param[in,out] pat Value to write to.
1904 * @param[in,out] exts Extension instances to add to.
1905 *
1906 * @return LY_ERR values.
1907 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001909parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001911 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001912 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001913 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001914 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001915
Radek Krejcif13b87b2020-12-01 22:02:17 +01001916 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001917 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001918 return LY_EVALID;
1919 }
1920
1921 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001922 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001923
Radek Krejcif13b87b2020-12-01 22:02:17 +01001924 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001925 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926 free(buf);
1927 return LY_EVALID;
1928 }
1929 free(buf);
1930
1931 /* replace the value in the dictionary */
1932 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001933 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001935 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001936
Radek Krejcif13b87b2020-12-01 22:02:17 +01001937 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
1938 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001939 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001940
Radek Krejci33090f92020-12-17 20:12:46 +01001941 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001942 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001943 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001944 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001945 break;
1946 default:
David Sedlákb3077192019-06-19 10:55:37 +02001947 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001948 return LY_EVALID;
1949 }
1950 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951 return ret;
1952}
1953
Michal Vaskoea5abea2018-09-18 13:10:54 +02001954/**
1955 * @brief Parse the pattern statement. Substatement of type statement.
1956 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001957 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001958 * @param[in,out] patterns Restrictions to add to.
1959 *
1960 * @return LY_ERR values.
1961 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001963parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001964{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001965 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001966 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001967 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001968 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969 struct lysp_restr *restr;
1970
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001971 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001972
1973 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001974 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975
1976 /* add special meaning first byte */
1977 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001978 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979 word = buf;
1980 } else {
1981 buf = malloc(word_len + 2);
1982 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001983 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001984 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001985 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02001986 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001987 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
1988 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989
Radek Krejci33090f92020-12-17 20:12:46 +01001990 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001992 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001993 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 +02001994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001995 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001996 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 +02001997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001998 case LY_STMT_ERROR_APP_TAG:
Radek Krejci33090f92020-12-17 20:12:46 +01001999 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002001 case LY_STMT_ERROR_MESSAGE:
Radek Krejci33090f92020-12-17 20:12:46 +01002002 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002004 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002005 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002006 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002008 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002009 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002010 break;
2011 default:
David Sedlákb3077192019-06-19 10:55:37 +02002012 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002013 return LY_EVALID;
2014 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002015 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002016 return ret;
2017}
2018
Michal Vaskoea5abea2018-09-18 13:10:54 +02002019/**
2020 * @brief Parse the type statement.
2021 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002022 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002023 * @param[in,out] type Type to wrote to.
2024 *
2025 * @return LY_ERR values.
2026 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002028parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002030 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002031 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002032 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002033 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002034 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002035 struct lysp_type *nest_type;
2036
2037 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002038 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039 return LY_EVALID;
2040 }
2041
2042 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002043 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002044 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002046 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002047 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002048
Radek Krejci33090f92020-12-17 20:12:46 +01002049 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002050 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002051 case LY_STMT_BASE:
Radek Krejci33090f92020-12-17 20:12:46 +01002052 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 +01002053 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002054 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002055 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002056 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002057 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002059 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002060 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002061 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002063 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002064 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002065 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002067 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002069 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070 return LY_EVALID;
2071 }
2072 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002073 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074
Radek Krejci33090f92020-12-17 20:12:46 +01002075 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002076 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002077 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002078 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002079 if (type->path) {
2080 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2081 return LY_EVALID;
2082 }
2083
Radek Krejci33090f92020-12-17 20:12:46 +01002084 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 +02002085 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 +02002086 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002087 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002088 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002089 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002091 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002092 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002093 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002095 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002097 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098 return LY_EVALID;
2099 }
2100 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002101 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102
Radek Krejci33090f92020-12-17 20:12:46 +01002103 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002104 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002106 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002107 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002108 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002110 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002111 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002112 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002113 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002115 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002116 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 break;
2118 default:
David Sedlákb3077192019-06-19 10:55:37 +02002119 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 return LY_EVALID;
2121 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 return ret;
2124}
2125
Michal Vaskoea5abea2018-09-18 13:10:54 +02002126/**
2127 * @brief Parse the leaf statement.
2128 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002129 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002130 * @param[in,out] siblings Siblings to add to.
2131 *
2132 * @return LY_ERR values.
2133 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002134LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002135parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002136{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002137 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002138 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002139 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002140 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141 struct lysp_node_leaf *leaf;
2142
David Sedlák60adc092019-08-06 15:57:02 +02002143 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002144 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002146 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002149 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002150 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151
2152 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002153 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002155 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002156 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002158 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002159 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 +02002160 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002162 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002163 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 +02002164 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002165 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002166 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002168 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002169 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002171 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002172 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002174 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002175 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 +02002176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002177 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002178 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002180 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002181 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002183 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002184 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 +02002185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002186 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002187 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002189 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002190 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191 break;
2192 default:
David Sedlákb3077192019-06-19 10:55:37 +02002193 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194 return LY_EVALID;
2195 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002196 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002197 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002198checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002199 /* mandatory substatements */
2200 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002201 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202 return LY_EVALID;
2203 }
2204
2205 return ret;
2206}
2207
Michal Vaskoea5abea2018-09-18 13:10:54 +02002208/**
2209 * @brief Parse the max-elements statement.
2210 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002211 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002212 * @param[in,out] max Value to write to.
2213 * @param[in,out] flags Flags to write to.
2214 * @param[in,out] exts Extension instances to add to.
2215 *
2216 * @return LY_ERR values.
2217 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002218LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002219parse_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 +02002220{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002221 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002223 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002225 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002226
2227 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002228 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002229 return LY_EVALID;
2230 }
2231 *flags |= LYS_SET_MAX;
2232
2233 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002234 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235
2236 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002237 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002238 free(buf);
2239 return LY_EVALID;
2240 }
2241
Radek Krejci7f9b6512019-09-18 13:11:09 +02002242 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002243 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002244 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002245 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002246 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002247 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002248 free(buf);
2249 return LY_EVALID;
2250 }
2251 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002252 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253 free(buf);
2254 return LY_EVALID;
2255 }
2256
2257 *max = num;
2258 }
2259 free(buf);
2260
Radek Krejci33090f92020-12-17 20:12:46 +01002261 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002262 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002263 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002264 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002265 break;
2266 default:
David Sedlákb3077192019-06-19 10:55:37 +02002267 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002268 return LY_EVALID;
2269 }
2270 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002271 return ret;
2272}
2273
Michal Vaskoea5abea2018-09-18 13:10:54 +02002274/**
2275 * @brief Parse the min-elements statement.
2276 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002277 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002278 * @param[in,out] min Value to write to.
2279 * @param[in,out] flags Flags to write to.
2280 * @param[in,out] exts Extension instances to add to.
2281 *
2282 * @return LY_ERR values.
2283 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002284LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002285parse_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 +02002286{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002287 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002289 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002291 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002292
2293 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002294 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002295 return LY_EVALID;
2296 }
2297 *flags |= LYS_SET_MIN;
2298
2299 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002300 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301
2302 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002303 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304 free(buf);
2305 return LY_EVALID;
2306 }
2307
2308 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002309 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002311 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002312 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313 free(buf);
2314 return LY_EVALID;
2315 }
2316 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002317 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002318 free(buf);
2319 return LY_EVALID;
2320 }
2321 *min = num;
2322 free(buf);
2323
Radek Krejci33090f92020-12-17 20:12:46 +01002324 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002325 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002326 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002327 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 break;
2329 default:
David Sedlákb3077192019-06-19 10:55:37 +02002330 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 return LY_EVALID;
2332 }
2333 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 return ret;
2335}
2336
Michal Vaskoea5abea2018-09-18 13:10:54 +02002337/**
2338 * @brief Parse the ordered-by statement.
2339 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002340 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002341 * @param[in,out] flags Flags to write to.
2342 * @param[in,out] exts Extension instances to add to.
2343 *
2344 * @return LY_ERR values.
2345 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002347parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002349 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002351 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002352 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002353
2354 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002355 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002356 return LY_EVALID;
2357 }
2358
2359 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002360 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361
Radek Krejcif13b87b2020-12-01 22:02:17 +01002362 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002364 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365 *flags |= LYS_ORDBY_USER;
2366 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002367 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368 free(buf);
2369 return LY_EVALID;
2370 }
2371 free(buf);
2372
Radek Krejci33090f92020-12-17 20:12:46 +01002373 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002374 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002375 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002376 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002377 break;
2378 default:
David Sedlákb3077192019-06-19 10:55:37 +02002379 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002380 return LY_EVALID;
2381 }
2382 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383 return ret;
2384}
2385
Michal Vaskoea5abea2018-09-18 13:10:54 +02002386/**
2387 * @brief Parse the leaf-list statement.
2388 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002389 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002390 * @param[in,out] siblings Siblings to add to.
2391 *
2392 * @return LY_ERR values.
2393 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002394LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002395parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002397 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002398 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002399 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002400 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002401 struct lysp_node_leaflist *llist;
2402
David Sedlák60adc092019-08-06 15:57:02 +02002403 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002404 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002405 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002406 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002407
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002409 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002410 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411
2412 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002413 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002414 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002415 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002416 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002418 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002419 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci33090f92020-12-17 20:12:46 +01002420 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002422 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002423 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 +02002424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002425 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002426 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002428 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002429 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002431 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002432 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002434 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002435 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002437 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002438 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002440 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002441 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 +02002442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002443 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002444 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002446 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002447 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002449 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002450 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 +02002451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002452 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002453 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002455 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002456 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002457 break;
2458 default:
David Sedlákb3077192019-06-19 10:55:37 +02002459 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460 return LY_EVALID;
2461 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002462 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002463 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002464checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465 /* mandatory substatements */
2466 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002467 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002468 return LY_EVALID;
2469 }
2470
2471 return ret;
2472}
2473
Michal Vaskoea5abea2018-09-18 13:10:54 +02002474/**
2475 * @brief Parse the refine statement.
2476 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002477 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002478 * @param[in,out] refines Refines to add to.
2479 *
2480 * @return LY_ERR values.
2481 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002482static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002483parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002484{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002485 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002486 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002487 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002488 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002489 struct lysp_refine *rf;
2490
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002491 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002492
2493 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002494 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002495 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002496 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497
Radek Krejci33090f92020-12-17 20:12:46 +01002498 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002500 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002501 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002503 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002504 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002506 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002507 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 +02002508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002509 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002510 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci33090f92020-12-17 20:12:46 +01002511 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002512 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002513 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002514 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002515 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002516 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002517 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002519 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002520 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002521 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002522 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002523 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002524 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002525 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002526 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 +02002527 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002528 case LY_STMT_PRESENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002529 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 +02002530 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002531 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002532 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533 break;
2534 default:
David Sedlákb3077192019-06-19 10:55:37 +02002535 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002536 return LY_EVALID;
2537 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539 return ret;
2540}
2541
Michal Vaskoea5abea2018-09-18 13:10:54 +02002542/**
2543 * @brief Parse the typedef statement.
2544 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002545 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002546 * @param[in,out] typedefs Typedefs to add to.
2547 *
2548 * @return LY_ERR values.
2549 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002550static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002551parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002553 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002555 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002556 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 struct lysp_tpdf *tpdf;
2558
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002559 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560
2561 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002562 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002563 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002564
2565 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002566 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002568 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002569 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 +02002570 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002572 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002573 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 +02002574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002575 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002576 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 +02002577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002578 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002579 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002580 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002581 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002582 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002583 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002584 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002585 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 +02002586 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002587 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002588 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002589 break;
2590 default:
David Sedlákb3077192019-06-19 10:55:37 +02002591 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002592 return LY_EVALID;
2593 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002595 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002596checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 /* mandatory substatements */
2598 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002599 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600 return LY_EVALID;
2601 }
2602
Radek Krejcibbe09a92018-11-08 09:36:54 +01002603 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002604 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002605 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002606 }
2607
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608 return ret;
2609}
2610
Michal Vaskoea5abea2018-09-18 13:10:54 +02002611/**
2612 * @brief Parse the input or output statement.
2613 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002614 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002615 * @param[in] kw Type of this particular keyword
2616 * @param[in,out] inout_p Input/output pointer to write to.
2617 *
2618 * @return LY_ERR values.
2619 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002621parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Michal Vasko7f45cf22020-10-01 12:49:44 +02002622 struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002624 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002625 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002626 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002627 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002628
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002629 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002630 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002631 return LY_EVALID;
2632 }
2633
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002634 /* initiate structure */
Michal Vasko22df3f02020-08-24 13:29:22 +02002635 inout_p->nodetype = &((struct lysp_action *)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002636 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002637
2638 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002639 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002641 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002642 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002643 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002644 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01002645 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002647 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01002648 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002650 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01002651 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002653 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01002654 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002656 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002657 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002659 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002660 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002662 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01002663 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002664 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002665 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002666 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002668 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002669 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002670 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002672 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002673 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002674 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002675 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002676 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002677 break;
2678 default:
David Sedlákb3077192019-06-19 10:55:37 +02002679 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680 return LY_EVALID;
2681 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002683 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002684
Radek Krejci7fc68292019-06-12 13:51:09 +02002685checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002686 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002687 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, inout_p->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002688
Michal Vaskob83af8a2020-01-06 09:49:22 +01002689 if (!inout_p->data) {
2690 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 Krejci33090f92020-12-17 20:12:46 +01002706parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_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;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712 struct lysp_action *act;
2713
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002714 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *actions, act, 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 Krejci33090f92020-12-17 20:12:46 +01002728 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &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
2760 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2761 if (!act->input.nodetype) {
2762 act->input.nodetype = LYS_INPUT;
2763 act->input.parent = (struct lysp_node *)act;
2764 }
2765 if (!act->output.nodetype) {
2766 act->output.nodetype = LYS_OUTPUT;
2767 act->output.parent = (struct lysp_node *)act;
2768 }
2769
Radek Krejci7fc68292019-06-12 13:51:09 +02002770checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002771 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002772 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002773
Michal Vasko7fbc8162018-09-17 10:35:16 +02002774 return ret;
2775}
2776
Michal Vaskoea5abea2018-09-18 13:10:54 +02002777/**
2778 * @brief Parse the notification statement.
2779 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002780 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002781 * @param[in,out] notifs Notifications to add to.
2782 *
2783 * @return LY_ERR values.
2784 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002785LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002786parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002788 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002789 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002790 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002791 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792 struct lysp_notif *notif;
2793
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002794 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002795
2796 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002797 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002798 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002799 notif->nodetype = LYS_NOTIF;
2800 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002801
Radek Krejci33090f92020-12-17 20:12:46 +01002802 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002803 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002804 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002805 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 +02002806 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002807 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002808 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002809 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002810 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002811 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 +02002812 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002813 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002814 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002815 break;
2816
Radek Krejcid6b76452019-09-03 17:03:03 +02002817 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002818 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002819 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002820 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01002821 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002822 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002823 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01002824 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002825 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002826 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01002827 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002829 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01002830 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002832 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002833 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002835 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002836 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002837 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002838 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01002839 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840 break;
2841
Radek Krejcid6b76452019-09-03 17:03:03 +02002842 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002843 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002844 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002846 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002847 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002849 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002850 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002852 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002853 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854 break;
2855 default:
David Sedlákb3077192019-06-19 10:55:37 +02002856 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 return LY_EVALID;
2858 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002860 LY_CHECK_RET(ret);
2861checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002862 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002863 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002864
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865 return ret;
2866}
2867
Michal Vaskoea5abea2018-09-18 13:10:54 +02002868/**
2869 * @brief Parse the grouping statement.
2870 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002871 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002872 * @param[in,out] groupings Groupings to add to.
2873 *
2874 * @return LY_ERR values.
2875 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002876LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002877parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002878{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002879 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002881 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002882 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 struct lysp_grp *grp;
2884
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002885 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002886
2887 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002888 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002889 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002890 grp->nodetype = LYS_GROUPING;
2891 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892
Radek Krejci33090f92020-12-17 20:12:46 +01002893 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002894 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002895 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002896 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 +02002897 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002898 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002899 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 +02002900 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002901 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002902 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002903 break;
2904
Radek Krejcid6b76452019-09-03 17:03:03 +02002905 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002906 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002907 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002908 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01002909 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002911 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01002912 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002914 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01002915 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002917 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01002918 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002920 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002921 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002922 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002923 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002924 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002926 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01002927 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 break;
2929
Radek Krejcid6b76452019-09-03 17:03:03 +02002930 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002931 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)grp, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002933 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002934 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci33090f92020-12-17 20:12:46 +01002935 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002936 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002937 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002938 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002940 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002941 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci33090f92020-12-17 20:12:46 +01002942 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002944 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002945 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 break;
2947 default:
David Sedlákb3077192019-06-19 10:55:37 +02002948 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949 return LY_EVALID;
2950 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002951 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002952 LY_CHECK_RET(ret);
2953checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002954 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002955 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02002956
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957 return ret;
2958}
2959
Michal Vaskoea5abea2018-09-18 13:10:54 +02002960/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002961 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002962 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002963 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002964 * @param[in,out] augments Augments to add to.
2965 *
2966 * @return LY_ERR values.
2967 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002968LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002969parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002971 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002972 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002973 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002974 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975 struct lysp_augment *aug;
2976
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002977 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002978
2979 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002980 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002981 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002982 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002983 aug->nodetype = LYS_AUGMENT;
2984 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985
Radek Krejci33090f92020-12-17 20:12:46 +01002986 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002988 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002989 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 +02002990 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002991 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002992 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002994 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002995 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 +02002996 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002997 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002998 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002999 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003000 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003001 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003002 break;
3003
Radek Krejcid6b76452019-09-03 17:03:03 +02003004 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003005 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003006 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003007 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003008 LY_CHECK_RET(parse_any(ctx, kw, (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_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003011 LY_CHECK_RET(parse_case(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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003014 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003017 LY_CHECK_RET(parse_container(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_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003020 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003022 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003023 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003025 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003026 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003028 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003029 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 break;
3031
Radek Krejcid6b76452019-09-03 17:03:03 +02003032 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003033 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003034 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003036 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003037 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003038 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003039 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003040 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003041 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003042 break;
3043 default:
David Sedlákb3077192019-06-19 10:55:37 +02003044 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045 return LY_EVALID;
3046 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003048 LY_CHECK_RET(ret);
3049checks:
3050 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003051 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003052
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 return ret;
3054}
3055
Michal Vaskoea5abea2018-09-18 13:10:54 +02003056/**
3057 * @brief Parse the uses statement.
3058 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003059 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003060 * @param[in,out] siblings Siblings to add to.
3061 *
3062 * @return LY_ERR values.
3063 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003064LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003065parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003067 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003068 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003069 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003070 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071 struct lysp_node_uses *uses;
3072
David Sedlák60adc092019-08-06 15:57:02 +02003073 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003074 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003076 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003079 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003080 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081
3082 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003083 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003085 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003086 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 +02003087 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003088 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003089 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003090 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003091 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003092 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 +02003093 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003094 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003095 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003097 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003098 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099 break;
3100
Radek Krejcid6b76452019-09-03 17:03:03 +02003101 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003102 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003103 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003104 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003105 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003106 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003107 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003108 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003109 break;
3110 default:
David Sedlákb3077192019-06-19 10:55:37 +02003111 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 return LY_EVALID;
3113 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003114 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003115checks:
3116 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003117 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003118
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 return ret;
3120}
3121
Michal Vaskoea5abea2018-09-18 13:10:54 +02003122/**
3123 * @brief Parse the case statement.
3124 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003125 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003126 * @param[in,out] siblings Siblings to add to.
3127 *
3128 * @return LY_ERR values.
3129 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003130LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003131parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003133 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003134 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003135 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003136 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137 struct lysp_node_case *cas;
3138
David Sedlák60adc092019-08-06 15:57:02 +02003139 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003140 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003142 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003145 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003146 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147
3148 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003149 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003150 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003151 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003152 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 +02003153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003154 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003155 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003157 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003158 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 +02003159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003160 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003161 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003163 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003164 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 break;
3166
Radek Krejcid6b76452019-09-03 17:03:03 +02003167 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003168 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003169 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003170 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003171 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003173 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003174 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003176 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003177 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003179 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003180 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003182 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003183 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003185 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003186 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003188 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003189 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003191 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003192 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003193 break;
3194 default:
David Sedlákb3077192019-06-19 10:55:37 +02003195 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196 return LY_EVALID;
3197 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003198 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199 return ret;
3200}
3201
Michal Vaskoea5abea2018-09-18 13:10:54 +02003202/**
3203 * @brief Parse the choice statement.
3204 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003205 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003206 * @param[in,out] siblings Siblings to add to.
3207 *
3208 * @return LY_ERR values.
3209 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003210LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003211parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003213 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003215 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003216 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003217 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003218
David Sedlák60adc092019-08-06 15:57:02 +02003219 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003220 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003221 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003222 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223
Michal Vasko7fbc8162018-09-17 10:35:16 +02003224 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003225 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003226 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227
3228 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003229 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003231 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003232 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003234 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003235 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 +02003236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003237 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003238 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003240 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003241 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003243 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003244 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 +02003245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003246 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003247 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003249 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003250 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003252 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01003253 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 +02003254 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003255 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003256 break;
3257
Radek Krejcid6b76452019-09-03 17:03:03 +02003258 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003259 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003260 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003261 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003262 LY_CHECK_RET(parse_any(ctx, kw, (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_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003265 LY_CHECK_RET(parse_case(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_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003268 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003269 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003271 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003272 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003274 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003275 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003277 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003278 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003280 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003281 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003282 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003283 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003284 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285 break;
3286 default:
David Sedlákb3077192019-06-19 10:55:37 +02003287 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288 return LY_EVALID;
3289 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003290 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003291 return ret;
3292}
3293
Michal Vaskoea5abea2018-09-18 13:10:54 +02003294/**
3295 * @brief Parse the container statement.
3296 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003297 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003298 * @param[in,out] siblings Siblings to add to.
3299 *
3300 * @return LY_ERR values.
3301 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003302LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003303parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304{
3305 LY_ERR ret = 0;
3306 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003307 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003308 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003309 struct lysp_node_container *cont;
3310
David Sedlák60adc092019-08-06 15:57:02 +02003311 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003312 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003314 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003317 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003318 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319
3320 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003321 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003323 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003324 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003326 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003327 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 +02003328 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003329 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003330 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003332 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003333 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 +02003334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003335 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003336 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003338 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003339 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003341 case LY_STMT_PRESENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003342 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 +02003343 break;
3344
Radek Krejcid6b76452019-09-03 17:03:03 +02003345 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003346 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003347 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003348 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003349 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003351 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003352 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003354 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003355 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003357 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003358 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003360 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003361 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003363 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003364 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003366 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003367 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368 break;
3369
Radek Krejcid6b76452019-09-03 17:03:03 +02003370 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003371 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003373 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003374 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003376 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003377 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003378 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003380 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003381 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003383 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003384 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003385 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003387 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003388 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003389 break;
3390 default:
David Sedlákb3077192019-06-19 10:55:37 +02003391 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392 return LY_EVALID;
3393 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003395checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003396 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003397 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 return ret;
3399}
3400
Michal Vaskoea5abea2018-09-18 13:10:54 +02003401/**
3402 * @brief Parse the list statement.
3403 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003404 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003405 * @param[in,out] siblings Siblings to add to.
3406 *
3407 * @return LY_ERR values.
3408 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003409LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003410parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003412 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003414 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003415 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416 struct lysp_node_list *list;
3417
David Sedlák60adc092019-08-06 15:57:02 +02003418 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003419 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003421 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003422
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003424 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003425 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426
3427 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003428 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003430 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003431 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003433 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003434 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 +02003435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003436 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003437 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003439 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003440 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 +02003441 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003442 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003443 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003445 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003446 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003448 case LY_STMT_KEY:
Radek Krejci33090f92020-12-17 20:12:46 +01003449 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 +02003450 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003451 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003452 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003453 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003454 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003455 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003457 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003458 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_UNIQUE:
Radek Krejci33090f92020-12-17 20:12:46 +01003461 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462 break;
3463
Radek Krejcid6b76452019-09-03 17:03:03 +02003464 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003465 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003466 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003467 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003468 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003469 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003470 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003471 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003473 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003474 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003476 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003477 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003479 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003480 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003481 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003482 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003483 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003485 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003486 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 break;
3488
Radek Krejcid6b76452019-09-03 17:03:03 +02003489 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003490 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003492 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003493 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003495 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003496 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003497 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003498 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003499 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003500 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003501 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003502 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003503 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003504 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003506 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003507 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508 break;
3509 default:
David Sedlákb3077192019-06-19 10:55:37 +02003510 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 return LY_EVALID;
3512 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003514 LY_CHECK_RET(ret);
3515checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003516 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003517 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003518
Michal Vasko7fbc8162018-09-17 10:35:16 +02003519 return ret;
3520}
3521
Michal Vaskoea5abea2018-09-18 13:10:54 +02003522/**
3523 * @brief Parse the yin-element statement.
3524 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003525 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003526 * @param[in,out] flags Flags to write to.
3527 * @param[in,out] exts Extension instances to add to.
3528 *
3529 * @return LY_ERR values.
3530 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003532parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003534 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003536 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003537 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538
3539 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003540 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 return LY_EVALID;
3542 }
3543
3544 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003545 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546
Radek Krejcif13b87b2020-12-01 22:02:17 +01003547 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003549 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 *flags |= LYS_YINELEM_FALSE;
3551 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003552 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 free(buf);
3554 return LY_EVALID;
3555 }
3556 free(buf);
3557
Radek Krejci33090f92020-12-17 20:12:46 +01003558 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003559 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003560 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003561 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003562 LY_CHECK_RET(ret);
3563 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 default:
David Sedlákb3077192019-06-19 10:55:37 +02003565 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 return LY_EVALID;
3567 }
3568 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569 return ret;
3570}
3571
Michal Vaskoea5abea2018-09-18 13:10:54 +02003572/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003573 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003574 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003575 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003576 * @param[in,out] argument Value to write to.
3577 * @param[in,out] flags Flags to write to.
3578 * @param[in,out] exts Extension instances to add to.
3579 *
3580 * @return LY_ERR values.
3581 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003583parse_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 +02003584{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003585 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003587 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003588 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589
3590 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003591 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 return LY_EVALID;
3593 }
3594
3595 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003596 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003597 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598
Radek Krejci33090f92020-12-17 20:12:46 +01003599 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003601 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003602 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003604 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003605 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003606 break;
3607 default:
David Sedlákb3077192019-06-19 10:55:37 +02003608 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609 return LY_EVALID;
3610 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003611 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003612 return ret;
3613}
3614
Michal Vaskoea5abea2018-09-18 13:10:54 +02003615/**
3616 * @brief Parse the extension statement.
3617 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003618 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003619 * @param[in,out] extensions Extensions to add to.
3620 *
3621 * @return LY_ERR values.
3622 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003624parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003626 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003628 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003629 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003630 struct lysp_ext *ex;
3631
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003632 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633
3634 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003635 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003636 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637
Radek Krejci33090f92020-12-17 20:12:46 +01003638 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003640 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003641 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 +02003642 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003643 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003644 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 +02003645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003646 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003647 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003648 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003649 case LY_STMT_ARGUMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003650 LY_CHECK_RET(parse_argument(ctx, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003651 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003652 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003653 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003654 break;
3655 default:
David Sedlákb3077192019-06-19 10:55:37 +02003656 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 return LY_EVALID;
3658 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003659 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660 return ret;
3661}
3662
Michal Vaskoea5abea2018-09-18 13:10:54 +02003663/**
3664 * @brief Parse the deviate statement.
3665 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003666 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003667 * @param[in,out] deviates Deviates to add to.
3668 *
3669 * @return LY_ERR values.
3670 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003671LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003672parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003674 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003675 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003676 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003677 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003678 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679 struct lysp_deviate_add *d_add = NULL;
3680 struct lysp_deviate_rpl *d_rpl = NULL;
3681 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003682 const char **d_units = NULL;
3683 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003684 struct lysp_restr **d_musts = NULL;
3685 uint16_t *d_flags = 0;
3686 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687
3688 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003689 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690
Radek Krejcif13b87b2020-12-01 22:02:17 +01003691 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003693 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003695 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003696 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003697 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003698 dev_mod = LYS_DEV_DELETE;
3699 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003700 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701 free(buf);
3702 return LY_EVALID;
3703 }
3704 free(buf);
3705
3706 /* create structure */
3707 switch (dev_mod) {
3708 case LYS_DEV_NOT_SUPPORTED:
3709 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003710 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003711 break;
3712 case LYS_DEV_ADD:
3713 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003714 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715 d = (struct lysp_deviate *)d_add;
3716 d_units = &d_add->units;
3717 d_uniques = &d_add->uniques;
3718 d_dflts = &d_add->dflts;
3719 d_musts = &d_add->musts;
3720 d_flags = &d_add->flags;
3721 d_min = &d_add->min;
3722 d_max = &d_add->max;
3723 break;
3724 case LYS_DEV_REPLACE:
3725 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003726 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003727 d = (struct lysp_deviate *)d_rpl;
3728 d_units = &d_rpl->units;
3729 d_flags = &d_rpl->flags;
3730 d_min = &d_rpl->min;
3731 d_max = &d_rpl->max;
3732 break;
3733 case LYS_DEV_DELETE:
3734 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003735 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736 d = (struct lysp_deviate *)d_del;
3737 d_units = &d_del->units;
3738 d_uniques = &d_del->uniques;
3739 d_dflts = &d_del->dflts;
3740 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003741 break;
3742 default:
3743 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003744 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 }
3746 d->mod = dev_mod;
3747
3748 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003749 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750
Radek Krejci33090f92020-12-17 20:12:46 +01003751 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003752 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003753 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003754 switch (dev_mod) {
3755 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003756 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003757 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003758 return LY_EVALID;
3759 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003760 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 break;
3762 }
3763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003764 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765 switch (dev_mod) {
3766 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003767 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 return LY_EVALID;
3769 case LYS_DEV_REPLACE:
Radek Krejci33090f92020-12-17 20:12:46 +01003770 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 +02003771 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 break;
3773 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003774 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &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_MANDATORY:
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_mandatory(ctx, 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_MAX_ELEMENTS:
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_DELETE:
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_maxelements(ctx, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003797 break;
3798 }
3799 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003800 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 switch (dev_mod) {
3802 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003803 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003804 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805 return LY_EVALID;
3806 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003807 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 break;
3809 }
3810 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003811 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 switch (dev_mod) {
3813 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003814 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003815 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003816 return LY_EVALID;
3817 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003818 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 break;
3820 }
3821 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003822 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 switch (dev_mod) {
3824 case LYS_DEV_NOT_SUPPORTED:
3825 case LYS_DEV_ADD:
3826 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003827 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 return LY_EVALID;
3829 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003830 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003831 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003832 return LY_EVALID;
3833 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003835 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003836 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003837 break;
3838 }
3839 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003840 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 switch (dev_mod) {
3842 case LYS_DEV_NOT_SUPPORTED:
3843 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003844 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845 return LY_EVALID;
3846 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003847 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 break;
3849 }
3850 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003851 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 switch (dev_mod) {
3853 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003854 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855 return LY_EVALID;
3856 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003857 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 +02003858 break;
3859 }
3860 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003861 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003862 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863 break;
3864 default:
David Sedlákb3077192019-06-19 10:55:37 +02003865 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 return LY_EVALID;
3867 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003868 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003869 return ret;
3870}
3871
Michal Vaskoea5abea2018-09-18 13:10:54 +02003872/**
3873 * @brief Parse the deviation statement.
3874 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003875 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003876 * @param[in,out] deviations Deviations to add to.
3877 *
3878 * @return LY_ERR values.
3879 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003880LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003881parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003882{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003883 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003885 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003886 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 struct lysp_deviation *dev;
3888
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003889 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003890
3891 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003892 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003893 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003894 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895
Radek Krejci33090f92020-12-17 20:12:46 +01003896 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003897 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003898 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003899 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 +02003900 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003901 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003902 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003903 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003904 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003905 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 +02003906 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003907 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003908 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003909 break;
3910 default:
David Sedlákb3077192019-06-19 10:55:37 +02003911 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 return LY_EVALID;
3913 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003915 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003916checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917 /* mandatory substatements */
3918 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003919 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003920 return LY_EVALID;
3921 }
3922
3923 return ret;
3924}
3925
Michal Vaskoea5abea2018-09-18 13:10:54 +02003926/**
3927 * @brief Parse the feature statement.
3928 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003929 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003930 * @param[in,out] features Features to add to.
3931 *
3932 * @return LY_ERR values.
3933 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003934LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003935parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003937 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003939 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003940 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003941 struct lysp_feature *feat;
3942
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003943 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944
3945 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003946 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003947 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003948
Radek Krejci33090f92020-12-17 20:12:46 +01003949 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003951 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003952 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 +02003953 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003954 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003955 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003957 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003958 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 +02003959 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003960 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003961 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003963 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003964 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003965 break;
3966 default:
David Sedlákb3077192019-06-19 10:55:37 +02003967 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003968 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003970 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003971 return ret;
3972}
3973
Michal Vaskoea5abea2018-09-18 13:10:54 +02003974/**
3975 * @brief Parse the identity statement.
3976 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003977 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003978 * @param[in,out] identities Identities to add to.
3979 *
3980 * @return LY_ERR values.
3981 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003982LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003983parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003985 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003986 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003987 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003988 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003989 struct lysp_ident *ident;
3990
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003991 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003992
3993 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003994 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003995 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003996
Radek Krejci33090f92020-12-17 20:12:46 +01003997 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003998 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003999 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01004000 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 +02004001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004002 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004003 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci33090f92020-12-17 20:12:46 +01004004 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004006 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004007 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 +02004008 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004009 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004010 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004012 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004013 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004014 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 +01004015 return LY_EVALID;
4016 }
Radek Krejci33090f92020-12-17 20:12:46 +01004017 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 +02004018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004019 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004020 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004021 break;
4022 default:
David Sedlákb3077192019-06-19 10:55:37 +02004023 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024 return LY_EVALID;
4025 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004026 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004027 return ret;
4028}
4029
Michal Vaskoea5abea2018-09-18 13:10:54 +02004030/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004031 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004032 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004033 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004034 * @param[in,out] mod Module to write to.
4035 *
4036 * @return LY_ERR values.
4037 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004038LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004039parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040{
4041 LY_ERR ret = 0;
4042 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004043 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004044 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004045 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004046 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004047
Michal Vaskoc3781c32020-10-06 14:04:08 +02004048 mod->is_submod = 0;
4049
4050 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004051 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004052 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004053
Radek Krejci33090f92020-12-17 20:12:46 +01004054 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004055
Radek Krejcie3846472018-10-15 15:24:51 +02004056#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004057 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 +02004058
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059 switch (kw) {
4060 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004061 case LY_STMT_NAMESPACE:
4062 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004063 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4064 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004065 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004066 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004067 break;
4068 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004069 case LY_STMT_INCLUDE:
4070 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004071 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004072 break;
4073 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004074 case LY_STMT_ORGANIZATION:
4075 case LY_STMT_CONTACT:
4076 case LY_STMT_DESCRIPTION:
4077 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004078 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004079 break;
4080
4081 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004082 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004083 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004084 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004085 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004086 case LY_STMT_ANYDATA:
4087 case LY_STMT_ANYXML:
4088 case LY_STMT_AUGMENT:
4089 case LY_STMT_CHOICE:
4090 case LY_STMT_CONTAINER:
4091 case LY_STMT_DEVIATION:
4092 case LY_STMT_EXTENSION:
4093 case LY_STMT_FEATURE:
4094 case LY_STMT_GROUPING:
4095 case LY_STMT_IDENTITY:
4096 case LY_STMT_LEAF:
4097 case LY_STMT_LEAF_LIST:
4098 case LY_STMT_LIST:
4099 case LY_STMT_NOTIFICATION:
4100 case LY_STMT_RPC:
4101 case LY_STMT_TYPEDEF:
4102 case LY_STMT_USES:
4103 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004104 mod_stmt = Y_MOD_BODY;
4105 break;
4106 default:
4107 /* error handled in the next switch */
4108 break;
4109 }
Radek Krejcie3846472018-10-15 15:24:51 +02004110#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111
Radek Krejcie3846472018-10-15 15:24:51 +02004112 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004113 switch (kw) {
4114 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004115 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004116 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004118 case LY_STMT_NAMESPACE:
Radek Krejci33090f92020-12-17 20:12:46 +01004119 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 +02004120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004121 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +01004122 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 +02004123 break;
4124
4125 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004126 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004127 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004128 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004129 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004130 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131 break;
4132
4133 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004134 case LY_STMT_ORGANIZATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004135 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 +02004136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004137 case LY_STMT_CONTACT:
Radek Krejci33090f92020-12-17 20:12:46 +01004138 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 +02004139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004140 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01004141 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 +02004142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004143 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004144 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 +02004145 break;
4146
4147 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004148 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004149 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004150 break;
4151
4152 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004153 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004154 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004155 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004156 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004157 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004159 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004160 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004162 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004163 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004164 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004165 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004166 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004168 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004169 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004171 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004172 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004174 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004175 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004176 break;
4177
Radek Krejcid6b76452019-09-03 17:03:03 +02004178 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004179 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004181 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004182 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004184 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004185 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004187 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004188 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004189 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004190 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004191 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004192 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004193 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004194 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004196 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004197 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004199 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004200 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004202 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004203 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004205 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004206 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 break;
4208
4209 default:
David Sedlákb3077192019-06-19 10:55:37 +02004210 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004211 return LY_EVALID;
4212 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004213 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004214 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004215
Radek Krejci6d6556c2018-11-08 09:37:45 +01004216checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004217 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004218 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004219
Michal Vasko7fbc8162018-09-17 10:35:16 +02004220 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004221 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004222 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004223 return LY_EVALID;
4224 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004225 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004226 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 }
4228
Radek Krejcie9e987e2018-10-31 12:50:27 +01004229 /* submodules share the namespace with the module names, so there must not be
4230 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004231 dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, mod->mod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004232 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004233 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004234 return LY_EVALID;
4235 }
4236
4237 return ret;
4238}
4239
4240/**
4241 * @brief Parse submodule substatements.
4242 *
4243 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004244 * @param[out] submod Parsed submodule structure.
4245 *
4246 * @return LY_ERR values.
4247 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004248LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004249parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004250{
4251 LY_ERR ret = 0;
4252 char *buf, *word;
4253 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004254 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004255 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4256 struct lysp_submodule *dup;
4257
Michal Vaskoc3781c32020-10-06 14:04:08 +02004258 submod->is_submod = 1;
4259
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004260 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004261 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004262 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004263
Radek Krejci33090f92020-12-17 20:12:46 +01004264 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004265
4266#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004267 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 +01004268
4269 switch (kw) {
4270 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004271 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004272 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4273 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004274 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004275 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4276 break;
4277 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004278 case LY_STMT_INCLUDE:
4279 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004280 CHECK_ORDER(Y_MOD_LINKAGE);
4281 break;
4282 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004283 case LY_STMT_ORGANIZATION:
4284 case LY_STMT_CONTACT:
4285 case LY_STMT_DESCRIPTION:
4286 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004287 CHECK_ORDER(Y_MOD_META);
4288 break;
4289
4290 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004291 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004292 CHECK_ORDER(Y_MOD_REVISION);
4293 break;
4294 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004295 case LY_STMT_ANYDATA:
4296 case LY_STMT_ANYXML:
4297 case LY_STMT_AUGMENT:
4298 case LY_STMT_CHOICE:
4299 case LY_STMT_CONTAINER:
4300 case LY_STMT_DEVIATION:
4301 case LY_STMT_EXTENSION:
4302 case LY_STMT_FEATURE:
4303 case LY_STMT_GROUPING:
4304 case LY_STMT_IDENTITY:
4305 case LY_STMT_LEAF:
4306 case LY_STMT_LEAF_LIST:
4307 case LY_STMT_LIST:
4308 case LY_STMT_NOTIFICATION:
4309 case LY_STMT_RPC:
4310 case LY_STMT_TYPEDEF:
4311 case LY_STMT_USES:
4312 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004313 mod_stmt = Y_MOD_BODY;
4314 break;
4315 default:
4316 /* error handled in the next switch */
4317 break;
4318 }
4319#undef CHECK_ORDER
4320
4321 prev_kw = kw;
4322 switch (kw) {
4323 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004324 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004325 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004327 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004328 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004329 break;
4330
4331 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004332 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004333 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004336 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004337 break;
4338
4339 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004340 case LY_STMT_ORGANIZATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004341 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 +01004342 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004343 case LY_STMT_CONTACT:
Radek Krejci33090f92020-12-17 20:12:46 +01004344 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 +01004345 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004346 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01004347 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 +01004348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004349 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004350 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 +01004351 break;
4352
4353 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004354 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004355 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004356 break;
4357
4358 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004359 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004360 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004361 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004362 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004363 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004365 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004366 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004367 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004368 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004369 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004371 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004372 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004374 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004375 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004377 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004378 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004380 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004381 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004382 break;
4383
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004385 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004388 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004390 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004391 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004393 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004394 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004396 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004397 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004399 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004400 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004402 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004403 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004405 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004406 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004407 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004408 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004409 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004411 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004412 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004413 break;
4414
4415 default:
David Sedlákb3077192019-06-19 10:55:37 +02004416 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004417 return LY_EVALID;
4418 }
4419 }
4420 LY_CHECK_RET(ret);
4421
4422checks:
4423 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004424 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments,
Michal Vasko69730152020-10-09 16:30:07 +02004425 submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004426
4427 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004428 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004429 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004430 return LY_EVALID;
4431 }
4432
4433 /* submodules share the namespace with the module names, so there must not be
4434 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004435 dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, submod->name, NULL);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004436 /* main modules may have different revisions */
4437 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02004438 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004439 return LY_EVALID;
4440 }
4441
Michal Vasko7fbc8162018-09-17 10:35:16 +02004442 return ret;
4443}
4444
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004445/**
4446 * @brief Skip any redundant characters, namely whitespaces and comments.
4447 *
4448 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004449 * @return LY_SUCCESS on success.
4450 * @return LY_EVALID on invalid comment.
4451 */
4452static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004453skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004454{
4455 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004456 while (ctx->in->current[0]) {
4457 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004458 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004459 ly_in_skip(ctx->in, 2);
4460 LY_CHECK_RET(skip_comment(ctx, 1));
4461 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004462 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004463 ly_in_skip(ctx->in, 2);
4464 LY_CHECK_RET(skip_comment(ctx, 2));
4465 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004466 /* whitespace */
Radek Krejci33090f92020-12-17 20:12:46 +01004467 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004468 } else {
4469 break;
4470 }
4471 }
4472
4473 return LY_SUCCESS;
4474}
4475
Radek Krejcid4557c62018-09-17 11:42:09 +02004476LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004477yang_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 +02004478 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004479{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004480 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004481 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004482 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004483 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004484 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004485
David Sedlák1b623122019-08-05 15:27:49 +02004486 /* create context */
4487 *context = calloc(1, sizeof **context);
4488 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004489 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004490 (*context)->unres = main_ctx->unres;
Radek Krejci99435242019-09-05 16:19:15 +02004491 (*context)->pos_type = LY_VLOG_LINE;
Radek Krejci33090f92020-12-17 20:12:46 +01004492 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004493 (*context)->line = 1;
4494
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004495 mod_p = calloc(1, sizeof *mod_p);
4496 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4497 mod_p->mod = main_ctx->parsed_mod->mod;
4498 mod_p->parsing = 1;
4499 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4500
David Sedlák1b623122019-08-05 15:27:49 +02004501 /* map the typedefs and groupings list from main context to the submodule's context */
4502 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4503 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4504
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004505 /* skip redundant but valid characters at the beginning */
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);
4508
Michal Vasko7fbc8162018-09-17 10:35:16 +02004509 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004510 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004511 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004512
Radek Krejcid6b76452019-09-03 17:03:03 +02004513 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004514 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004515 ret = LY_EINVAL;
4516 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004517 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004518 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004519 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004520 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004521 }
4522
Michal Vasko7fbc8162018-09-17 10:35:16 +02004523 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004524 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004525 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004526
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004527 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004528 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004529 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004530 if (in->current[0]) {
4531 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004532 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004533 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004534 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004535
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004536 mod_p->parsing = 0;
4537 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004538
Radek Krejcibbe09a92018-11-08 09:36:54 +01004539cleanup:
4540 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004541 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004542 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004543 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004544 }
4545
4546 return ret;
4547}
4548
4549LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004550yang_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 +01004551{
4552 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004553 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004554 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004555 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004556 struct lysp_module *mod_p = NULL;
4557
David Sedlák1b623122019-08-05 15:27:49 +02004558 /* create context */
4559 *context = calloc(1, sizeof **context);
4560 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004561 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004562 (*context)->unres = unres;
Radek Krejci335332a2019-09-05 13:03:35 +02004563 (*context)->pos_type = LY_VLOG_LINE;
Radek Krejci33090f92020-12-17 20:12:46 +01004564 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004565 (*context)->line = 1;
4566
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004567 mod_p = calloc(1, sizeof *mod_p);
4568 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4569 mod_p->mod = mod;
4570 mod_p->parsing = 1;
4571 (*context)->parsed_mod = mod_p;
4572
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004573 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004574 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004575 LY_CHECK_GOTO(ret, cleanup);
4576
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004577 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004578 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004579 LY_CHECK_GOTO(ret, cleanup);
4580
Radek Krejcid6b76452019-09-03 17:03:03 +02004581 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004582 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 +01004583 ret = LY_EINVAL;
4584 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004585 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004586 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004587 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004588 goto cleanup;
4589 }
4590
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004591 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004592 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004593 LY_CHECK_GOTO(ret, cleanup);
4594
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004595 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004596 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004597 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004598 if (in->current[0]) {
4599 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004600 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004601 goto cleanup;
4602 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004603
4604 mod_p->parsing = 0;
4605 mod->parsed = mod_p;
4606
4607cleanup:
4608 if (ret) {
4609 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004610 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004611 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004612 }
4613
Michal Vasko7fbc8162018-09-17 10:35:16 +02004614 return ret;
4615}