blob: f3d98fe9a8b1673910056c8596d9500c751b1088 [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko63f3d842020-07-08 10:10:14 +020014#include "parser_internal.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
17#include <ctype.h>
18#include <errno.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "log.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020030#include "path.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "set.h"
32#include "tree.h"
33#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020034#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020035
Radek Krejciceaf2122019-01-02 15:03:26 +010036/**
37 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
38 * @param[in] CTX yang parser context to access libyang context.
39 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
40 * @param[out] TARGET variable where to store the pointer to the inserted value.
41 * @param[in] WORD string to store.
42 * @param[in] LEN length of the string in WORD to store.
43 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020044#define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \
Michal Vasko5d24f6c2020-10-13 13:49:06 +020045 if (BUF) {LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)));}\
46 else {LY_CHECK_RET(lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)));}
Radek Krejci44ceedc2018-10-02 15:54:31 +020047
Radek Krejciceaf2122019-01-02 15:03:26 +010048/**
Michal Vasko63f3d842020-07-08 10:10:14 +020049 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Radek Krejciceaf2122019-01-02 15:03:26 +010050 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010051 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
52 */
Radek Krejcid54412f2020-12-17 20:25:35 +010053#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020054
Michal Vaskoea5abea2018-09-18 13:10:54 +020055/**
56 * @brief Loop through all substatements providing, return if there are none.
57 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020058 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020059 * @param[out] KW YANG keyword read.
60 * @param[out] WORD Pointer to the keyword itself.
61 * @param[out] WORD_LEN Length of the keyword.
62 * @param[out] ERR Variable for error storing.
63 *
64 * @return In case there are no substatements or a fatal error encountered.
65 */
Radek Krejci33090f92020-12-17 20:12:46 +010066#define YANG_READ_SUBSTMT_FOR(CTX, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
67 LY_CHECK_RET(get_keyword(CTX, &KW, &WORD, &WORD_LEN)); \
Radek Krejcid6b76452019-09-03 17:03:03 +020068 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010069 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010070 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020071 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020072 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020073 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020074 return LY_EVALID; \
75 } \
Radek Krejci33090f92020-12-17 20:12:46 +010076 for (ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020077 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Radek Krejci33090f92020-12-17 20:12:46 +010078 ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020079
Radek Krejci33090f92020-12-17 20:12:46 +010080LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020081 struct lysp_node **siblings);
Radek Krejci33090f92020-12-17 20:12:46 +010082LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
83LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
84LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
85LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
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 Krejcid54412f2020-12-17 20:25:35 +0100235 LY_IN_NEW_LINE(ctx->in);
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 Krejcid54412f2020-12-17 20:25:35 +0100242 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200243 }
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 Krejcid54412f2020-12-17 20:25:35 +0100250 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200251 }
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
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200410 /* reset context indentation counter for possible string after this one */
411 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200412 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200413 break;
414 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200415 /* first non-whitespace character, stop eating indentation */
416 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200417
418 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100419 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 +0200420 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200421 break;
422 }
423 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100424 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200425 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100426 c = ctx->in->current;
427 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200428 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100429 ctx->in->current = "\n";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200430 break;
431 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100432 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433 break;
434 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200435 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200436 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200437 break;
438 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200439 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100440 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 return LY_EVALID;
442 }
443
444 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100445 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 +0200446
Radek Krejcif13b87b2020-12-01 22:02:17 +0100447 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100448 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200449 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100450 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100451 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200452 case '+':
453 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100454 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200455 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200456 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200457 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200458 case ' ':
459 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200460 /* just skip */
461 break;
462 default:
463 /* string is finished */
464 goto string_end;
465 }
Radek Krejci33090f92020-12-17 20:12:46 +0100466 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100468 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100469 switch (ctx->in->current[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200470 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200471 case ' ':
472 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200473 /* skip */
474 break;
475 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100476 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200477 break;
478 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100479 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 break;
481 default:
482 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200483 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200484 return LY_EVALID;
485 }
Radek Krejci33090f92020-12-17 20:12:46 +0100486 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 break;
488 default:
489 return LY_EINT;
490 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200491 }
492
493string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200494 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200495 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200496 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200497 return LY_EVALID;
498 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200499 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100500
501#undef STRING_ENDED
502#undef STRING_SINGLE_QUOTED
503#undef STRING_DOUBLE_QUOTED
504#undef STRING_DOUBLE_QUOTED_ESCAPED
505#undef STRING_PAUSED_NEXTSTRING
506#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200507}
508
Michal Vaskoea5abea2018-09-18 13:10:54 +0200509/**
510 * @brief Get another YANG string from the raw data.
511 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200512 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200513 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200514 * @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 +0200515 * @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 +0200516 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
517 * set to NULL. Otherwise equal to \p word_p.
518 * @param[out] word_len Length of the read string.
519 *
520 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200521 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200522LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100523get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200524 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200525{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200526 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200527 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200528
Michal Vasko7fbc8162018-09-17 10:35:16 +0200529 /* word buffer - dynamically allocated */
530 *word_b = NULL;
531
532 /* word pointer - just a pointer to data */
533 *word_p = NULL;
534
535 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100536 while (ctx->in->current[0]) {
537 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200538 case '\'':
539 case '\"':
540 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200541 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100542 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200543 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200544 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200545 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200546 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100547 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200548 }
Radek Krejci33090f92020-12-17 20:12:46 +0100549 LY_CHECK_RET(read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200550 goto str_end;
551 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100552 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200553 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100554 MOVE_INPUT(ctx, 2);
555 LY_CHECK_RET(skip_comment(ctx, 1));
556 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200557 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100558 MOVE_INPUT(ctx, 2);
559 LY_CHECK_RET(skip_comment(ctx, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200560 } else {
561 /* not a comment after all */
Radek Krejci33090f92020-12-17 20:12:46 +0100562 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 +0200563 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200564 break;
565 case ' ':
566 if (*word_len) {
567 /* word is finished */
568 goto str_end;
569 }
Radek Krejci33090f92020-12-17 20:12:46 +0100570 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200571 break;
572 case '\t':
573 if (*word_len) {
574 /* word is finished */
575 goto str_end;
576 }
577 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100578 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200579
Radek Krejci33090f92020-12-17 20:12:46 +0100580 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200581 break;
582 case '\n':
583 if (*word_len) {
584 /* word is finished */
585 goto str_end;
586 }
Radek Krejcid54412f2020-12-17 20:25:35 +0100587 MOVE_INPUT(ctx, 1);
588
Michal Vasko7fbc8162018-09-17 10:35:16 +0200589 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200590 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200591 break;
592 case ';':
593 case '{':
594 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
595 /* word is finished */
596 goto str_end;
597 }
598
Radek Krejci33090f92020-12-17 20:12:46 +0100599 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200601 case '}':
602 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100603 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200604 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200605 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200606 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100607 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 +0200608 break;
609 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200610 }
611
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200612 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200613 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200614 return LY_EVALID;
615
Michal Vasko7fbc8162018-09-17 10:35:16 +0200616str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200617 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200618 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200619 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200620 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200621 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200622 *word_p = *word_b;
623 }
624
625 return LY_SUCCESS;
626}
627
Michal Vaskoea5abea2018-09-18 13:10:54 +0200628/**
629 * @brief Get another YANG keyword from the raw data.
630 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200631 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200632 * @param[out] kw YANG keyword read.
633 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
634 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
635 *
636 * @return LY_ERR values.
637 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200638LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100639get_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 +0200640{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200641 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200642 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200643 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200644
645 if (word_p) {
646 *word_p = NULL;
647 *word_len = 0;
648 }
649
650 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100651 while (ctx->in->current[0]) {
652 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200653 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100654 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100656 MOVE_INPUT(ctx, 2);
657 LY_CHECK_RET(skip_comment(ctx, 1));
658 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100660 MOVE_INPUT(ctx, 2);
661 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200662 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200663 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200664 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200665 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200666 }
Radek Krejci13028282019-06-11 14:56:48 +0200667 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200668 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200669 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200670 ctx->indent = 0;
671 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200673 /* skip whitespaces (optsep) */
674 ++ctx->indent;
675 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200676 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200677 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100678 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200679 break;
680 default:
681 /* either a keyword start or an invalid character */
682 goto keyword_start;
683 }
684
Radek Krejci33090f92020-12-17 20:12:46 +0100685 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686 }
687
688keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100689 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100690 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200691
Michal Vasko69730152020-10-09 16:30:07 +0200692 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 +0200693 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200694 }
695
Radek Krejcid6b76452019-09-03 17:03:03 +0200696 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200697 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100698 switch (ctx->in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200699 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200700 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200701 case ' ':
702 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200703 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200704 case ':':
705 /* keyword is not actually a keyword, but prefix of an extension.
706 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
707 * and we will be checking the keyword (extension instance) itself */
708 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100709 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200710 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200711 case '{':
712 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200713 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200714 break;
715 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100716 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200717 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100718 MOVE_INPUT(ctx, 1);
719 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200720 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 return LY_EVALID;
722 }
723 } else {
724 /* still can be an extension */
725 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200726extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100727 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
728 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '{') && (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200729 uint32_t c = 0;
730
Radek Krejci33090f92020-12-17 20:12:46 +0100731 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
732 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200733 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200734 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200735 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100736 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737 }
Radek Krejci33090f92020-12-17 20:12:46 +0100738 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200739 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200740 return LY_EVALID;
741 }
742
743 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200744 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100745 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200746 return LY_EVALID;
747 }
748
Radek Krejcid6b76452019-09-03 17:03:03 +0200749 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200750 }
Radek Krejci626df482018-10-11 15:06:31 +0200751success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 if (word_p) {
753 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100754 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200755 }
756
757 return LY_SUCCESS;
758}
759
Michal Vaskoea5abea2018-09-18 13:10:54 +0200760/**
761 * @brief Parse extension instance substatements.
762 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200763 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200764 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200765 * @param[in] word Extension instance substatement name (keyword).
766 * @param[in] word_len Extension instance substatement name length.
767 * @param[in,out] child Children of this extension instance to add to.
768 *
769 * @return LY_ERR values.
770 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200771static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100772parse_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 +0200773 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200774{
775 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100776 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200777 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200778 struct lysp_stmt *stmt, *par_child;
779
780 stmt = calloc(1, sizeof *stmt);
781 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
782
Radek Krejcibb9b1982019-04-08 14:24:59 +0200783 /* insert into parent statements */
784 if (!*child) {
785 *child = stmt;
786 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200787 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200788 par_child->next = stmt;
789 }
790
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200791 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Radek Krejci335332a2019-09-05 13:03:35 +0200792 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200793
794 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100795 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200796
Radek Krejci0ae092d2018-09-20 16:43:19 +0200797 if (word) {
798 if (buf) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200799 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200800 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200801 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200802 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200803 }
804
Radek Krejci33090f92020-12-17 20:12:46 +0100805 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, ) {
806 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200807 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200808 return ret;
809}
810
Michal Vaskoea5abea2018-09-18 13:10:54 +0200811/**
812 * @brief Parse extension instance.
813 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200814 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200815 * @param[in] ext_name Extension instance substatement name (keyword).
816 * @param[in] ext_name_len Extension instance substatement name length.
817 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
818 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
819 * @param[in,out] exts Extension instances to add to.
820 *
821 * @return LY_ERR values.
822 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200823static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100824parse_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 +0200825 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100827 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200828 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200829 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200830 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200831 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200832
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200833 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200834
835 /* store name and insubstmt info */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200836 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837 e->insubstmt = insubstmt;
838 e->insubstmt_index = insubstmt_index;
839
840 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100841 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200842
Radek Krejci0ae092d2018-09-20 16:43:19 +0200843 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200844 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845 }
846
Radek Krejci33090f92020-12-17 20:12:46 +0100847 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
848 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200849 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200850 return ret;
851}
852
Michal Vaskoea5abea2018-09-18 13:10:54 +0200853/**
854 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
855 * description, etc...
856 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200857 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200858 * @param[in] substmt Type of this substatement.
859 * @param[in] substmt_index Index of this substatement.
860 * @param[in,out] value Place to store the parsed value.
861 * @param[in] arg Type of the YANG keyword argument (of the value).
862 * @param[in,out] exts Extension instances to add to.
863 *
864 * @return LY_ERR values.
865 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200866static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100867parse_text_field(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200868 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200869{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100870 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200871 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200872 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200873 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200874
875 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200876 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200877 return LY_EVALID;
878 }
879
880 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100881 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200882
883 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200884 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200885
Radek Krejci33090f92020-12-17 20:12:46 +0100886 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200887 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200888 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100889 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200890 break;
891 default:
David Sedlákb3077192019-06-19 10:55:37 +0200892 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200893 return LY_EVALID;
894 }
895 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200896 return ret;
897}
898
Michal Vaskoea5abea2018-09-18 13:10:54 +0200899/**
900 * @brief Parse the yang-version statement.
901 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200902 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100903 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200904 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200905 *
906 * @return LY_ERR values.
907 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200908static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100909parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100911 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200913 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200914 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200915
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100916 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200917 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918 return LY_EVALID;
919 }
920
921 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100922 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200923
Radek Krejci96e48da2020-09-04 13:18:06 +0200924 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100925 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100926 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100927 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200929 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200930 free(buf);
931 return LY_EVALID;
932 }
933 free(buf);
934
Radek Krejci33090f92020-12-17 20:12:46 +0100935 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200937 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100938 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939 break;
940 default:
David Sedlákb3077192019-06-19 10:55:37 +0200941 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200942 return LY_EVALID;
943 }
944 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200945 return ret;
946}
947
Michal Vaskoea5abea2018-09-18 13:10:54 +0200948/**
949 * @brief Parse the belongs-to statement.
950 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200951 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200952 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
953 * @param[in,out] exts Extension instances to add to.
954 *
955 * @return LY_ERR values.
956 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200957static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100958parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200959{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100960 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200961 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200962 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200963 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964
Michal Vaskoc3781c32020-10-06 14:04:08 +0200965 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200966 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200967 return LY_EVALID;
968 }
969
Michal Vaskoc3781c32020-10-06 14:04:08 +0200970 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +0100971 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200972 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +0200973 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 +0200974 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +0200975 free(buf);
976 return LY_EVALID;
977 }
978 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200979
Radek Krejci33090f92020-12-17 20:12:46 +0100980 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200982 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +0100983 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200985 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100986 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987 break;
988 default:
David Sedlákb3077192019-06-19 10:55:37 +0200989 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 return LY_EVALID;
991 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200993 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100994checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995 /* mandatory substatements */
996 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200997 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200998 return LY_EVALID;
999 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001000 return ret;
1001}
1002
Michal Vaskoea5abea2018-09-18 13:10:54 +02001003/**
1004 * @brief Parse the revision-date statement.
1005 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001006 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001007 * @param[in,out] rev Array to store the parsed value in.
1008 * @param[in,out] exts Extension instances to add to.
1009 *
1010 * @return LY_ERR values.
1011 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001012static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001013parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001015 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001016 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001017 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001018 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001019
1020 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001021 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001022 return LY_EVALID;
1023 }
1024
1025 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001026 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001027
1028 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001029 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001030 free(buf);
1031 return LY_EVALID;
1032 }
1033
1034 /* store value and spend buf if allocated */
1035 strncpy(rev, word, word_len);
1036 free(buf);
1037
Radek Krejci33090f92020-12-17 20:12:46 +01001038 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001040 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001041 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001042 break;
1043 default:
David Sedlákb3077192019-06-19 10:55:37 +02001044 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001045 return LY_EVALID;
1046 }
1047 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001048 return ret;
1049}
1050
Michal Vaskoea5abea2018-09-18 13:10:54 +02001051/**
1052 * @brief Parse the include statement.
1053 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001054 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001055 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001056 * @param[in,out] includes Parsed includes to add to.
1057 *
1058 * @return LY_ERR values.
1059 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001060static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001061parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001062{
Radek Krejcid33273d2018-10-25 14:55:52 +02001063 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001065 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001066 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067 struct lysp_include *inc;
1068
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001069 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001070
1071 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001072 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073
Radek Krejci011e4aa2020-09-04 15:22:31 +02001074 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001075
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001076 /* submodules share the namespace with the module names, so there must not be
1077 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001078 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001079 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001080 return LY_EVALID;
1081 }
1082
Radek Krejci33090f92020-12-17 20:12:46 +01001083 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001084 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001085 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001086 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci33090f92020-12-17 20:12:46 +01001087 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 +02001088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001089 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001090 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci33090f92020-12-17 20:12:46 +01001091 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 +02001092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001093 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001094 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001096 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001097 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001098 break;
1099 default:
David Sedlákb3077192019-06-19 10:55:37 +02001100 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001101 return LY_EVALID;
1102 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001103 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104 return ret;
1105}
1106
Michal Vaskoea5abea2018-09-18 13:10:54 +02001107/**
1108 * @brief Parse the import statement.
1109 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001110 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001111 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001112 * @param[in,out] imports Parsed imports to add to.
1113 *
1114 * @return LY_ERR values.
1115 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001117parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001119 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001120 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001121 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001122 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001123 struct lysp_import *imp;
1124
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001125 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126
1127 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001128 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001129 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130
Radek Krejci33090f92020-12-17 20:12:46 +01001131 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001133 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +01001134 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 +01001135 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 +02001136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001137 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001138 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci33090f92020-12-17 20:12:46 +01001139 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 +02001140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001141 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001142 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci33090f92020-12-17 20:12:46 +01001143 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 +02001144 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001145 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001146 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001148 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001149 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150 break;
1151 default:
David Sedlákb3077192019-06-19 10:55:37 +02001152 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153 return LY_EVALID;
1154 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001156 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001157checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001159 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160
1161 return ret;
1162}
1163
Michal Vaskoea5abea2018-09-18 13:10:54 +02001164/**
1165 * @brief Parse the revision statement.
1166 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001167 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001168 * @param[in,out] revs Parsed revisions to add to.
1169 *
1170 * @return LY_ERR values.
1171 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001173parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001174{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001175 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001176 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001177 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001178 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001179 struct lysp_revision *rev;
1180
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001181 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182
1183 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001184 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185
1186 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001187 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001188 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189 return LY_EVALID;
1190 }
1191
Radek Krejcib7db73a2018-10-24 14:18:40 +02001192 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001193 free(buf);
1194
Radek Krejci33090f92020-12-17 20:12:46 +01001195 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001197 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001198 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 +02001199 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001200 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001201 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 +02001202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001203 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001204 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001205 break;
1206 default:
David Sedlákb3077192019-06-19 10:55:37 +02001207 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208 return LY_EVALID;
1209 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001210 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 return ret;
1212}
1213
Michal Vaskoea5abea2018-09-18 13:10:54 +02001214/**
1215 * @brief Parse a generic text field that can have more instances such as base.
1216 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001217 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001218 * @param[in] substmt Type of this substatement.
1219 * @param[in,out] texts Parsed values to add to.
1220 * @param[in] arg Type of the expected argument.
1221 * @param[in,out] exts Extension instances to add to.
1222 *
1223 * @return LY_ERR values.
1224 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001225static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001226parse_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 +02001227 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001228{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001229 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001231 const char **item;
1232 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001233 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234
1235 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001236 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237
1238 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001239 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240
Radek Krejci011e4aa2020-09-04 15:22:31 +02001241 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Radek Krejci33090f92020-12-17 20:12:46 +01001242 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001243 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001244 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001245 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246 break;
1247 default:
David Sedlákb3077192019-06-19 10:55:37 +02001248 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249 return LY_EVALID;
1250 }
1251 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252 return ret;
1253}
1254
Michal Vaskoea5abea2018-09-18 13:10:54 +02001255/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001256 * @brief Parse a generic text field that can have more instances such as base.
1257 *
1258 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001259 * @param[in] substmt Type of this substatement.
1260 * @param[in,out] qnames Parsed qnames to add to.
1261 * @param[in] arg Type of the expected argument.
1262 * @param[in,out] exts Extension instances to add to.
1263 *
1264 * @return LY_ERR values.
1265 */
1266static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001267parse_qnames(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001268 enum yang_arg arg, struct lysp_ext_instance **exts)
1269{
1270 LY_ERR ret = LY_SUCCESS;
1271 char *buf, *word;
1272 struct lysp_qname *item;
1273 size_t word_len;
1274 enum ly_stmt kw;
1275
1276 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001277 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001278
1279 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001280 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001281
1282 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001283 item->mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001284 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001285 switch (kw) {
1286 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001287 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001288 break;
1289 default:
1290 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
1291 return LY_EVALID;
1292 }
1293 }
1294 return ret;
1295}
1296
1297/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001298 * @brief Parse the config statement.
1299 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001300 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001301 * @param[in,out] flags Flags to add to.
1302 * @param[in,out] exts Extension instances to add to.
1303 *
1304 * @return LY_ERR values.
1305 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001306static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001307parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001308{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001309 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001311 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001312 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001313
1314 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001315 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316 return LY_EVALID;
1317 }
1318
1319 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001320 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001321
Radek Krejcif13b87b2020-12-01 22:02:17 +01001322 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001323 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001324 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325 *flags |= LYS_CONFIG_R;
1326 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001327 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001328 free(buf);
1329 return LY_EVALID;
1330 }
1331 free(buf);
1332
Radek Krejci33090f92020-12-17 20:12:46 +01001333 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001335 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001336 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337 break;
1338 default:
David Sedlákb3077192019-06-19 10:55:37 +02001339 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001340 return LY_EVALID;
1341 }
1342 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001343 return ret;
1344}
1345
Michal Vaskoea5abea2018-09-18 13:10:54 +02001346/**
1347 * @brief Parse the mandatory statement.
1348 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001349 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001350 * @param[in,out] flags Flags to add to.
1351 * @param[in,out] exts Extension instances to add to.
1352 *
1353 * @return LY_ERR values.
1354 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001355static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001356parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001357{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001358 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001359 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001360 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001361 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362
1363 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001364 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001365 return LY_EVALID;
1366 }
1367
1368 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001369 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001370
Radek Krejcif13b87b2020-12-01 22:02:17 +01001371 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001372 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001373 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374 *flags |= LYS_MAND_FALSE;
1375 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001376 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001377 free(buf);
1378 return LY_EVALID;
1379 }
1380 free(buf);
1381
Radek Krejci33090f92020-12-17 20:12:46 +01001382 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001384 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001385 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 break;
1387 default:
David Sedlákb3077192019-06-19 10:55:37 +02001388 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001389 return LY_EVALID;
1390 }
1391 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001392 return ret;
1393}
1394
Michal Vaskoea5abea2018-09-18 13:10:54 +02001395/**
1396 * @brief Parse a restriction such as range or length.
1397 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001398 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001399 * @param[in] restr_kw Type of this particular restriction.
1400 * @param[in,out] exts Extension instances to add to.
1401 *
1402 * @return LY_ERR values.
1403 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001404static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001405parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001406{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001407 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001408 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001409 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001410 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001411
1412 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001413 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001414
Michal Vaskob36053d2020-03-26 15:49:30 +01001415 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001416 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001417 restr->arg.mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001418 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001419 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001420 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001421 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 +02001422 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001423 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001424 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 +02001425 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001426 case LY_STMT_ERROR_APP_TAG:
Radek Krejci33090f92020-12-17 20:12:46 +01001427 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 +02001428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001429 case LY_STMT_ERROR_MESSAGE:
Radek Krejci33090f92020-12-17 20:12:46 +01001430 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 +02001431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001432 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001433 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434 break;
1435 default:
David Sedlákb3077192019-06-19 10:55:37 +02001436 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437 return LY_EVALID;
1438 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001439 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440 return ret;
1441}
1442
Michal Vaskoea5abea2018-09-18 13:10:54 +02001443/**
1444 * @brief Parse a restriction that can have more instances such as must.
1445 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001446 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001447 * @param[in] restr_kw Type of this particular restriction.
1448 * @param[in,out] restrs Restrictions to add to.
1449 *
1450 * @return LY_ERR values.
1451 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001452static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001453parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001454{
1455 struct lysp_restr *restr;
1456
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001457 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001458 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001459}
1460
Michal Vaskoea5abea2018-09-18 13:10:54 +02001461/**
1462 * @brief Parse the status statement.
1463 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001464 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001465 * @param[in,out] flags Flags to add to.
1466 * @param[in,out] exts Extension instances to add to.
1467 *
1468 * @return LY_ERR values.
1469 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001470static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001471parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001473 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001474 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001475 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001476 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001477
1478 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001479 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001480 return LY_EVALID;
1481 }
1482
1483 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001484 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001485
Radek Krejcif13b87b2020-12-01 22:02:17 +01001486 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001488 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001490 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491 *flags |= LYS_STATUS_OBSLT;
1492 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001493 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001494 free(buf);
1495 return LY_EVALID;
1496 }
1497 free(buf);
1498
Radek Krejci33090f92020-12-17 20:12:46 +01001499 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001501 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001502 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 break;
1504 default:
David Sedlákb3077192019-06-19 10:55:37 +02001505 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001506 return LY_EVALID;
1507 }
1508 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001509 return ret;
1510}
1511
Michal Vaskoea5abea2018-09-18 13:10:54 +02001512/**
1513 * @brief Parse the when statement.
1514 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001515 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001516 * @param[in,out] when_p When pointer to parse to.
1517 *
1518 * @return LY_ERR values.
1519 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001520LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001521parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001522{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001523 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001524 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001525 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001526 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001527 struct lysp_when *when;
1528
1529 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001530 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531 return LY_EVALID;
1532 }
1533
1534 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001535 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001536 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537
1538 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001539 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001540 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001541 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001542
Radek Krejci33090f92020-12-17 20:12:46 +01001543 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001545 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001546 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 +02001547 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001548 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001549 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 +02001550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001551 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001552 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553 break;
1554 default:
David Sedlákb3077192019-06-19 10:55:37 +02001555 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556 return LY_EVALID;
1557 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001558 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559 return ret;
1560}
1561
Michal Vaskoea5abea2018-09-18 13:10:54 +02001562/**
1563 * @brief Parse the anydata or anyxml statement.
1564 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001565 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001566 * @param[in] kw Type of this particular keyword.
1567 * @param[in,out] siblings Siblings to add to.
1568 *
1569 * @return LY_ERR values.
1570 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001571LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001572parse_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 +02001573{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001574 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001575 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001576 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 struct lysp_node_anydata *any;
1578
David Sedlák60adc092019-08-06 15:57:02 +02001579 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001580 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001581
Radek Krejcid6b76452019-09-03 17:03:03 +02001582 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001583 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584
Michal Vasko7fbc8162018-09-17 10:35:16 +02001585 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001586 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001587 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588
1589 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01001590 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001591 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001592 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001593 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001594 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001595 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001596 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 +02001597 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001598 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01001599 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001601 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001602 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001604 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001605 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001607 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001608 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 +02001609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001610 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001611 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001613 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001614 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001616 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001617 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001618 break;
1619 default:
David Sedlákb3077192019-06-19 10:55:37 +02001620 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejci0f969882020-08-21 16:56:47 +02001621 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622 return LY_EVALID;
1623 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 return ret;
1626}
1627
Michal Vaskoea5abea2018-09-18 13:10:54 +02001628/**
1629 * @brief Parse the value or position statement. Substatement of type enum statement.
1630 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001631 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001632 * @param[in] val_kw Type of this particular keyword.
1633 * @param[in,out] value Value to write to.
1634 * @param[in,out] flags Flags to write to.
1635 * @param[in,out] exts Extension instances to add to.
1636 *
1637 * @return LY_ERR values.
1638 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001639LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001640parse_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 +02001641 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001642{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001643 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001645 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001646 long int num = 0;
1647 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001648 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001649
1650 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001651 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001652 return LY_EVALID;
1653 }
1654 *flags |= LYS_SET_VALUE;
1655
1656 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001657 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658
Radek Krejcid6b76452019-09-03 17:03:03 +02001659 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 +02001660 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001661 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001662 }
1663
1664 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001665 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001666 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001667 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001668 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001669 goto error;
1670 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001672 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001673 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001674 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001675 goto error;
1676 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001677 }
1678 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001679 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001680 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001681 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001682 }
1683 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001684 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001685 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001687 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001688 *value = num;
1689 } else {
1690 *value = unum;
1691 }
1692 free(buf);
1693
Radek Krejci33090f92020-12-17 20:12:46 +01001694 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001695 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001696 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001697 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 +02001698 break;
1699 default:
David Sedlákb3077192019-06-19 10:55:37 +02001700 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001701 return LY_EVALID;
1702 }
1703 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001704 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001705
1706error:
1707 free(buf);
1708 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001709}
1710
Michal Vaskoea5abea2018-09-18 13:10:54 +02001711/**
1712 * @brief Parse the enum or bit statement. Substatement of type statement.
1713 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001714 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001715 * @param[in] enum_kw Type of this particular keyword.
1716 * @param[in,out] enums Enums or bits to add to.
1717 *
1718 * @return LY_ERR values.
1719 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001720static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001721parse_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 +02001722{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001723 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001725 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001726 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001727 struct lysp_type_enum *enm;
1728
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001729 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001730
1731 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001732 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 +02001733 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001734 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001735 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001736 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001737
Radek Krejci011e4aa2020-09-04 15:22:31 +02001738 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001739 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1740
Radek Krejci33090f92020-12-17 20:12:46 +01001741 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001742 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001743 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001744 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 +02001745 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001746 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001747 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01001748 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001749 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001750 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001751 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 +02001752 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001753 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001754 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001755 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001756 case LY_STMT_VALUE:
1757 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 +02001758 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001759 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001760 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001761 case LY_STMT_POSITION:
1762 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 +02001763 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001764 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001766 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001767 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768 break;
1769 default:
David Sedlákb3077192019-06-19 10:55:37 +02001770 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771 return LY_EVALID;
1772 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001773 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 return ret;
1775}
1776
Michal Vaskoea5abea2018-09-18 13:10:54 +02001777/**
1778 * @brief Parse the fraction-digits statement. Substatement of type statement.
1779 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001780 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001781 * @param[in,out] fracdig Value to write to.
1782 * @param[in,out] exts Extension instances to add to.
1783 *
1784 * @return LY_ERR values.
1785 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001786static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001787parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001788{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001789 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001790 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001791 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001792 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001793 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794
1795 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001796 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797 return LY_EVALID;
1798 }
1799
1800 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001801 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001802
1803 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001804 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001805 free(buf);
1806 return LY_EVALID;
1807 }
1808
1809 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001810 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001812 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001813 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814 free(buf);
1815 return LY_EVALID;
1816 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001817 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001818 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 free(buf);
1820 return LY_EVALID;
1821 }
1822 *fracdig = num;
1823 free(buf);
1824
Radek Krejci33090f92020-12-17 20:12:46 +01001825 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001826 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001827 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001828 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001829 break;
1830 default:
David Sedlákb3077192019-06-19 10:55:37 +02001831 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001832 return LY_EVALID;
1833 }
1834 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835 return ret;
1836}
1837
Michal Vaskoea5abea2018-09-18 13:10:54 +02001838/**
1839 * @brief Parse the require-instance statement. Substatement of type statement.
1840 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001841 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001842 * @param[in,out] reqinst Value to write to.
1843 * @param[in,out] flags Flags to write to.
1844 * @param[in,out] exts Extension instances to add to.
1845 *
1846 * @return LY_ERR values.
1847 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001849parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001850 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001851{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001852 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001853 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001854 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001855 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001856
1857 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001858 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001859 return LY_EVALID;
1860 }
1861 *flags |= LYS_SET_REQINST;
1862
1863 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001864 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001865
Radek Krejcif13b87b2020-12-01 22:02:17 +01001866 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001868 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001869 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001870 free(buf);
1871 return LY_EVALID;
1872 }
1873 free(buf);
1874
Radek Krejci33090f92020-12-17 20:12:46 +01001875 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001877 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001878 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879 break;
1880 default:
David Sedlákb3077192019-06-19 10:55:37 +02001881 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001882 return LY_EVALID;
1883 }
1884 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 return ret;
1886}
1887
Michal Vaskoea5abea2018-09-18 13:10:54 +02001888/**
1889 * @brief Parse the modifier statement. Substatement of type pattern statement.
1890 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001891 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001892 * @param[in,out] pat Value to write to.
1893 * @param[in,out] exts Extension instances to add to.
1894 *
1895 * @return LY_ERR values.
1896 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001898parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001900 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001901 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001902 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001903 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904
Radek Krejcif13b87b2020-12-01 22:02:17 +01001905 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001906 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001907 return LY_EVALID;
1908 }
1909
1910 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001911 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001912
Radek Krejcif13b87b2020-12-01 22:02:17 +01001913 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001914 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001915 free(buf);
1916 return LY_EVALID;
1917 }
1918 free(buf);
1919
1920 /* replace the value in the dictionary */
1921 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001922 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001923 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001924 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925
Radek Krejcif13b87b2020-12-01 22:02:17 +01001926 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
1927 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001928 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929
Radek Krejci33090f92020-12-17 20:12:46 +01001930 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001932 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001933 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934 break;
1935 default:
David Sedlákb3077192019-06-19 10:55:37 +02001936 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001937 return LY_EVALID;
1938 }
1939 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001940 return ret;
1941}
1942
Michal Vaskoea5abea2018-09-18 13:10:54 +02001943/**
1944 * @brief Parse the pattern statement. Substatement of type statement.
1945 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001946 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001947 * @param[in,out] patterns Restrictions to add to.
1948 *
1949 * @return LY_ERR values.
1950 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001952parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001954 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001955 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001956 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001957 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001958 struct lysp_restr *restr;
1959
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001960 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001961
1962 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001963 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001964
1965 /* add special meaning first byte */
1966 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001967 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001968 word = buf;
1969 } else {
1970 buf = malloc(word_len + 2);
1971 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001972 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001973 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001974 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02001975 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001976 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
1977 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978
Radek Krejci33090f92020-12-17 20:12:46 +01001979 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001980 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001981 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001982 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 +02001983 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001984 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001985 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 +02001986 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001987 case LY_STMT_ERROR_APP_TAG:
Radek Krejci33090f92020-12-17 20:12:46 +01001988 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 +02001989 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 case LY_STMT_ERROR_MESSAGE:
Radek Krejci33090f92020-12-17 20:12:46 +01001991 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 +02001992 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001993 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02001994 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01001995 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001996 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001997 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001998 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001999 break;
2000 default:
David Sedlákb3077192019-06-19 10:55:37 +02002001 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002002 return LY_EVALID;
2003 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002004 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002005 return ret;
2006}
2007
Michal Vaskoea5abea2018-09-18 13:10:54 +02002008/**
2009 * @brief Parse the type statement.
2010 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002011 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002012 * @param[in,out] type Type to wrote to.
2013 *
2014 * @return LY_ERR values.
2015 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002016static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002017parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002019 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002021 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002022 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002023 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002024 struct lysp_type *nest_type;
2025
2026 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002027 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028 return LY_EVALID;
2029 }
2030
2031 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002032 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002033 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002035 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002036 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002037
Radek Krejci33090f92020-12-17 20:12:46 +01002038 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002040 case LY_STMT_BASE:
Radek Krejci33090f92020-12-17 20:12:46 +01002041 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 +01002042 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002044 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002045 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002046 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002048 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002049 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002050 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002051 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002052 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002053 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002054 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002056 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002057 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002058 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002059 return LY_EVALID;
2060 }
2061 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002062 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002063
Radek Krejci33090f92020-12-17 20:12:46 +01002064 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002065 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002067 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002068 if (type->path) {
2069 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2070 return LY_EVALID;
2071 }
2072
Radek Krejci33090f92020-12-17 20:12:46 +01002073 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 +02002074 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 +02002075 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002076 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002077 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002078 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002079 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002080 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002081 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002082 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002084 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002085 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002086 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002087 return LY_EVALID;
2088 }
2089 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002090 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002091
Radek Krejci33090f92020-12-17 20:12:46 +01002092 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002093 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002095 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002096 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002097 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002099 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002100 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002101 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002102 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002104 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002105 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106 break;
2107 default:
David Sedlákb3077192019-06-19 10:55:37 +02002108 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109 return LY_EVALID;
2110 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002111 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 return ret;
2113}
2114
Michal Vaskoea5abea2018-09-18 13:10:54 +02002115/**
2116 * @brief Parse the leaf statement.
2117 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002118 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002119 * @param[in,out] siblings Siblings to add to.
2120 *
2121 * @return LY_ERR values.
2122 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002123LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002124parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002126 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002128 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002129 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130 struct lysp_node_leaf *leaf;
2131
David Sedlák60adc092019-08-06 15:57:02 +02002132 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002133 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002135 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002136
Michal Vasko7fbc8162018-09-17 10:35:16 +02002137 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002138 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002139 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002140
2141 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002142 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002144 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002145 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002147 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002148 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 +02002149 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002151 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002152 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 +02002153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002154 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002155 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002157 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002158 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002160 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002161 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002163 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002164 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 +02002165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002166 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002167 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002169 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002170 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002172 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002173 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 +02002174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002175 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002176 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002178 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002179 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180 break;
2181 default:
David Sedlákb3077192019-06-19 10:55:37 +02002182 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 return LY_EVALID;
2184 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002186 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002187checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 /* mandatory substatements */
2189 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002190 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191 return LY_EVALID;
2192 }
2193
2194 return ret;
2195}
2196
Michal Vaskoea5abea2018-09-18 13:10:54 +02002197/**
2198 * @brief Parse the max-elements statement.
2199 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002200 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002201 * @param[in,out] max Value to write to.
2202 * @param[in,out] flags Flags to write to.
2203 * @param[in,out] exts Extension instances to add to.
2204 *
2205 * @return LY_ERR values.
2206 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002207LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002208parse_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 +02002209{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002210 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002212 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002213 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002214 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002215
2216 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002217 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002218 return LY_EVALID;
2219 }
2220 *flags |= LYS_SET_MAX;
2221
2222 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002223 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224
2225 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002226 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002227 free(buf);
2228 return LY_EVALID;
2229 }
2230
Radek Krejci7f9b6512019-09-18 13:11:09 +02002231 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002232 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002233 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002235 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002236 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 free(buf);
2238 return LY_EVALID;
2239 }
2240 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002241 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002242 free(buf);
2243 return LY_EVALID;
2244 }
2245
2246 *max = num;
2247 }
2248 free(buf);
2249
Radek Krejci33090f92020-12-17 20:12:46 +01002250 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002252 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002253 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254 break;
2255 default:
David Sedlákb3077192019-06-19 10:55:37 +02002256 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002257 return LY_EVALID;
2258 }
2259 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 return ret;
2261}
2262
Michal Vaskoea5abea2018-09-18 13:10:54 +02002263/**
2264 * @brief Parse the min-elements statement.
2265 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002266 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002267 * @param[in,out] min Value to write to.
2268 * @param[in,out] flags Flags to write to.
2269 * @param[in,out] exts Extension instances to add to.
2270 *
2271 * @return LY_ERR values.
2272 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002273LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002274parse_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 +02002275{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002276 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002278 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002280 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281
2282 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002283 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284 return LY_EVALID;
2285 }
2286 *flags |= LYS_SET_MIN;
2287
2288 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002289 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290
2291 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002292 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 free(buf);
2294 return LY_EVALID;
2295 }
2296
2297 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002298 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002300 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002301 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302 free(buf);
2303 return LY_EVALID;
2304 }
2305 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002306 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307 free(buf);
2308 return LY_EVALID;
2309 }
2310 *min = num;
2311 free(buf);
2312
Radek Krejci33090f92020-12-17 20:12:46 +01002313 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002315 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002316 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317 break;
2318 default:
David Sedlákb3077192019-06-19 10:55:37 +02002319 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002320 return LY_EVALID;
2321 }
2322 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002323 return ret;
2324}
2325
Michal Vaskoea5abea2018-09-18 13:10:54 +02002326/**
2327 * @brief Parse the ordered-by statement.
2328 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002329 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002330 * @param[in,out] flags Flags to write to.
2331 * @param[in,out] exts Extension instances to add to.
2332 *
2333 * @return LY_ERR values.
2334 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002335static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002336parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002338 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002339 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002340 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002341 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002342
2343 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002344 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002345 return LY_EVALID;
2346 }
2347
2348 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002349 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350
Radek Krejcif13b87b2020-12-01 22:02:17 +01002351 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002352 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002353 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002354 *flags |= LYS_ORDBY_USER;
2355 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002356 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357 free(buf);
2358 return LY_EVALID;
2359 }
2360 free(buf);
2361
Radek Krejci33090f92020-12-17 20:12:46 +01002362 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002364 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002365 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 break;
2367 default:
David Sedlákb3077192019-06-19 10:55:37 +02002368 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 return LY_EVALID;
2370 }
2371 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372 return ret;
2373}
2374
Michal Vaskoea5abea2018-09-18 13:10:54 +02002375/**
2376 * @brief Parse the leaf-list statement.
2377 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002378 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002379 * @param[in,out] siblings Siblings to add to.
2380 *
2381 * @return LY_ERR values.
2382 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002383LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002384parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002386 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002387 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002388 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002389 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 struct lysp_node_leaflist *llist;
2391
David Sedlák60adc092019-08-06 15:57:02 +02002392 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002393 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002395 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002398 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002399 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400
2401 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002402 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002404 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002405 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002407 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002408 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci33090f92020-12-17 20:12:46 +01002409 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002411 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002412 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 +02002413 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002414 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002415 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002417 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002418 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002420 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002421 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002423 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002424 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002425 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002426 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002427 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002429 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002430 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 +02002431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002432 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002433 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002435 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002436 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002438 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002439 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 +02002440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002441 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002442 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002444 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002445 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 break;
2447 default:
David Sedlákb3077192019-06-19 10:55:37 +02002448 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 return LY_EVALID;
2450 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002451 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002452 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002453checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454 /* mandatory substatements */
2455 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002456 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002457 return LY_EVALID;
2458 }
2459
2460 return ret;
2461}
2462
Michal Vaskoea5abea2018-09-18 13:10:54 +02002463/**
2464 * @brief Parse the refine statement.
2465 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002466 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002467 * @param[in,out] refines Refines to add to.
2468 *
2469 * @return LY_ERR values.
2470 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002471static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002472parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002474 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002476 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002477 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 struct lysp_refine *rf;
2479
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002480 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002481
2482 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002483 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002484 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002485 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002486
Radek Krejci33090f92020-12-17 20:12:46 +01002487 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002488 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002489 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002490 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002491 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002492 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002493 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002495 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002496 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 +02002497 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002498 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002499 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci33090f92020-12-17 20:12:46 +01002500 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002502 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002503 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002505 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002506 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002508 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002509 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002511 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002512 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002514 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002515 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 +02002516 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002517 case LY_STMT_PRESENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002518 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 +02002519 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002520 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002521 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002522 break;
2523 default:
David Sedlákb3077192019-06-19 10:55:37 +02002524 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 return LY_EVALID;
2526 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 return ret;
2529}
2530
Michal Vaskoea5abea2018-09-18 13:10:54 +02002531/**
2532 * @brief Parse the typedef statement.
2533 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002534 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002535 * @param[in,out] typedefs Typedefs to add to.
2536 *
2537 * @return LY_ERR values.
2538 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002540parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002542 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002544 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002545 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546 struct lysp_tpdf *tpdf;
2547
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002548 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002549
2550 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002551 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002552 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002553
2554 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002555 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002557 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002558 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 +02002559 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002561 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002562 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 +02002563 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002564 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002565 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 +02002566 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002567 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002568 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002569 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002570 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002571 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002573 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002574 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 +02002575 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002576 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002577 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578 break;
2579 default:
David Sedlákb3077192019-06-19 10:55:37 +02002580 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 return LY_EVALID;
2582 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002583 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002584 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002585checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586 /* mandatory substatements */
2587 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002588 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002589 return LY_EVALID;
2590 }
2591
Radek Krejcibbe09a92018-11-08 09:36:54 +01002592 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002593 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002594 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002595 }
2596
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 return ret;
2598}
2599
Michal Vaskoea5abea2018-09-18 13:10:54 +02002600/**
2601 * @brief Parse the input or output statement.
2602 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002603 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002604 * @param[in] kw Type of this particular keyword
2605 * @param[in,out] inout_p Input/output pointer to write to.
2606 *
2607 * @return LY_ERR values.
2608 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002610parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Michal Vasko7f45cf22020-10-01 12:49:44 +02002611 struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002613 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002615 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002616 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002617
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002618 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002619 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620 return LY_EVALID;
2621 }
2622
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002623 /* initiate structure */
Michal Vasko22df3f02020-08-24 13:29:22 +02002624 inout_p->nodetype = &((struct lysp_action *)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002625 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626
2627 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002628 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002630 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002631 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002632 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002633 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01002634 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002636 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01002637 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002639 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01002640 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002642 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01002643 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002645 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002646 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002648 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002649 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002651 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01002652 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002654 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002655 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002656 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002657 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002658 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002659 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002661 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002662 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002664 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002665 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 break;
2667 default:
David Sedlákb3077192019-06-19 10:55:37 +02002668 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 return LY_EVALID;
2670 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002672 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002673
Radek Krejci7fc68292019-06-12 13:51:09 +02002674checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002675 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002676 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 +02002677
Michal Vaskob83af8a2020-01-06 09:49:22 +01002678 if (!inout_p->data) {
2679 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2680 return LY_EVALID;
2681 }
2682
Michal Vasko7fbc8162018-09-17 10:35:16 +02002683 return ret;
2684}
2685
Michal Vaskoea5abea2018-09-18 13:10:54 +02002686/**
2687 * @brief Parse the action statement.
2688 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002689 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002690 * @param[in,out] actions Actions to add to.
2691 *
2692 * @return LY_ERR values.
2693 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002694LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002695parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002696{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002697 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002698 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002699 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002700 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002701 struct lysp_action *act;
2702
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002703 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704
2705 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002706 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002707 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002708 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002709 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710
Radek Krejci33090f92020-12-17 20:12:46 +01002711 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002713 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002714 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 +02002715 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002716 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002717 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002719 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002720 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 +02002721 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002722 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002723 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002724 break;
2725
Radek Krejcid6b76452019-09-03 17:03:03 +02002726 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002727 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002729 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002730 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731 break;
2732
Radek Krejcid6b76452019-09-03 17:03:03 +02002733 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002734 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002736 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002737 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002739 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002740 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741 break;
2742 default:
David Sedlákb3077192019-06-19 10:55:37 +02002743 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002744 return LY_EVALID;
2745 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002746 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002747 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002748
2749 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2750 if (!act->input.nodetype) {
2751 act->input.nodetype = LYS_INPUT;
2752 act->input.parent = (struct lysp_node *)act;
2753 }
2754 if (!act->output.nodetype) {
2755 act->output.nodetype = LYS_OUTPUT;
2756 act->output.parent = (struct lysp_node *)act;
2757 }
2758
Radek Krejci7fc68292019-06-12 13:51:09 +02002759checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002760 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002761 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002762
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763 return ret;
2764}
2765
Michal Vaskoea5abea2018-09-18 13:10:54 +02002766/**
2767 * @brief Parse the notification statement.
2768 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002769 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002770 * @param[in,out] notifs Notifications to add to.
2771 *
2772 * @return LY_ERR values.
2773 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002774LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002775parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002777 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002779 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002780 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002781 struct lysp_notif *notif;
2782
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002783 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784
2785 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002786 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002787 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002788 notif->nodetype = LYS_NOTIF;
2789 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002790
Radek Krejci33090f92020-12-17 20:12:46 +01002791 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002793 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002794 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 +02002795 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002796 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002797 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002799 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002800 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 +02002801 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002802 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002803 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002804 break;
2805
Radek Krejcid6b76452019-09-03 17:03:03 +02002806 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002807 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002808 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002809 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01002810 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002812 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01002813 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002815 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01002816 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002818 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01002819 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002821 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002822 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002824 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002825 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002827 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01002828 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002829 break;
2830
Radek Krejcid6b76452019-09-03 17:03:03 +02002831 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002832 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002833 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002835 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002836 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002837 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002838 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002839 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002841 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002842 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002843 break;
2844 default:
David Sedlákb3077192019-06-19 10:55:37 +02002845 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002846 return LY_EVALID;
2847 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002849 LY_CHECK_RET(ret);
2850checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002851 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002852 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002853
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854 return ret;
2855}
2856
Michal Vaskoea5abea2018-09-18 13:10:54 +02002857/**
2858 * @brief Parse the grouping statement.
2859 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002860 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002861 * @param[in,out] groupings Groupings to add to.
2862 *
2863 * @return LY_ERR values.
2864 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002865LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002866parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002868 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002870 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002871 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002872 struct lysp_grp *grp;
2873
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002874 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002875
2876 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002877 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002878 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002879 grp->nodetype = LYS_GROUPING;
2880 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881
Radek Krejci33090f92020-12-17 20:12:46 +01002882 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002884 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002885 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 +02002886 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002887 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002888 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 +02002889 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002890 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002891 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
2893
Radek Krejcid6b76452019-09-03 17:03:03 +02002894 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002895 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002896 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002897 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01002898 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002899 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002900 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01002901 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002902 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002903 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01002904 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002906 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01002907 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002908 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002909 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002910 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002911 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002912 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002913 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002914 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002915 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01002916 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917 break;
2918
Radek Krejcid6b76452019-09-03 17:03:03 +02002919 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002920 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)grp, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002922 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002923 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci33090f92020-12-17 20:12:46 +01002924 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002926 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002927 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002929 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002930 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci33090f92020-12-17 20:12:46 +01002931 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002933 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002934 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935 break;
2936 default:
David Sedlákb3077192019-06-19 10:55:37 +02002937 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002938 return LY_EVALID;
2939 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002940 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002941 LY_CHECK_RET(ret);
2942checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002943 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002944 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 +02002945
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 return ret;
2947}
2948
Michal Vaskoea5abea2018-09-18 13:10:54 +02002949/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002950 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002951 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002952 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002953 * @param[in,out] augments Augments to add to.
2954 *
2955 * @return LY_ERR values.
2956 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002957LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002958parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002959{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002960 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002962 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002963 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 struct lysp_augment *aug;
2965
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002966 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967
2968 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002969 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002970 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002971 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002972 aug->nodetype = LYS_AUGMENT;
2973 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974
Radek Krejci33090f92020-12-17 20:12:46 +01002975 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002977 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002978 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 +02002979 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002980 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002981 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002983 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002984 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 +02002985 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002986 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002987 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002988 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002989 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002990 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 break;
2992
Radek Krejcid6b76452019-09-03 17:03:03 +02002993 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002994 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02002995 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002996 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01002997 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002999 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003000 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003002 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003003 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003004 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003005 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003006 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003008 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003009 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003011 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003012 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003014 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003015 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003017 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003018 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 break;
3020
Radek Krejcid6b76452019-09-03 17:03:03 +02003021 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003022 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003023 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003025 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003026 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003027 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003029 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003030 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003031 break;
3032 default:
David Sedlákb3077192019-06-19 10:55:37 +02003033 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003034 return LY_EVALID;
3035 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003036 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003037 LY_CHECK_RET(ret);
3038checks:
3039 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003040 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 +02003041
Michal Vasko7fbc8162018-09-17 10:35:16 +02003042 return ret;
3043}
3044
Michal Vaskoea5abea2018-09-18 13:10:54 +02003045/**
3046 * @brief Parse the uses statement.
3047 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003048 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003049 * @param[in,out] siblings Siblings to add to.
3050 *
3051 * @return LY_ERR values.
3052 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003053LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003054parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003055{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003056 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003057 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003058 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003059 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060 struct lysp_node_uses *uses;
3061
David Sedlák60adc092019-08-06 15:57:02 +02003062 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003063 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003065 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066
Michal Vasko7fbc8162018-09-17 10:35:16 +02003067 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003068 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003069 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003070
3071 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003072 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003074 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003075 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 +02003076 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003077 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003078 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003080 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003081 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 +02003082 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003083 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003084 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003086 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003087 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003088 break;
3089
Radek Krejcid6b76452019-09-03 17:03:03 +02003090 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003091 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003093 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003094 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003096 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003097 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 break;
3099 default:
David Sedlákb3077192019-06-19 10:55:37 +02003100 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 return LY_EVALID;
3102 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003103 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003104checks:
3105 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003106 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003107
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108 return ret;
3109}
3110
Michal Vaskoea5abea2018-09-18 13:10:54 +02003111/**
3112 * @brief Parse the case statement.
3113 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003114 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003115 * @param[in,out] siblings Siblings to add to.
3116 *
3117 * @return LY_ERR values.
3118 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003119LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003120parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003122 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003124 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003125 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 struct lysp_node_case *cas;
3127
David Sedlák60adc092019-08-06 15:57:02 +02003128 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003129 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003131 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003134 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003135 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003136
3137 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003138 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003140 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003141 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 +02003142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003143 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003144 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003146 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003147 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 +02003148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003149 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003150 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003152 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003153 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 break;
3155
Radek Krejcid6b76452019-09-03 17:03:03 +02003156 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003157 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003158 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003159 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003160 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003162 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003163 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003165 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003166 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003168 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003169 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003171 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003172 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003174 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003175 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003177 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003178 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003180 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003181 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 break;
3183 default:
David Sedlákb3077192019-06-19 10:55:37 +02003184 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 return LY_EVALID;
3186 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003187 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 return ret;
3189}
3190
Michal Vaskoea5abea2018-09-18 13:10:54 +02003191/**
3192 * @brief Parse the choice statement.
3193 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003194 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003195 * @param[in,out] siblings Siblings to add to.
3196 *
3197 * @return LY_ERR values.
3198 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003199LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003200parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003202 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003204 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003205 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003206 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207
David Sedlák60adc092019-08-06 15:57:02 +02003208 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003209 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003210 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003211 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212
Michal Vasko7fbc8162018-09-17 10:35:16 +02003213 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003214 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003215 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216
3217 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003218 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003220 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003221 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003223 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003224 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 +02003225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003226 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003227 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003229 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003230 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003232 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003233 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 +02003234 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003235 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003236 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003238 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003239 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003241 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01003242 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 +02003243 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003244 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 break;
3246
Radek Krejcid6b76452019-09-03 17:03:03 +02003247 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003248 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003249 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003250 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003251 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003253 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003254 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003256 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003257 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003258 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003259 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003260 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003261 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003263 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003264 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003266 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003267 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003269 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003270 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003272 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003273 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 break;
3275 default:
David Sedlákb3077192019-06-19 10:55:37 +02003276 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 return LY_EVALID;
3278 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 return ret;
3281}
3282
Michal Vaskoea5abea2018-09-18 13:10:54 +02003283/**
3284 * @brief Parse the container statement.
3285 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003286 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003287 * @param[in,out] siblings Siblings to add to.
3288 *
3289 * @return LY_ERR values.
3290 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003291LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003292parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003293{
3294 LY_ERR ret = 0;
3295 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003296 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003297 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003298 struct lysp_node_container *cont;
3299
David Sedlák60adc092019-08-06 15:57:02 +02003300 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003301 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003302 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003303 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003306 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003307 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308
3309 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003310 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003312 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003313 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003315 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003316 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 +02003317 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003318 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003319 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003321 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003322 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 +02003323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003324 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003325 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003328 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_PRESENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003331 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 +02003332 break;
3333
Radek Krejcid6b76452019-09-03 17:03:03 +02003334 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003335 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003336 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003337 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003338 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003340 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003341 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003343 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003344 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003346 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003347 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003349 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003350 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003352 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003353 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003354 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003355 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003356 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357 break;
3358
Radek Krejcid6b76452019-09-03 17:03:03 +02003359 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003360 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003362 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003363 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003365 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003366 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003367 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003369 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003370 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003372 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003373 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003374 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003376 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003377 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 break;
3379 default:
David Sedlákb3077192019-06-19 10:55:37 +02003380 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381 return LY_EVALID;
3382 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003384checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003385 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003386 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 +02003387 return ret;
3388}
3389
Michal Vaskoea5abea2018-09-18 13:10:54 +02003390/**
3391 * @brief Parse the list statement.
3392 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003393 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003394 * @param[in,out] siblings Siblings to add to.
3395 *
3396 * @return LY_ERR values.
3397 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003398LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003399parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003400{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003401 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003403 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003404 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405 struct lysp_node_list *list;
3406
David Sedlák60adc092019-08-06 15:57:02 +02003407 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003408 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003410 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003413 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003414 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415
3416 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003417 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003419 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003420 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003422 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003423 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 +02003424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003425 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003426 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003429 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 +02003430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003432 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003434 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003435 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003437 case LY_STMT_KEY:
Radek Krejci33090f92020-12-17 20:12:46 +01003438 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 +02003439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003440 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003441 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003443 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003444 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003446 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003447 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003449 case LY_STMT_UNIQUE:
Radek Krejci33090f92020-12-17 20:12:46 +01003450 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003451 break;
3452
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003454 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003455 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003456 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003457 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003459 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003460 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003462 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003463 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003465 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003466 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003468 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003469 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003470 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003471 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003472 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003474 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003475 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 break;
3477
Radek Krejcid6b76452019-09-03 17:03:03 +02003478 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003479 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003481 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003482 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003484 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003485 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003486 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003488 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003489 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003491 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003492 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003493 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003495 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003496 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497 break;
3498 default:
David Sedlákb3077192019-06-19 10:55:37 +02003499 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 return LY_EVALID;
3501 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003502 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003503 LY_CHECK_RET(ret);
3504checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003505 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003506 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 +02003507
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508 return ret;
3509}
3510
Michal Vaskoea5abea2018-09-18 13:10:54 +02003511/**
3512 * @brief Parse the yin-element statement.
3513 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003514 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003515 * @param[in,out] flags Flags to write to.
3516 * @param[in,out] exts Extension instances to add to.
3517 *
3518 * @return LY_ERR values.
3519 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003521parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003523 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003524 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003525 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527
3528 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003529 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 return LY_EVALID;
3531 }
3532
3533 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003534 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535
Radek Krejcif13b87b2020-12-01 22:02:17 +01003536 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003538 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539 *flags |= LYS_YINELEM_FALSE;
3540 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003541 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 free(buf);
3543 return LY_EVALID;
3544 }
3545 free(buf);
3546
Radek Krejci33090f92020-12-17 20:12:46 +01003547 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003549 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003550 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003551 LY_CHECK_RET(ret);
3552 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 default:
David Sedlákb3077192019-06-19 10:55:37 +02003554 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003555 return LY_EVALID;
3556 }
3557 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 return ret;
3559}
3560
Michal Vaskoea5abea2018-09-18 13:10:54 +02003561/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003562 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003563 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003564 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003565 * @param[in,out] argument Value to write to.
3566 * @param[in,out] flags Flags to write to.
3567 * @param[in,out] exts Extension instances to add to.
3568 *
3569 * @return LY_ERR values.
3570 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003572parse_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 +02003573{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003574 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003575 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003576 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003577 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578
3579 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003580 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581 return LY_EVALID;
3582 }
3583
3584 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003585 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003586 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587
Radek Krejci33090f92020-12-17 20:12:46 +01003588 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003590 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003591 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003593 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003594 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 break;
3596 default:
David Sedlákb3077192019-06-19 10:55:37 +02003597 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 return LY_EVALID;
3599 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601 return ret;
3602}
3603
Michal Vaskoea5abea2018-09-18 13:10:54 +02003604/**
3605 * @brief Parse the extension statement.
3606 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003607 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003608 * @param[in,out] extensions Extensions to add to.
3609 *
3610 * @return LY_ERR values.
3611 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003612static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003613parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003614{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003615 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003616 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003617 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003618 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003619 struct lysp_ext *ex;
3620
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003621 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622
3623 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003624 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003625 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626
Radek Krejci33090f92020-12-17 20:12:46 +01003627 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003629 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003630 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 +02003631 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003632 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003633 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 +02003634 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003635 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003636 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003638 case LY_STMT_ARGUMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003639 LY_CHECK_RET(parse_argument(ctx, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003641 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003642 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643 break;
3644 default:
David Sedlákb3077192019-06-19 10:55:37 +02003645 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 return LY_EVALID;
3647 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003648 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 return ret;
3650}
3651
Michal Vaskoea5abea2018-09-18 13:10:54 +02003652/**
3653 * @brief Parse the deviate statement.
3654 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003655 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003656 * @param[in,out] deviates Deviates to add to.
3657 *
3658 * @return LY_ERR values.
3659 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003660LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003661parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003662{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003663 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003665 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003666 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003667 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668 struct lysp_deviate_add *d_add = NULL;
3669 struct lysp_deviate_rpl *d_rpl = NULL;
3670 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003671 const char **d_units = NULL;
3672 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003673 struct lysp_restr **d_musts = NULL;
3674 uint16_t *d_flags = 0;
3675 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676
3677 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003678 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679
Radek Krejcif13b87b2020-12-01 22:02:17 +01003680 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003681 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003682 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003683 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003684 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003686 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687 dev_mod = LYS_DEV_DELETE;
3688 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003689 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 free(buf);
3691 return LY_EVALID;
3692 }
3693 free(buf);
3694
3695 /* create structure */
3696 switch (dev_mod) {
3697 case LYS_DEV_NOT_SUPPORTED:
3698 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003699 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003700 break;
3701 case LYS_DEV_ADD:
3702 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003703 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704 d = (struct lysp_deviate *)d_add;
3705 d_units = &d_add->units;
3706 d_uniques = &d_add->uniques;
3707 d_dflts = &d_add->dflts;
3708 d_musts = &d_add->musts;
3709 d_flags = &d_add->flags;
3710 d_min = &d_add->min;
3711 d_max = &d_add->max;
3712 break;
3713 case LYS_DEV_REPLACE:
3714 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003715 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003716 d = (struct lysp_deviate *)d_rpl;
3717 d_units = &d_rpl->units;
3718 d_flags = &d_rpl->flags;
3719 d_min = &d_rpl->min;
3720 d_max = &d_rpl->max;
3721 break;
3722 case LYS_DEV_DELETE:
3723 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003724 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003725 d = (struct lysp_deviate *)d_del;
3726 d_units = &d_del->units;
3727 d_uniques = &d_del->uniques;
3728 d_dflts = &d_del->dflts;
3729 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003730 break;
3731 default:
3732 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003733 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 }
3735 d->mod = dev_mod;
3736
3737 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003738 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739
Radek Krejci33090f92020-12-17 20:12:46 +01003740 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003741 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003742 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743 switch (dev_mod) {
3744 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003745 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003746 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747 return LY_EVALID;
3748 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003749 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 break;
3751 }
3752 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003753 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003754 switch (dev_mod) {
3755 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003756 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 return LY_EVALID;
3758 case LYS_DEV_REPLACE:
Radek Krejci33090f92020-12-17 20:12:46 +01003759 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 +02003760 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 break;
3762 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003763 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764 break;
3765 }
3766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003767 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 switch (dev_mod) {
3769 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003770 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003771 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 return LY_EVALID;
3773 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003774 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 break;
3776 }
3777 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003778 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 switch (dev_mod) {
3780 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003781 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003782 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 return LY_EVALID;
3784 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003785 LY_CHECK_RET(parse_maxelements(ctx, d_max, 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_MIN_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_minelements(ctx, d_min, 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_MUST:
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_REPLACE:
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_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 break;
3809 }
3810 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003811 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 switch (dev_mod) {
3813 case LYS_DEV_NOT_SUPPORTED:
3814 case LYS_DEV_ADD:
3815 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003816 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003817 return LY_EVALID;
3818 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003819 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003820 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003821 return LY_EVALID;
3822 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003824 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003825 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 break;
3827 }
3828 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003829 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 switch (dev_mod) {
3831 case LYS_DEV_NOT_SUPPORTED:
3832 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003833 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 return LY_EVALID;
3835 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003836 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003837 break;
3838 }
3839 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003840 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 switch (dev_mod) {
3842 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003843 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844 return LY_EVALID;
3845 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003846 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 +02003847 break;
3848 }
3849 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003850 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003851 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 break;
3853 default:
David Sedlákb3077192019-06-19 10:55:37 +02003854 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855 return LY_EVALID;
3856 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003857 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858 return ret;
3859}
3860
Michal Vaskoea5abea2018-09-18 13:10:54 +02003861/**
3862 * @brief Parse the deviation statement.
3863 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003864 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003865 * @param[in,out] deviations Deviations to add to.
3866 *
3867 * @return LY_ERR values.
3868 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003869LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003870parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003871{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003872 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003873 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003874 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003875 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003876 struct lysp_deviation *dev;
3877
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003878 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003879
3880 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003881 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003882 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003883 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884
Radek Krejci33090f92020-12-17 20:12:46 +01003885 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003886 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003887 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003888 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 +02003889 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003890 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003891 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003893 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003894 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 +02003895 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003896 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003897 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 break;
3899 default:
David Sedlákb3077192019-06-19 10:55:37 +02003900 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 return LY_EVALID;
3902 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003903 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003904 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003905checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003906 /* mandatory substatements */
3907 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003908 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003909 return LY_EVALID;
3910 }
3911
3912 return ret;
3913}
3914
Michal Vaskoea5abea2018-09-18 13:10:54 +02003915/**
3916 * @brief Parse the feature statement.
3917 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003918 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003919 * @param[in,out] features Features to add to.
3920 *
3921 * @return LY_ERR values.
3922 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003923LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003924parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003926 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003927 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003928 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003929 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 struct lysp_feature *feat;
3931
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003932 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933
3934 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003935 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003936 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003937
Radek Krejci33090f92020-12-17 20:12:46 +01003938 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003940 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003941 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 +02003942 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003943 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003944 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003945 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003946 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003947 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 +02003948 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003949 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003950 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003951 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003952 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003953 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 break;
3955 default:
David Sedlákb3077192019-06-19 10:55:37 +02003956 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003957 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003958 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960 return ret;
3961}
3962
Michal Vaskoea5abea2018-09-18 13:10:54 +02003963/**
3964 * @brief Parse the identity statement.
3965 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003966 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003967 * @param[in,out] identities Identities to add to.
3968 *
3969 * @return LY_ERR values.
3970 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003971LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003972parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003973{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003974 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003975 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003976 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003977 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978 struct lysp_ident *ident;
3979
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003980 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981
3982 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003983 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003984 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003985
Radek Krejci33090f92020-12-17 20:12:46 +01003986 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003988 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003989 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 +02003990 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003991 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02003992 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci33090f92020-12-17 20:12:46 +01003993 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003995 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003996 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 +02003997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003998 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003999 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004001 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004002 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004003 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 +01004004 return LY_EVALID;
4005 }
Radek Krejci33090f92020-12-17 20:12:46 +01004006 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 +02004007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004008 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004009 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004010 break;
4011 default:
David Sedlákb3077192019-06-19 10:55:37 +02004012 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 return LY_EVALID;
4014 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004015 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016 return ret;
4017}
4018
Michal Vaskoea5abea2018-09-18 13:10:54 +02004019/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004020 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004021 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004022 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004023 * @param[in,out] mod Module to write to.
4024 *
4025 * @return LY_ERR values.
4026 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004027LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004028parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004029{
4030 LY_ERR ret = 0;
4031 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004032 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004033 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004034 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004035 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004036
Michal Vaskoc3781c32020-10-06 14:04:08 +02004037 mod->is_submod = 0;
4038
4039 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004040 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004041 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004042
Radek Krejci33090f92020-12-17 20:12:46 +01004043 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004044
Radek Krejcie3846472018-10-15 15:24:51 +02004045#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004046 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 +02004047
Michal Vasko7fbc8162018-09-17 10:35:16 +02004048 switch (kw) {
4049 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004050 case LY_STMT_NAMESPACE:
4051 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004052 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004054 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004055 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 break;
4057 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004058 case LY_STMT_INCLUDE:
4059 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004060 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004061 break;
4062 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004063 case LY_STMT_ORGANIZATION:
4064 case LY_STMT_CONTACT:
4065 case LY_STMT_DESCRIPTION:
4066 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004067 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004068 break;
4069
4070 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004071 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004072 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004074 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004075 case LY_STMT_ANYDATA:
4076 case LY_STMT_ANYXML:
4077 case LY_STMT_AUGMENT:
4078 case LY_STMT_CHOICE:
4079 case LY_STMT_CONTAINER:
4080 case LY_STMT_DEVIATION:
4081 case LY_STMT_EXTENSION:
4082 case LY_STMT_FEATURE:
4083 case LY_STMT_GROUPING:
4084 case LY_STMT_IDENTITY:
4085 case LY_STMT_LEAF:
4086 case LY_STMT_LEAF_LIST:
4087 case LY_STMT_LIST:
4088 case LY_STMT_NOTIFICATION:
4089 case LY_STMT_RPC:
4090 case LY_STMT_TYPEDEF:
4091 case LY_STMT_USES:
4092 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 mod_stmt = Y_MOD_BODY;
4094 break;
4095 default:
4096 /* error handled in the next switch */
4097 break;
4098 }
Radek Krejcie3846472018-10-15 15:24:51 +02004099#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004100
Radek Krejcie3846472018-10-15 15:24:51 +02004101 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102 switch (kw) {
4103 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004104 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004105 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004107 case LY_STMT_NAMESPACE:
Radek Krejci33090f92020-12-17 20:12:46 +01004108 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 +02004109 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004110 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +01004111 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 +02004112 break;
4113
4114 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004115 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004116 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004118 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004119 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 break;
4121
4122 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004123 case LY_STMT_ORGANIZATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004124 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 +02004125 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004126 case LY_STMT_CONTACT:
Radek Krejci33090f92020-12-17 20:12:46 +01004127 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 +02004128 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004129 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01004130 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 +02004131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004132 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004133 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 +02004134 break;
4135
4136 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004137 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004138 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139 break;
4140
4141 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004142 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004143 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004144 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004145 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004146 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004148 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004149 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004151 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004152 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004154 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004155 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004157 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004158 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004160 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004161 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004163 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004164 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 break;
4166
Radek Krejcid6b76452019-09-03 17:03:03 +02004167 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004168 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004170 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004171 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004173 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004174 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004176 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004177 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004179 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004180 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004182 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004183 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004186 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004189 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004191 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004192 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004194 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004195 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 break;
4197
4198 default:
David Sedlákb3077192019-06-19 10:55:37 +02004199 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004200 return LY_EVALID;
4201 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004202 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004203 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004204
Radek Krejci6d6556c2018-11-08 09:37:45 +01004205checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004206 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004207 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 +01004208
Michal Vasko7fbc8162018-09-17 10:35:16 +02004209 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004210 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004211 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004212 return LY_EVALID;
4213 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004214 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004215 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004216 }
4217
Radek Krejcie9e987e2018-10-31 12:50:27 +01004218 /* submodules share the namespace with the module names, so there must not be
4219 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004220 dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, mod->mod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004221 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004222 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004223 return LY_EVALID;
4224 }
4225
4226 return ret;
4227}
4228
4229/**
4230 * @brief Parse submodule substatements.
4231 *
4232 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004233 * @param[out] submod Parsed submodule structure.
4234 *
4235 * @return LY_ERR values.
4236 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004237LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004238parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004239{
4240 LY_ERR ret = 0;
4241 char *buf, *word;
4242 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004243 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004244 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4245 struct lysp_submodule *dup;
4246
Michal Vaskoc3781c32020-10-06 14:04:08 +02004247 submod->is_submod = 1;
4248
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004249 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004250 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004251 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004252
Radek Krejci33090f92020-12-17 20:12:46 +01004253 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004254
4255#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004256 if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004257
4258 switch (kw) {
4259 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004260 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004261 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4262 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004263 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004264 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4265 break;
4266 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004267 case LY_STMT_INCLUDE:
4268 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004269 CHECK_ORDER(Y_MOD_LINKAGE);
4270 break;
4271 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004272 case LY_STMT_ORGANIZATION:
4273 case LY_STMT_CONTACT:
4274 case LY_STMT_DESCRIPTION:
4275 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004276 CHECK_ORDER(Y_MOD_META);
4277 break;
4278
4279 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004280 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004281 CHECK_ORDER(Y_MOD_REVISION);
4282 break;
4283 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004284 case LY_STMT_ANYDATA:
4285 case LY_STMT_ANYXML:
4286 case LY_STMT_AUGMENT:
4287 case LY_STMT_CHOICE:
4288 case LY_STMT_CONTAINER:
4289 case LY_STMT_DEVIATION:
4290 case LY_STMT_EXTENSION:
4291 case LY_STMT_FEATURE:
4292 case LY_STMT_GROUPING:
4293 case LY_STMT_IDENTITY:
4294 case LY_STMT_LEAF:
4295 case LY_STMT_LEAF_LIST:
4296 case LY_STMT_LIST:
4297 case LY_STMT_NOTIFICATION:
4298 case LY_STMT_RPC:
4299 case LY_STMT_TYPEDEF:
4300 case LY_STMT_USES:
4301 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004302 mod_stmt = Y_MOD_BODY;
4303 break;
4304 default:
4305 /* error handled in the next switch */
4306 break;
4307 }
4308#undef CHECK_ORDER
4309
4310 prev_kw = kw;
4311 switch (kw) {
4312 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004313 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004314 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004315 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004316 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004317 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004318 break;
4319
4320 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004321 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004322 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004324 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004325 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004326 break;
4327
4328 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004329 case LY_STMT_ORGANIZATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004330 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 +01004331 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004332 case LY_STMT_CONTACT:
Radek Krejci33090f92020-12-17 20:12:46 +01004333 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 +01004334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01004336 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 +01004337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004339 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 +01004340 break;
4341
4342 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004343 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004344 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004345 break;
4346
4347 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004348 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004349 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004350 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004351 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004352 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004353 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004354 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004355 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004357 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004358 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004361 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004364 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004367 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004370 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004371 break;
4372
Radek Krejcid6b76452019-09-03 17:03:03 +02004373 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004374 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004376 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004377 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004379 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004380 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004382 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004383 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004385 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004386 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004389 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004392 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004395 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004397 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004398 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004399 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004400 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004401 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004402 break;
4403
4404 default:
David Sedlákb3077192019-06-19 10:55:37 +02004405 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004406 return LY_EVALID;
4407 }
4408 }
4409 LY_CHECK_RET(ret);
4410
4411checks:
4412 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004413 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments,
Michal Vasko69730152020-10-09 16:30:07 +02004414 submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004415
4416 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004417 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004418 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004419 return LY_EVALID;
4420 }
4421
4422 /* submodules share the namespace with the module names, so there must not be
4423 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004424 dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, submod->name, NULL);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004425 /* main modules may have different revisions */
4426 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004427 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004428 return LY_EVALID;
4429 }
4430
Michal Vasko7fbc8162018-09-17 10:35:16 +02004431 return ret;
4432}
4433
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004434/**
4435 * @brief Skip any redundant characters, namely whitespaces and comments.
4436 *
4437 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004438 * @return LY_SUCCESS on success.
4439 * @return LY_EVALID on invalid comment.
4440 */
4441static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004442skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004443{
4444 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004445 while (ctx->in->current[0]) {
4446 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004447 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004448 ly_in_skip(ctx->in, 2);
4449 LY_CHECK_RET(skip_comment(ctx, 1));
4450 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004451 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004452 ly_in_skip(ctx->in, 2);
4453 LY_CHECK_RET(skip_comment(ctx, 2));
4454 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004455 /* whitespace */
Radek Krejci33090f92020-12-17 20:12:46 +01004456 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004457 } else {
4458 break;
4459 }
4460 }
4461
4462 return LY_SUCCESS;
4463}
4464
Radek Krejcid4557c62018-09-17 11:42:09 +02004465LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004466yang_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 +02004467 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004468{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004469 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004470 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004471 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004472 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004473 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004474
David Sedlák1b623122019-08-05 15:27:49 +02004475 /* create context */
4476 *context = calloc(1, sizeof **context);
4477 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004478 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004479 (*context)->unres = main_ctx->unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004480 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004481
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004482 mod_p = calloc(1, sizeof *mod_p);
4483 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4484 mod_p->mod = main_ctx->parsed_mod->mod;
4485 mod_p->parsing = 1;
4486 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4487
Radek Krejci2efc45b2020-12-22 16:25:44 +01004488 LOG_LOCINIT(PARSER_CTX(*context), NULL, NULL, NULL, in);
4489
David Sedlák1b623122019-08-05 15:27:49 +02004490 /* map the typedefs and groupings list from main context to the submodule's context */
4491 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4492 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4493
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004494 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004495 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004496 LY_CHECK_GOTO(ret, cleanup);
4497
Michal Vasko7fbc8162018-09-17 10:35:16 +02004498 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004499 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004500 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004501
Radek Krejcid6b76452019-09-03 17:03:03 +02004502 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004503 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004504 ret = LY_EINVAL;
4505 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004506 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004507 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004508 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004509 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004510 }
4511
Michal Vasko7fbc8162018-09-17 10:35:16 +02004512 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004513 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004514 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004515
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004516 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004517 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004518 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004519 if (in->current[0]) {
4520 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004521 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004522 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004523 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004524
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004525 mod_p->parsing = 0;
4526 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004527
Radek Krejcibbe09a92018-11-08 09:36:54 +01004528cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01004529 LOG_LOCBACK(PARSER_CTX(*context), 0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004530 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004531 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004532 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004533 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004534 }
4535
4536 return ret;
4537}
4538
4539LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004540yang_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 +01004541{
4542 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004543 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004544 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004545 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004546 struct lysp_module *mod_p = NULL;
4547
David Sedlák1b623122019-08-05 15:27:49 +02004548 /* create context */
4549 *context = calloc(1, sizeof **context);
4550 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004551 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004552 (*context)->unres = unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004553 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004554
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004555 mod_p = calloc(1, sizeof *mod_p);
4556 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4557 mod_p->mod = mod;
4558 mod_p->parsing = 1;
4559 (*context)->parsed_mod = mod_p;
4560
Radek Krejci2efc45b2020-12-22 16:25:44 +01004561 LOG_LOCINIT(PARSER_CTX(*context), NULL, NULL, NULL, in);
4562
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004563 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004564 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004565 LY_CHECK_GOTO(ret, cleanup);
4566
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004567 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004568 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004569 LY_CHECK_GOTO(ret, cleanup);
4570
Radek Krejcid6b76452019-09-03 17:03:03 +02004571 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004572 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 +01004573 ret = LY_EINVAL;
4574 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004575 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004576 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004577 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004578 goto cleanup;
4579 }
4580
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004581 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004582 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004583 LY_CHECK_GOTO(ret, cleanup);
4584
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004585 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004586 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004587 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004588 if (in->current[0]) {
4589 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004590 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004591 goto cleanup;
4592 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004593
4594 mod_p->parsing = 0;
4595 mod->parsed = mod_p;
4596
4597cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01004598 LOG_LOCBACK(PARSER_CTX(*context), 0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004599 if (ret) {
4600 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004601 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004602 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004603 }
4604
Michal Vasko7fbc8162018-09-17 10:35:16 +02004605 return ret;
4606}