blob: 9fe738611dabeb98ad549852668f20b1cd79bf60 [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;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100156 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200157 } else {
158 /* note - even the multibyte character is count as 1 */
159 ++ctx->indent;
160 }
161
Radek Krejci44ceedc2018-10-02 15:54:31 +0200162 /* check character validity */
163 switch (arg) {
164 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100165 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166 break;
167 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100168 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200169 break;
170 case Y_STR_ARG:
171 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100172 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200173 break;
174 }
175
Michal Vasko7fbc8162018-09-17 10:35:16 +0200176 if (word_b && *word_b) {
177 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100178 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200179 return LY_EMEM;
180 }
181
182 /* in case of realloc */
183 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200184 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200185 /* first time we need a buffer, copy everything read up to now */
186 if (*word_len) {
187 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200188 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200189 *buf_len = *word_len;
190 memcpy(*word_b, *word_p, *word_len);
191 }
192
193 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100194 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200195 return LY_EMEM;
196 }
197
198 /* in case of realloc */
199 *word_p = *word_b;
200 } else {
201 /* just remember the first character pointer */
202 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100203 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200204 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200205 /* ... and update the word's length */
206 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100207 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200208 }
209
210 return LY_SUCCESS;
211}
212
Michal Vaskoea5abea2018-09-18 13:10:54 +0200213/**
214 * @brief Skip YANG comment in data.
215 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200216 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200217 * @param[in] comment Type of the comment to process:
218 * 1 for a one-line comment,
219 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200220 * @return LY_ERR values.
221 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200222LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100223skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200224{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100225 /* internal statuses: */
226#define COMMENT_NO 0 /* comment ended */
227#define COMMENT_LINE 1 /* in line comment */
228#define COMMENT_BLOCK 2 /* in block comment */
229#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
230
Radek Krejci33090f92020-12-17 20:12:46 +0100231 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200232 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100233 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100234 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100235 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100236 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200237 }
238 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100239 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100240 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100241 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100242 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100243 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200244 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200245 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100246 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100247 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100248 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100249 } else if (ctx->in->current[0] != '*') {
250 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100251 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200252 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100253 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200254 }
255 break;
256 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200257 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200258 }
259
Radek Krejci33090f92020-12-17 20:12:46 +0100260 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200261 ctx->indent = 0;
262 } else {
263 ++ctx->indent;
264 }
Radek Krejci33090f92020-12-17 20:12:46 +0100265 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200266 }
267
Radek Krejci33090f92020-12-17 20:12:46 +0100268 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200269 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200270 return LY_EVALID;
271 }
272
273 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100274
275#undef COMMENT_NO
276#undef COMMENT_LINE
277#undef COMMENT_BLOCK
278#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200279}
280
Michal Vaskoea5abea2018-09-18 13:10:54 +0200281/**
282 * @brief Read a quoted string from data.
283 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200284 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200285 * @param[in] arg Type of YANG keyword argument expected.
286 * @param[out] word_p Pointer to the read quoted string.
287 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
288 * set to NULL. Otherwise equal to \p word_p.
289 * @param[out] word_len Length of the read quoted string.
290 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
291 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
292 * indenation in the final quoted string.
293 *
294 * @return LY_ERR values.
295 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200296static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100297read_qstring(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200298 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200299{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100300 /* string parsing status: */
301#define STRING_ENDED 0 /* string ended */
302#define STRING_SINGLE_QUOTED 1 /* string with ' */
303#define STRING_DOUBLE_QUOTED 2 /* string with " */
304#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
305#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
306#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
307
Radek Krejci1deb5be2020-08-26 16:43:36 +0200308 uint8_t string;
309 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200310 ly_bool need_buf = 0;
311 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200312 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200313 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200314
Radek Krejci33090f92020-12-17 20:12:46 +0100315 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100316 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200317 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200318 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100319 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100320 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 }
Radek Krejci33090f92020-12-17 20:12:46 +0100322 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200323
Radek Krejci33090f92020-12-17 20:12:46 +0100324 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200325 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100326 case STRING_SINGLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100327 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200328 case '\'':
329 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100330 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100331 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200332 break;
333 default:
334 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100335 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200336 break;
337 }
338 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100339 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100340 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200341 case '\"':
342 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100343 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100344 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200345 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200346 break;
347 case '\\':
348 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100349 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200350
351 /* the backslash sequence is substituted, so we will need a buffer to store the result */
352 need_buf = 1;
353
354 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100355 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200356
357 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
358 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
359 trailing_ws = 0;
360
361 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
362 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200363 break;
364 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200365 if (current_indent < block_indent) {
366 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100367 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200368 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100369 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100370 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100371 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200372 }
373 break;
374 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200375 if (current_indent < block_indent) {
376 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100377 current_indent += Y_TAB_SPACES;
378 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200379 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200380 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100381 c = ctx->in->current;
382 ctx->in->current = " ";
383 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
384 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100385 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200386 }
Radek Krejci33090f92020-12-17 20:12:46 +0100387 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200388 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100389 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100390 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100391 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200392 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100393 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200394 }
395 break;
396 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200397 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200398 /* we will be removing the indents so we need our own buffer */
399 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200400
401 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200402 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200403
Radek Krejciff13cd12019-10-25 15:34:24 +0200404 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200405 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200406 }
407
408 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100409 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200410
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200411 /* reset context indentation counter for possible string after this one */
412 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200413 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200414 break;
415 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200416 /* first non-whitespace character, stop eating indentation */
417 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200418
419 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100420 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200421 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200422 break;
423 }
424 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100425 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200426 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100427 c = ctx->in->current;
428 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100430 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100431 /* fix false newline count in buf_store_char() */
432 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433 break;
434 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100435 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200436 break;
437 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200439 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200440 break;
441 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200442 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100443 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200444 return LY_EVALID;
445 }
446
447 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100448 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200449
Radek Krejcif13b87b2020-12-01 22:02:17 +0100450 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100451 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200452 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100453 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100454 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200455 case '+':
456 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100457 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200458 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200459 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200460 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100461 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100462 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200463 case ' ':
464 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200465 /* just skip */
466 break;
467 default:
468 /* string is finished */
469 goto string_end;
470 }
Radek Krejci33090f92020-12-17 20:12:46 +0100471 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200472 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100473 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100474 switch (ctx->in->current[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200475 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100476 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100477 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200478 case ' ':
479 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 /* skip */
481 break;
482 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100483 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200484 break;
485 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100486 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 break;
488 default:
489 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200490 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200491 return LY_EVALID;
492 }
Radek Krejci33090f92020-12-17 20:12:46 +0100493 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200494 break;
495 default:
496 return LY_EINT;
497 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200498 }
499
500string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200501 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200502 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200503 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200504 return LY_EVALID;
505 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200506 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100507
508#undef STRING_ENDED
509#undef STRING_SINGLE_QUOTED
510#undef STRING_DOUBLE_QUOTED
511#undef STRING_DOUBLE_QUOTED_ESCAPED
512#undef STRING_PAUSED_NEXTSTRING
513#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200514}
515
Michal Vaskoea5abea2018-09-18 13:10:54 +0200516/**
517 * @brief Get another YANG string from the raw data.
518 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200519 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200520 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200521 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QOUTED - see [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200522 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200523 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
524 * set to NULL. Otherwise equal to \p word_p.
525 * @param[out] word_len Length of the read string.
526 *
527 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200528 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200529LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100530get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200531 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200532{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200533 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200534 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200535
Michal Vasko7fbc8162018-09-17 10:35:16 +0200536 /* word buffer - dynamically allocated */
537 *word_b = NULL;
538
539 /* word pointer - just a pointer to data */
540 *word_p = NULL;
541
542 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100543 while (ctx->in->current[0]) {
544 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200545 case '\'':
546 case '\"':
547 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200548 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100549 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200550 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200551 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200552 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200553 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100554 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200555 }
Radek Krejci33090f92020-12-17 20:12:46 +0100556 LY_CHECK_RET(read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200557 goto str_end;
558 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100559 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200560 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100561 MOVE_INPUT(ctx, 2);
562 LY_CHECK_RET(skip_comment(ctx, 1));
563 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200564 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100565 MOVE_INPUT(ctx, 2);
566 LY_CHECK_RET(skip_comment(ctx, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200567 } else {
568 /* not a comment after all */
Radek Krejci33090f92020-12-17 20:12:46 +0100569 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200570 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200571 break;
572 case ' ':
573 if (*word_len) {
574 /* word is finished */
575 goto str_end;
576 }
Radek Krejci33090f92020-12-17 20:12:46 +0100577 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200578 break;
579 case '\t':
580 if (*word_len) {
581 /* word is finished */
582 goto str_end;
583 }
584 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100585 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200586
Radek Krejci33090f92020-12-17 20:12:46 +0100587 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200588 break;
589 case '\n':
590 if (*word_len) {
591 /* word is finished */
592 goto str_end;
593 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100594 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100595 MOVE_INPUT(ctx, 1);
596
Michal Vasko7fbc8162018-09-17 10:35:16 +0200597 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200598 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200599 break;
600 case ';':
601 case '{':
602 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
603 /* word is finished */
604 goto str_end;
605 }
606
Radek Krejci33090f92020-12-17 20:12:46 +0100607 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200608 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200609 case '}':
610 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100611 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200612 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200613 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200614 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100615 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200616 break;
617 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200618 }
619
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200620 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200621 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200622 return LY_EVALID;
623
Michal Vasko7fbc8162018-09-17 10:35:16 +0200624str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200625 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200626 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200627 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200628 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200629 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200630 *word_p = *word_b;
631 }
632
633 return LY_SUCCESS;
634}
635
Michal Vaskoea5abea2018-09-18 13:10:54 +0200636/**
637 * @brief Get another YANG keyword from the raw data.
638 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200639 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200640 * @param[out] kw YANG keyword read.
641 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
642 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
643 *
644 * @return LY_ERR values.
645 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200646LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100647get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200648{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200649 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200650 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200651 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200652
653 if (word_p) {
654 *word_p = NULL;
655 *word_len = 0;
656 }
657
658 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100659 while (ctx->in->current[0]) {
660 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200661 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100662 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200663 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100664 MOVE_INPUT(ctx, 2);
665 LY_CHECK_RET(skip_comment(ctx, 1));
666 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200667 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100668 MOVE_INPUT(ctx, 2);
669 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200671 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200672 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200673 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200674 }
Radek Krejci13028282019-06-11 14:56:48 +0200675 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200676 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200677 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100678 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200679 ctx->indent = 0;
680 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200681 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200682 /* skip whitespaces (optsep) */
683 ++ctx->indent;
684 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200685 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100687 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 break;
689 default:
690 /* either a keyword start or an invalid character */
691 goto keyword_start;
692 }
693
Radek Krejci33090f92020-12-17 20:12:46 +0100694 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 }
696
697keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100698 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100699 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200700
Michal Vasko69730152020-10-09 16:30:07 +0200701 if ((*kw == LY_STMT_SYNTAX_SEMICOLON) || (*kw == LY_STMT_SYNTAX_LEFT_BRACE) || (*kw == LY_STMT_SYNTAX_RIGHT_BRACE)) {
Radek Krejci626df482018-10-11 15:06:31 +0200702 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200703 }
704
Radek Krejcid6b76452019-09-03 17:03:03 +0200705 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200706 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100707 switch (ctx->in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200708 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200709 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200710 case ' ':
711 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200712 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200713 case ':':
714 /* keyword is not actually a keyword, but prefix of an extension.
715 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
716 * and we will be checking the keyword (extension instance) itself */
717 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100718 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200719 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200720 case '{':
721 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200722 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200723 break;
724 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100725 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200726 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100727 MOVE_INPUT(ctx, 1);
728 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200729 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 return LY_EVALID;
731 }
732 } else {
733 /* still can be an extension */
734 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200735extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100736 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
737 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '{') && (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200738 uint32_t c = 0;
739
Radek Krejci33090f92020-12-17 20:12:46 +0100740 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
741 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200742 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200743 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200744 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100745 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200746 }
Radek Krejci33090f92020-12-17 20:12:46 +0100747 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200748 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200749 return LY_EVALID;
750 }
751
752 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200753 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100754 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200755 return LY_EVALID;
756 }
757
Radek Krejcid6b76452019-09-03 17:03:03 +0200758 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 }
Radek Krejci626df482018-10-11 15:06:31 +0200760success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200761 if (word_p) {
762 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100763 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200764 }
765
766 return LY_SUCCESS;
767}
768
Michal Vaskoea5abea2018-09-18 13:10:54 +0200769/**
770 * @brief Parse extension instance substatements.
771 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200772 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200773 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200774 * @param[in] word Extension instance substatement name (keyword).
775 * @param[in] word_len Extension instance substatement name length.
776 * @param[in,out] child Children of this extension instance to add to.
777 *
778 * @return LY_ERR values.
779 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200780static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100781parse_ext_substmt(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200782 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200783{
784 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100785 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200786 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200787 struct lysp_stmt *stmt, *par_child;
788
789 stmt = calloc(1, sizeof *stmt);
790 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
791
Radek Krejcibb9b1982019-04-08 14:24:59 +0200792 /* insert into parent statements */
793 if (!*child) {
794 *child = stmt;
795 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200796 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200797 par_child->next = stmt;
798 }
799
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200800 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Radek Krejci335332a2019-09-05 13:03:35 +0200801 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200802
803 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100804 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200805
Radek Krejci0ae092d2018-09-20 16:43:19 +0200806 if (word) {
807 if (buf) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200808 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200809 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200810 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200811 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200812 }
813
Radek Krejci33090f92020-12-17 20:12:46 +0100814 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, ) {
815 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200816 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200817 return ret;
818}
819
Michal Vaskoea5abea2018-09-18 13:10:54 +0200820/**
821 * @brief Parse extension instance.
822 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200823 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200824 * @param[in] ext_name Extension instance substatement name (keyword).
825 * @param[in] ext_name_len Extension instance substatement name length.
826 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
827 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
828 * @param[in,out] exts Extension instances to add to.
829 *
830 * @return LY_ERR values.
831 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200832static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100833parse_ext(struct lys_yang_parser_ctx *ctx, const char *ext_name, size_t ext_name_len, LYEXT_SUBSTMT insubstmt,
Radek Krejci0f969882020-08-21 16:56:47 +0200834 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200835{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100836 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200838 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200839 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200840 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200842 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843
844 /* store name and insubstmt info */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200845 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200846 e->insubstmt = insubstmt;
847 e->insubstmt_index = insubstmt_index;
848
849 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100850 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200851
Radek Krejci0ae092d2018-09-20 16:43:19 +0200852 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200853 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200854 }
855
Radek Krejci33090f92020-12-17 20:12:46 +0100856 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
857 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200858 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200859 return ret;
860}
861
Michal Vaskoea5abea2018-09-18 13:10:54 +0200862/**
863 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
864 * description, etc...
865 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200866 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200867 * @param[in] substmt Type of this substatement.
868 * @param[in] substmt_index Index of this substatement.
869 * @param[in,out] value Place to store the parsed value.
870 * @param[in] arg Type of the YANG keyword argument (of the value).
871 * @param[in,out] exts Extension instances to add to.
872 *
873 * @return LY_ERR values.
874 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200875static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100876parse_text_field(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200877 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100879 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200880 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200881 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200882 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200883
884 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200885 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 return LY_EVALID;
887 }
888
889 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100890 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200891
892 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200893 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894
Radek Krejci33090f92020-12-17 20:12:46 +0100895 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200896 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200897 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100898 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200899 break;
900 default:
David Sedlákb3077192019-06-19 10:55:37 +0200901 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200902 return LY_EVALID;
903 }
904 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905 return ret;
906}
907
Michal Vaskoea5abea2018-09-18 13:10:54 +0200908/**
909 * @brief Parse the yang-version statement.
910 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200911 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100912 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200913 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200914 *
915 * @return LY_ERR values.
916 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200917static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100918parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200919{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100920 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200921 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200922 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200923 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200924
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100925 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200926 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927 return LY_EVALID;
928 }
929
930 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100931 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932
Radek Krejci96e48da2020-09-04 13:18:06 +0200933 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100934 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100935 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100936 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200937 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200938 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939 free(buf);
940 return LY_EVALID;
941 }
942 free(buf);
943
Radek Krejci33090f92020-12-17 20:12:46 +0100944 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200945 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200946 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100947 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200948 break;
949 default:
David Sedlákb3077192019-06-19 10:55:37 +0200950 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951 return LY_EVALID;
952 }
953 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200954 return ret;
955}
956
Michal Vaskoea5abea2018-09-18 13:10:54 +0200957/**
958 * @brief Parse the belongs-to statement.
959 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200960 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200961 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
962 * @param[in,out] exts Extension instances to add to.
963 *
964 * @return LY_ERR values.
965 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200966static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100967parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100969 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200970 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200971 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200972 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973
Michal Vaskoc3781c32020-10-06 14:04:08 +0200974 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200975 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200976 return LY_EVALID;
977 }
978
Michal Vaskoc3781c32020-10-06 14:04:08 +0200979 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +0100980 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200981 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +0200982 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Submodule \"belongs-to\" value \"%.*s\" does not match its module name \"%s\".",
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200983 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +0200984 free(buf);
985 return LY_EVALID;
986 }
987 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200988
Radek Krejci33090f92020-12-17 20:12:46 +0100989 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200991 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +0100992 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200994 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100995 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996 break;
997 default:
David Sedlákb3077192019-06-19 10:55:37 +0200998 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200999 return LY_EVALID;
1000 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001002 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001003checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001004 /* mandatory substatements */
1005 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001006 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007 return LY_EVALID;
1008 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009 return ret;
1010}
1011
Michal Vaskoea5abea2018-09-18 13:10:54 +02001012/**
1013 * @brief Parse the revision-date statement.
1014 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001015 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001016 * @param[in,out] rev Array to store the parsed value in.
1017 * @param[in,out] exts Extension instances to add to.
1018 *
1019 * @return LY_ERR values.
1020 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001021static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001022parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001024 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001026 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001027 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001028
1029 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001030 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031 return LY_EVALID;
1032 }
1033
1034 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001035 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036
1037 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001038 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039 free(buf);
1040 return LY_EVALID;
1041 }
1042
1043 /* store value and spend buf if allocated */
1044 strncpy(rev, word, word_len);
1045 free(buf);
1046
Radek Krejci33090f92020-12-17 20:12:46 +01001047 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001048 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001049 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001050 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051 break;
1052 default:
David Sedlákb3077192019-06-19 10:55:37 +02001053 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001054 return LY_EVALID;
1055 }
1056 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001057 return ret;
1058}
1059
Michal Vaskoea5abea2018-09-18 13:10:54 +02001060/**
1061 * @brief Parse the include statement.
1062 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001063 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001064 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001065 * @param[in,out] includes Parsed includes to add to.
1066 *
1067 * @return LY_ERR values.
1068 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001070parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071{
Radek Krejcid33273d2018-10-25 14:55:52 +02001072 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001074 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001075 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001076 struct lysp_include *inc;
1077
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001078 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079
1080 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001081 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001082
Radek Krejci011e4aa2020-09-04 15:22:31 +02001083 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001084
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001085 /* submodules share the namespace with the module names, so there must not be
1086 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001087 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001088 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001089 return LY_EVALID;
1090 }
1091
Radek Krejci33090f92020-12-17 20:12:46 +01001092 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001093 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001094 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001095 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci33090f92020-12-17 20:12:46 +01001096 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001098 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001099 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci33090f92020-12-17 20:12:46 +01001100 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001102 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001103 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001105 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001106 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 break;
1108 default:
David Sedlákb3077192019-06-19 10:55:37 +02001109 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 return LY_EVALID;
1111 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113 return ret;
1114}
1115
Michal Vaskoea5abea2018-09-18 13:10:54 +02001116/**
1117 * @brief Parse the import statement.
1118 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001119 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001120 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001121 * @param[in,out] imports Parsed imports to add to.
1122 *
1123 * @return LY_ERR values.
1124 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001125static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001126parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001128 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001129 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001130 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001131 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132 struct lysp_import *imp;
1133
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001134 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135
1136 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001137 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001138 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139
Radek Krejci33090f92020-12-17 20:12:46 +01001140 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001142 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +01001143 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskob36053d2020-03-26 15:49:30 +01001144 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001146 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001147 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci33090f92020-12-17 20:12:46 +01001148 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001150 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001151 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci33090f92020-12-17 20:12:46 +01001152 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001154 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001155 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001157 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001158 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001159 break;
1160 default:
David Sedlákb3077192019-06-19 10:55:37 +02001161 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001162 return LY_EVALID;
1163 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001165 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001166checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001167 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001168 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169
1170 return ret;
1171}
1172
Michal Vaskoea5abea2018-09-18 13:10:54 +02001173/**
1174 * @brief Parse the revision statement.
1175 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001176 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001177 * @param[in,out] revs Parsed revisions to add to.
1178 *
1179 * @return LY_ERR values.
1180 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001182parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001184 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001186 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001187 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001188 struct lysp_revision *rev;
1189
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001190 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191
1192 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001193 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194
1195 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001196 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001197 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 return LY_EVALID;
1199 }
1200
Radek Krejcib7db73a2018-10-24 14:18:40 +02001201 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001202 free(buf);
1203
Radek Krejci33090f92020-12-17 20:12:46 +01001204 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001205 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001206 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001207 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001209 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001210 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001212 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001213 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 break;
1215 default:
David Sedlákb3077192019-06-19 10:55:37 +02001216 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 return LY_EVALID;
1218 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220 return ret;
1221}
1222
Michal Vaskoea5abea2018-09-18 13:10:54 +02001223/**
1224 * @brief Parse a generic text field that can have more instances such as base.
1225 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001226 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001227 * @param[in] substmt Type of this substatement.
1228 * @param[in,out] texts Parsed values to add to.
1229 * @param[in] arg Type of the expected argument.
1230 * @param[in,out] exts Extension instances to add to.
1231 *
1232 * @return LY_ERR values.
1233 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001235parse_text_fields(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
Radek Krejci0f969882020-08-21 16:56:47 +02001236 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001238 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001240 const char **item;
1241 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001242 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001243
1244 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001245 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246
1247 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001248 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249
Radek Krejci011e4aa2020-09-04 15:22:31 +02001250 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Radek Krejci33090f92020-12-17 20:12:46 +01001251 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001253 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001254 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255 break;
1256 default:
David Sedlákb3077192019-06-19 10:55:37 +02001257 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001258 return LY_EVALID;
1259 }
1260 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261 return ret;
1262}
1263
Michal Vaskoea5abea2018-09-18 13:10:54 +02001264/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001265 * @brief Parse a generic text field that can have more instances such as base.
1266 *
1267 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001268 * @param[in] substmt Type of this substatement.
1269 * @param[in,out] qnames Parsed qnames to add to.
1270 * @param[in] arg Type of the expected argument.
1271 * @param[in,out] exts Extension instances to add to.
1272 *
1273 * @return LY_ERR values.
1274 */
1275static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001276parse_qnames(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001277 enum yang_arg arg, struct lysp_ext_instance **exts)
1278{
1279 LY_ERR ret = LY_SUCCESS;
1280 char *buf, *word;
1281 struct lysp_qname *item;
1282 size_t word_len;
1283 enum ly_stmt kw;
1284
1285 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001286 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001287
1288 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001289 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001290
1291 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001292 item->mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001293 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001294 switch (kw) {
1295 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001296 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001297 break;
1298 default:
1299 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
1300 return LY_EVALID;
1301 }
1302 }
1303 return ret;
1304}
1305
1306/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001307 * @brief Parse the config statement.
1308 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001309 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001310 * @param[in,out] flags Flags to add to.
1311 * @param[in,out] exts Extension instances to add to.
1312 *
1313 * @return LY_ERR values.
1314 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001316parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001317{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001318 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001320 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001321 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001322
1323 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001324 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325 return LY_EVALID;
1326 }
1327
1328 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001329 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330
Radek Krejcif13b87b2020-12-01 22:02:17 +01001331 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001333 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 *flags |= LYS_CONFIG_R;
1335 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001336 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337 free(buf);
1338 return LY_EVALID;
1339 }
1340 free(buf);
1341
Radek Krejci33090f92020-12-17 20:12:46 +01001342 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001343 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001344 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001345 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346 break;
1347 default:
David Sedlákb3077192019-06-19 10:55:37 +02001348 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001349 return LY_EVALID;
1350 }
1351 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001352 return ret;
1353}
1354
Michal Vaskoea5abea2018-09-18 13:10:54 +02001355/**
1356 * @brief Parse the mandatory statement.
1357 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001358 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001359 * @param[in,out] flags Flags to add to.
1360 * @param[in,out] exts Extension instances to add to.
1361 *
1362 * @return LY_ERR values.
1363 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001365parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001367 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001369 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001370 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371
1372 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001373 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374 return LY_EVALID;
1375 }
1376
1377 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001378 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001379
Radek Krejcif13b87b2020-12-01 22:02:17 +01001380 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001382 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383 *flags |= LYS_MAND_FALSE;
1384 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001385 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 free(buf);
1387 return LY_EVALID;
1388 }
1389 free(buf);
1390
Radek Krejci33090f92020-12-17 20:12:46 +01001391 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001392 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001393 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001394 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001395 break;
1396 default:
David Sedlákb3077192019-06-19 10:55:37 +02001397 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001398 return LY_EVALID;
1399 }
1400 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401 return ret;
1402}
1403
Michal Vaskoea5abea2018-09-18 13:10:54 +02001404/**
1405 * @brief Parse a restriction such as range or length.
1406 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001407 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001408 * @param[in] restr_kw Type of this particular restriction.
1409 * @param[in,out] exts Extension instances to add to.
1410 *
1411 * @return LY_ERR values.
1412 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001413static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001414parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001416 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001418 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001419 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420
1421 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001422 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001423
Michal Vaskob36053d2020-03-26 15:49:30 +01001424 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001425 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001426 restr->arg.mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001427 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001428 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001429 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001430 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001432 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001433 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001435 case LY_STMT_ERROR_APP_TAG:
Radek Krejci33090f92020-12-17 20:12:46 +01001436 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 +02001437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001438 case LY_STMT_ERROR_MESSAGE:
Radek Krejci33090f92020-12-17 20:12:46 +01001439 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 +02001440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001441 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001442 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443 break;
1444 default:
David Sedlákb3077192019-06-19 10:55:37 +02001445 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001446 return LY_EVALID;
1447 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449 return ret;
1450}
1451
Michal Vaskoea5abea2018-09-18 13:10:54 +02001452/**
1453 * @brief Parse a restriction that can have more instances such as must.
1454 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001455 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001456 * @param[in] restr_kw Type of this particular restriction.
1457 * @param[in,out] restrs Restrictions to add to.
1458 *
1459 * @return LY_ERR values.
1460 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001462parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463{
1464 struct lysp_restr *restr;
1465
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001466 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001467 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468}
1469
Michal Vaskoea5abea2018-09-18 13:10:54 +02001470/**
1471 * @brief Parse the status statement.
1472 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001473 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001474 * @param[in,out] flags Flags to add to.
1475 * @param[in,out] exts Extension instances to add to.
1476 *
1477 * @return LY_ERR values.
1478 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001479static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001480parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001482 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001484 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001485 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486
1487 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001488 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489 return LY_EVALID;
1490 }
1491
1492 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001493 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001494
Radek Krejcif13b87b2020-12-01 22:02:17 +01001495 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001497 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001499 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500 *flags |= LYS_STATUS_OBSLT;
1501 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001502 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 free(buf);
1504 return LY_EVALID;
1505 }
1506 free(buf);
1507
Radek Krejci33090f92020-12-17 20:12:46 +01001508 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001509 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001510 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001511 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001512 break;
1513 default:
David Sedlákb3077192019-06-19 10:55:37 +02001514 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515 return LY_EVALID;
1516 }
1517 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001518 return ret;
1519}
1520
Michal Vaskoea5abea2018-09-18 13:10:54 +02001521/**
1522 * @brief Parse the when statement.
1523 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001524 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001525 * @param[in,out] when_p When pointer to parse to.
1526 *
1527 * @return LY_ERR values.
1528 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001529LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001530parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001532 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001533 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001534 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001535 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536 struct lysp_when *when;
1537
1538 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001539 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001540 return LY_EVALID;
1541 }
1542
1543 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001544 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001545 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546
1547 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001548 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001549 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001550 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551
Radek Krejci33090f92020-12-17 20:12:46 +01001552 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001554 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001555 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001557 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001558 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001560 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001561 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001562 break;
1563 default:
David Sedlákb3077192019-06-19 10:55:37 +02001564 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 return LY_EVALID;
1566 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 return ret;
1569}
1570
Michal Vaskoea5abea2018-09-18 13:10:54 +02001571/**
1572 * @brief Parse the anydata or anyxml statement.
1573 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001574 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001575 * @param[in] kw Type of this particular keyword.
1576 * @param[in,out] siblings Siblings to add to.
1577 *
1578 * @return LY_ERR values.
1579 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001580LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001581parse_any(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001582{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001583 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001585 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 struct lysp_node_anydata *any;
1587
David Sedlák60adc092019-08-06 15:57:02 +02001588 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001589 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001590
Radek Krejcid6b76452019-09-03 17:03:03 +02001591 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001592 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001593
Michal Vasko7fbc8162018-09-17 10:35:16 +02001594 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001595 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001596 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001597
1598 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01001599 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001601 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001602 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001604 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001605 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001607 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01001608 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001610 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001611 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001613 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001614 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001616 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001617 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001619 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001620 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001622 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001623 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001626 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627 break;
1628 default:
David Sedlákb3077192019-06-19 10:55:37 +02001629 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejci0f969882020-08-21 16:56:47 +02001630 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631 return LY_EVALID;
1632 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 return ret;
1635}
1636
Michal Vaskoea5abea2018-09-18 13:10:54 +02001637/**
1638 * @brief Parse the value or position statement. Substatement of type enum statement.
1639 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001640 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001641 * @param[in] val_kw Type of this particular keyword.
1642 * @param[in,out] value Value to write to.
1643 * @param[in,out] flags Flags to write to.
1644 * @param[in,out] exts Extension instances to add to.
1645 *
1646 * @return LY_ERR values.
1647 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001648LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001649parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, enum ly_stmt val_kw, int64_t *value, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001650 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001651{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001652 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001654 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001655 long int num = 0;
1656 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001657 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658
1659 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001660 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 return LY_EVALID;
1662 }
1663 *flags |= LYS_SET_VALUE;
1664
1665 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001666 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667
Radek Krejcid6b76452019-09-03 17:03:03 +02001668 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) {
David Sedlákb3077192019-06-19 10:55:37 +02001669 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001670 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671 }
1672
1673 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001674 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001675 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001676 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001677 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001678 goto error;
1679 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001680 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001681 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001682 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001683 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001684 goto error;
1685 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686 }
1687 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001688 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001689 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001690 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001691 }
1692 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001693 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001694 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001695 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001696 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697 *value = num;
1698 } else {
1699 *value = unum;
1700 }
1701 free(buf);
1702
Radek Krejci33090f92020-12-17 20:12:46 +01001703 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001704 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001705 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001706 LY_CHECK_RET(parse_ext(ctx, word, word_len, val_kw == LY_STMT_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707 break;
1708 default:
David Sedlákb3077192019-06-19 10:55:37 +02001709 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710 return LY_EVALID;
1711 }
1712 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001714
1715error:
1716 free(buf);
1717 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718}
1719
Michal Vaskoea5abea2018-09-18 13:10:54 +02001720/**
1721 * @brief Parse the enum or bit statement. Substatement of type statement.
1722 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001723 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001724 * @param[in] enum_kw Type of this particular keyword.
1725 * @param[in,out] enums Enums or bits to add to.
1726 *
1727 * @return LY_ERR values.
1728 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001729static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001730parse_type_enum(struct lys_yang_parser_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001731{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001732 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001734 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001735 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736 struct lysp_type_enum *enm;
1737
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001738 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739
1740 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001741 LY_CHECK_RET(get_argument(ctx, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejcid6b76452019-09-03 17:03:03 +02001742 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001743 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001744 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001745 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001746
Radek Krejci011e4aa2020-09-04 15:22:31 +02001747 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001748 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1749
Radek Krejci33090f92020-12-17 20:12:46 +01001750 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001751 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001752 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001753 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001755 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001756 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01001757 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001759 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001760 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001762 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001763 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001764 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001765 case LY_STMT_VALUE:
1766 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Michal Vasko69730152020-10-09 16:30:07 +02001767 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001768 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001770 case LY_STMT_POSITION:
1771 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Michal Vasko69730152020-10-09 16:30:07 +02001772 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001773 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001775 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001776 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 break;
1778 default:
David Sedlákb3077192019-06-19 10:55:37 +02001779 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001780 return LY_EVALID;
1781 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001782 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001783 return ret;
1784}
1785
Michal Vaskoea5abea2018-09-18 13:10:54 +02001786/**
1787 * @brief Parse the fraction-digits statement. Substatement of type statement.
1788 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001789 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001790 * @param[in,out] fracdig Value to write to.
1791 * @param[in,out] exts Extension instances to add to.
1792 *
1793 * @return LY_ERR values.
1794 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001795static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001796parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001798 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001800 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001801 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001802 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001803
1804 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001805 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001806 return LY_EVALID;
1807 }
1808
1809 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001810 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811
1812 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001813 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814 free(buf);
1815 return LY_EVALID;
1816 }
1817
1818 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001819 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001820 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001821 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001822 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001823 free(buf);
1824 return LY_EVALID;
1825 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001826 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001827 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 free(buf);
1829 return LY_EVALID;
1830 }
1831 *fracdig = num;
1832 free(buf);
1833
Radek Krejci33090f92020-12-17 20:12:46 +01001834 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001836 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001837 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001838 break;
1839 default:
David Sedlákb3077192019-06-19 10:55:37 +02001840 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001841 return LY_EVALID;
1842 }
1843 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844 return ret;
1845}
1846
Michal Vaskoea5abea2018-09-18 13:10:54 +02001847/**
1848 * @brief Parse the require-instance statement. Substatement of type statement.
1849 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001850 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001851 * @param[in,out] reqinst Value to write to.
1852 * @param[in,out] flags Flags to write to.
1853 * @param[in,out] exts Extension instances to add to.
1854 *
1855 * @return LY_ERR values.
1856 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001857static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001858parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001859 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001860{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001861 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001863 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001864 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001865
1866 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001867 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868 return LY_EVALID;
1869 }
1870 *flags |= LYS_SET_REQINST;
1871
1872 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001873 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874
Radek Krejcif13b87b2020-12-01 22:02:17 +01001875 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001877 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001878 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879 free(buf);
1880 return LY_EVALID;
1881 }
1882 free(buf);
1883
Radek Krejci33090f92020-12-17 20:12:46 +01001884 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001886 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001887 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888 break;
1889 default:
David Sedlákb3077192019-06-19 10:55:37 +02001890 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891 return LY_EVALID;
1892 }
1893 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001894 return ret;
1895}
1896
Michal Vaskoea5abea2018-09-18 13:10:54 +02001897/**
1898 * @brief Parse the modifier statement. Substatement of type pattern statement.
1899 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001900 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001901 * @param[in,out] pat Value to write to.
1902 * @param[in,out] exts Extension instances to add to.
1903 *
1904 * @return LY_ERR values.
1905 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001907parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001909 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001911 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001912 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001913
Radek Krejcif13b87b2020-12-01 22:02:17 +01001914 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001915 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001916 return LY_EVALID;
1917 }
1918
1919 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001920 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921
Radek Krejcif13b87b2020-12-01 22:02:17 +01001922 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001923 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924 free(buf);
1925 return LY_EVALID;
1926 }
1927 free(buf);
1928
1929 /* replace the value in the dictionary */
1930 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001931 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001933 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934
Radek Krejcif13b87b2020-12-01 22:02:17 +01001935 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
1936 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001937 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001938
Radek Krejci33090f92020-12-17 20:12:46 +01001939 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001940 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001941 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001942 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001943 break;
1944 default:
David Sedlákb3077192019-06-19 10:55:37 +02001945 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946 return LY_EVALID;
1947 }
1948 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949 return ret;
1950}
1951
Michal Vaskoea5abea2018-09-18 13:10:54 +02001952/**
1953 * @brief Parse the pattern statement. Substatement of type statement.
1954 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001955 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001956 * @param[in,out] patterns Restrictions to add to.
1957 *
1958 * @return LY_ERR values.
1959 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001961parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001963 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001964 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001965 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001966 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001967 struct lysp_restr *restr;
1968
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001969 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001970
1971 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001972 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973
1974 /* add special meaning first byte */
1975 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001976 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977 word = buf;
1978 } else {
1979 buf = malloc(word_len + 2);
1980 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001981 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001982 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001983 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02001984 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001985 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
1986 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987
Radek Krejci33090f92020-12-17 20:12:46 +01001988 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001991 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001993 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001994 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001996 case LY_STMT_ERROR_APP_TAG:
Radek Krejci33090f92020-12-17 20:12:46 +01001997 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 +02001998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001999 case LY_STMT_ERROR_MESSAGE:
Radek Krejci33090f92020-12-17 20:12:46 +01002000 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 +02002001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002002 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002003 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002004 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002006 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002007 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008 break;
2009 default:
David Sedlákb3077192019-06-19 10:55:37 +02002010 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 return LY_EVALID;
2012 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002013 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 return ret;
2015}
2016
Michal Vaskoea5abea2018-09-18 13:10:54 +02002017/**
2018 * @brief Parse the type statement.
2019 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002020 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002021 * @param[in,out] type Type to wrote to.
2022 *
2023 * @return LY_ERR values.
2024 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002026parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002028 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002030 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002031 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002032 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 struct lysp_type *nest_type;
2034
2035 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002036 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037 return LY_EVALID;
2038 }
2039
2040 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002041 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002042 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002044 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002045 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002046
Radek Krejci33090f92020-12-17 20:12:46 +01002047 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002048 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002049 case LY_STMT_BASE:
Radek Krejci33090f92020-12-17 20:12:46 +01002050 LY_CHECK_RET(parse_text_fields(ctx, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002051 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002053 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002054 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002055 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002057 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002058 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002059 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002060 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002061 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002062 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002063 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002065 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002067 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 return LY_EVALID;
2069 }
2070 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002071 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002072
Radek Krejci33090f92020-12-17 20:12:46 +01002073 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002074 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002075 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002076 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002077 if (type->path) {
2078 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2079 return LY_EVALID;
2080 }
2081
Radek Krejci33090f92020-12-17 20:12:46 +01002082 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002083 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
Michal Vasko69730152020-10-09 16:30:07 +02002084 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002085 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002086 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002087 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002089 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002090 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002091 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002093 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002095 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096 return LY_EVALID;
2097 }
2098 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002099 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100
Radek Krejci33090f92020-12-17 20:12:46 +01002101 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002102 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002104 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002105 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002106 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002108 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002109 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002110 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002111 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002113 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002114 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 break;
2116 default:
David Sedlákb3077192019-06-19 10:55:37 +02002117 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002118 return LY_EVALID;
2119 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121 return ret;
2122}
2123
Michal Vaskoea5abea2018-09-18 13:10:54 +02002124/**
2125 * @brief Parse the leaf statement.
2126 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002127 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002128 * @param[in,out] siblings Siblings to add to.
2129 *
2130 * @return LY_ERR values.
2131 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002132LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002133parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002135 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002136 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002137 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002138 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 struct lysp_node_leaf *leaf;
2140
David Sedlák60adc092019-08-06 15:57:02 +02002141 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002142 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002144 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002147 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002148 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002149
2150 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002151 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002153 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002154 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002156 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002157 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002158 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002160 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002161 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002163 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002164 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002166 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002167 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002169 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002170 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002172 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002173 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002175 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002176 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002178 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002179 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002181 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002182 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002184 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002185 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002187 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002188 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 break;
2190 default:
David Sedlákb3077192019-06-19 10:55:37 +02002191 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 return LY_EVALID;
2193 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002195 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002196checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 /* mandatory substatements */
2198 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002199 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200 return LY_EVALID;
2201 }
2202
2203 return ret;
2204}
2205
Michal Vaskoea5abea2018-09-18 13:10:54 +02002206/**
2207 * @brief Parse the max-elements statement.
2208 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002209 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002210 * @param[in,out] max Value to write to.
2211 * @param[in,out] flags Flags to write to.
2212 * @param[in,out] exts Extension instances to add to.
2213 *
2214 * @return LY_ERR values.
2215 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002216LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002217parse_maxelements(struct lys_yang_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002218{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002219 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002221 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002223 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224
2225 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002226 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002227 return LY_EVALID;
2228 }
2229 *flags |= LYS_SET_MAX;
2230
2231 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002232 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233
2234 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002235 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236 free(buf);
2237 return LY_EVALID;
2238 }
2239
Radek Krejci7f9b6512019-09-18 13:11:09 +02002240 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002242 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002243 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002244 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002245 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002246 free(buf);
2247 return LY_EVALID;
2248 }
2249 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002250 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 free(buf);
2252 return LY_EVALID;
2253 }
2254
2255 *max = num;
2256 }
2257 free(buf);
2258
Radek Krejci33090f92020-12-17 20:12:46 +01002259 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002261 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002262 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 break;
2264 default:
David Sedlákb3077192019-06-19 10:55:37 +02002265 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002266 return LY_EVALID;
2267 }
2268 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269 return ret;
2270}
2271
Michal Vaskoea5abea2018-09-18 13:10:54 +02002272/**
2273 * @brief Parse the min-elements statement.
2274 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002275 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002276 * @param[in,out] min Value to write to.
2277 * @param[in,out] flags Flags to write to.
2278 * @param[in,out] exts Extension instances to add to.
2279 *
2280 * @return LY_ERR values.
2281 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002282LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002283parse_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 +02002284{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002285 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002287 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002289 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290
2291 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002292 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 return LY_EVALID;
2294 }
2295 *flags |= LYS_SET_MIN;
2296
2297 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002298 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299
2300 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
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
2306 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002307 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002309 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002310 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 free(buf);
2312 return LY_EVALID;
2313 }
2314 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002315 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 free(buf);
2317 return LY_EVALID;
2318 }
2319 *min = num;
2320 free(buf);
2321
Radek Krejci33090f92020-12-17 20:12:46 +01002322 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002323 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002324 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002325 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326 break;
2327 default:
David Sedlákb3077192019-06-19 10:55:37 +02002328 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002329 return LY_EVALID;
2330 }
2331 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002332 return ret;
2333}
2334
Michal Vaskoea5abea2018-09-18 13:10:54 +02002335/**
2336 * @brief Parse the ordered-by statement.
2337 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002338 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002339 * @param[in,out] flags Flags to write to.
2340 * @param[in,out] exts Extension instances to add to.
2341 *
2342 * @return LY_ERR values.
2343 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002345parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002347 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002349 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002350 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002351
2352 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002353 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002354 return LY_EVALID;
2355 }
2356
2357 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002358 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359
Radek Krejcif13b87b2020-12-01 22:02:17 +01002360 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002362 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363 *flags |= LYS_ORDBY_USER;
2364 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002365 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 free(buf);
2367 return LY_EVALID;
2368 }
2369 free(buf);
2370
Radek Krejci33090f92020-12-17 20:12:46 +01002371 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002373 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002374 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375 break;
2376 default:
David Sedlákb3077192019-06-19 10:55:37 +02002377 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378 return LY_EVALID;
2379 }
2380 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002381 return ret;
2382}
2383
Michal Vaskoea5abea2018-09-18 13:10:54 +02002384/**
2385 * @brief Parse the leaf-list statement.
2386 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002387 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002388 * @param[in,out] siblings Siblings to add to.
2389 *
2390 * @return LY_ERR values.
2391 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002392LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002393parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002395 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002397 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002398 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399 struct lysp_node_leaflist *llist;
2400
David Sedlák60adc092019-08-06 15:57:02 +02002401 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002402 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002404 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002405
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002407 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002408 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409
2410 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002411 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002413 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002414 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002416 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002417 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci33090f92020-12-17 20:12:46 +01002418 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002420 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002421 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 +02002422 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002423 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002424 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002425 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002426 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002427 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002429 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002430 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002432 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002433 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002435 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002436 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002438 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002439 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 +02002440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002441 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002442 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002444 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002445 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002447 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002448 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 +02002449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002450 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002451 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002453 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002454 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455 break;
2456 default:
David Sedlákb3077192019-06-19 10:55:37 +02002457 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 return LY_EVALID;
2459 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002461 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002462checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 /* mandatory substatements */
2464 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002465 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002466 return LY_EVALID;
2467 }
2468
2469 return ret;
2470}
2471
Michal Vaskoea5abea2018-09-18 13:10:54 +02002472/**
2473 * @brief Parse the refine statement.
2474 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002475 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002476 * @param[in,out] refines Refines to add to.
2477 *
2478 * @return LY_ERR values.
2479 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002481parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002482{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002483 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002484 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002485 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002486 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 struct lysp_refine *rf;
2488
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002489 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490
2491 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002492 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002493 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002494 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002495
Radek Krejci33090f92020-12-17 20:12:46 +01002496 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002498 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002499 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002501 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002502 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002504 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002505 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 +02002506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002507 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002508 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci33090f92020-12-17 20:12:46 +01002509 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002511 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002512 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002514 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002515 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002517 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002518 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002520 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002521 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002523 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002524 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 +02002525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002526 case LY_STMT_PRESENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002527 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 +02002528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002529 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002530 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 break;
2532 default:
David Sedlákb3077192019-06-19 10:55:37 +02002533 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 return LY_EVALID;
2535 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002536 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 return ret;
2538}
2539
Michal Vaskoea5abea2018-09-18 13:10:54 +02002540/**
2541 * @brief Parse the typedef statement.
2542 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002543 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002544 * @param[in,out] typedefs Typedefs to add to.
2545 *
2546 * @return LY_ERR values.
2547 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002548static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002549parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002550{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002551 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002553 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002554 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 struct lysp_tpdf *tpdf;
2556
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002557 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558
2559 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002560 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002561 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002562
2563 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002564 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002565 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002566 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002567 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 +02002568 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002569 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002570 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002571 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 +02002572 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002573 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002574 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 +02002575 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002576 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002577 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002579 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002580 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002582 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002583 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 +02002584 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002585 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002586 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 break;
2588 default:
David Sedlákb3077192019-06-19 10:55:37 +02002589 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 return LY_EVALID;
2591 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002592 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002593 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002594checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002595 /* mandatory substatements */
2596 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002597 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002598 return LY_EVALID;
2599 }
2600
Radek Krejcibbe09a92018-11-08 09:36:54 +01002601 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002602 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002603 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002604 }
2605
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 return ret;
2607}
2608
Michal Vaskoea5abea2018-09-18 13:10:54 +02002609/**
2610 * @brief Parse the input or output statement.
2611 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002612 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002613 * @param[in] kw Type of this particular keyword
2614 * @param[in,out] inout_p Input/output pointer to write to.
2615 *
2616 * @return LY_ERR values.
2617 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002619parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Michal Vasko7f45cf22020-10-01 12:49:44 +02002620 struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002622 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002624 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002627 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002628 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 return LY_EVALID;
2630 }
2631
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002632 /* initiate structure */
Michal Vasko22df3f02020-08-24 13:29:22 +02002633 inout_p->nodetype = &((struct lysp_action *)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002634 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635
2636 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002637 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002639 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002640 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002641 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002642 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01002643 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01002646 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01002649 LY_CHECK_RET(parse_container(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_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01002652 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002655 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002656 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002657 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002658 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002659 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002660 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01002661 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002663 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002664 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002665 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002666 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002667 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002668 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002670 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002671 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002673 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002674 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 break;
2676 default:
David Sedlákb3077192019-06-19 10:55:37 +02002677 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 return LY_EVALID;
2679 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002681 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002682
Radek Krejci7fc68292019-06-12 13:51:09 +02002683checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002684 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002685 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 +02002686
Michal Vaskob83af8a2020-01-06 09:49:22 +01002687 if (!inout_p->data) {
2688 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2689 return LY_EVALID;
2690 }
2691
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 return ret;
2693}
2694
Michal Vaskoea5abea2018-09-18 13:10:54 +02002695/**
2696 * @brief Parse the action statement.
2697 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002698 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002699 * @param[in,out] actions Actions to add to.
2700 *
2701 * @return LY_ERR values.
2702 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002703LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002704parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002705{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002706 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002707 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002708 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002709 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710 struct lysp_action *act;
2711
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002712 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002713
2714 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002715 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002716 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002717 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002718 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002719
Radek Krejci33090f92020-12-17 20:12:46 +01002720 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002722 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002723 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 +02002724 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002725 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002726 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002728 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002729 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 +02002730 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002731 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002732 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733 break;
2734
Radek Krejcid6b76452019-09-03 17:03:03 +02002735 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002736 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002738 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002739 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002740 break;
2741
Radek Krejcid6b76452019-09-03 17:03:03 +02002742 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002743 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002744 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002745 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002746 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002747 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002748 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002749 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002750 break;
2751 default:
David Sedlákb3077192019-06-19 10:55:37 +02002752 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002753 return LY_EVALID;
2754 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002756 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002757
2758 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2759 if (!act->input.nodetype) {
2760 act->input.nodetype = LYS_INPUT;
2761 act->input.parent = (struct lysp_node *)act;
2762 }
2763 if (!act->output.nodetype) {
2764 act->output.nodetype = LYS_OUTPUT;
2765 act->output.parent = (struct lysp_node *)act;
2766 }
2767
Radek Krejci7fc68292019-06-12 13:51:09 +02002768checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002769 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002770 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002771
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 return ret;
2773}
2774
Michal Vaskoea5abea2018-09-18 13:10:54 +02002775/**
2776 * @brief Parse the notification statement.
2777 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002778 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002779 * @param[in,out] notifs Notifications to add to.
2780 *
2781 * @return LY_ERR values.
2782 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002783LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002784parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002785{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002786 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002788 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002789 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002790 struct lysp_notif *notif;
2791
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002792 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002793
2794 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002795 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002796 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002797 notif->nodetype = LYS_NOTIF;
2798 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799
Radek Krejci33090f92020-12-17 20:12:46 +01002800 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002801 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002802 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002803 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 +02002804 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002805 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002806 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002807 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002808 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002809 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 +02002810 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002811 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002812 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002813 break;
2814
Radek Krejcid6b76452019-09-03 17:03:03 +02002815 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002816 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002817 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002818 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01002819 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01002822 LY_CHECK_RET(parse_case(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01002825 LY_CHECK_RET(parse_container(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_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01002828 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002829 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002830 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002831 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002832 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002833 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002834 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002835 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002836 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01002837 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002838 break;
2839
Radek Krejcid6b76452019-09-03 17:03:03 +02002840 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002841 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002842 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002843 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002844 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002845 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002846 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002847 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002848 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002849 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002850 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002851 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002852 break;
2853 default:
David Sedlákb3077192019-06-19 10:55:37 +02002854 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855 return LY_EVALID;
2856 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002858 LY_CHECK_RET(ret);
2859checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002860 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002861 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002862
Michal Vasko7fbc8162018-09-17 10:35:16 +02002863 return ret;
2864}
2865
Michal Vaskoea5abea2018-09-18 13:10:54 +02002866/**
2867 * @brief Parse the grouping statement.
2868 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002869 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002870 * @param[in,out] groupings Groupings to add to.
2871 *
2872 * @return LY_ERR values.
2873 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002874LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002875parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002877 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002878 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002879 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002880 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881 struct lysp_grp *grp;
2882
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002883 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002884
2885 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002886 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002887 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002888 grp->nodetype = LYS_GROUPING;
2889 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890
Radek Krejci33090f92020-12-17 20:12:46 +01002891 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002893 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002894 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 +02002895 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002896 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002897 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 +02002898 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002899 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002900 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 break;
2902
Radek Krejcid6b76452019-09-03 17:03:03 +02002903 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002904 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002905 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002906 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01002907 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01002910 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01002913 LY_CHECK_RET(parse_container(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_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01002916 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002918 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002919 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002920 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002921 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01002922 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002924 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01002925 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002926 break;
2927
Radek Krejcid6b76452019-09-03 17:03:03 +02002928 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002929 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)grp, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002930 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002931 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002932 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci33090f92020-12-17 20:12:46 +01002933 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002934 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002935 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002936 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002938 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002939 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci33090f92020-12-17 20:12:46 +01002940 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002942 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002943 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944 break;
2945 default:
David Sedlákb3077192019-06-19 10:55:37 +02002946 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002947 return LY_EVALID;
2948 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002950 LY_CHECK_RET(ret);
2951checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002952 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002953 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 +02002954
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955 return ret;
2956}
2957
Michal Vaskoea5abea2018-09-18 13:10:54 +02002958/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002959 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002960 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002961 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002962 * @param[in,out] augments Augments to add to.
2963 *
2964 * @return LY_ERR values.
2965 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002966LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002967parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002969 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002971 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002972 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973 struct lysp_augment *aug;
2974
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002975 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976
2977 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002978 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002979 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002980 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002981 aug->nodetype = LYS_AUGMENT;
2982 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983
Radek Krejci33090f92020-12-17 20:12:46 +01002984 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002986 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002987 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 +02002988 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002989 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002990 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002992 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002993 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 +02002994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002995 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002996 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002998 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002999 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 break;
3001
Radek Krejcid6b76452019-09-03 17:03:03 +02003002 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003003 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003004 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003005 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003006 LY_CHECK_RET(parse_any(ctx, kw, (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_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003009 LY_CHECK_RET(parse_case(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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003012 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003015 LY_CHECK_RET(parse_container(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_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003018 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003020 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003021 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003022 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003023 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003024 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003025 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003026 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003027 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028 break;
3029
Radek Krejcid6b76452019-09-03 17:03:03 +02003030 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003031 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003032 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003034 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003035 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003036 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003037 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003038 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003039 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040 break;
3041 default:
David Sedlákb3077192019-06-19 10:55:37 +02003042 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043 return LY_EVALID;
3044 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003046 LY_CHECK_RET(ret);
3047checks:
3048 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003049 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 +02003050
Michal Vasko7fbc8162018-09-17 10:35:16 +02003051 return ret;
3052}
3053
Michal Vaskoea5abea2018-09-18 13:10:54 +02003054/**
3055 * @brief Parse the uses statement.
3056 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003057 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003058 * @param[in,out] siblings Siblings to add to.
3059 *
3060 * @return LY_ERR values.
3061 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003062LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003063parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003065 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003067 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003068 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 struct lysp_node_uses *uses;
3070
David Sedlák60adc092019-08-06 15:57:02 +02003071 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003072 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003074 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075
Michal Vasko7fbc8162018-09-17 10:35:16 +02003076 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003077 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003078 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079
3080 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003081 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003082 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003083 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003084 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 +02003085 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003086 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003087 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003089 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003090 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 +02003091 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003092 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003093 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003094 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003095 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003096 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097 break;
3098
Radek Krejcid6b76452019-09-03 17:03:03 +02003099 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003100 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003102 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003103 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003105 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003106 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107 break;
3108 default:
David Sedlákb3077192019-06-19 10:55:37 +02003109 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 return LY_EVALID;
3111 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003113checks:
3114 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003115 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003116
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117 return ret;
3118}
3119
Michal Vaskoea5abea2018-09-18 13:10:54 +02003120/**
3121 * @brief Parse the case statement.
3122 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003123 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003124 * @param[in,out] siblings Siblings to add to.
3125 *
3126 * @return LY_ERR values.
3127 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003128LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003129parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003131 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003133 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003134 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 struct lysp_node_case *cas;
3136
David Sedlák60adc092019-08-06 15:57:02 +02003137 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003138 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003140 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003143 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003144 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145
3146 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003147 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003149 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003150 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 +02003151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003152 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003153 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003155 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003156 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 +02003157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003158 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003159 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003161 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003162 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003163 break;
3164
Radek Krejcid6b76452019-09-03 17:03:03 +02003165 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003166 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003167 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003168 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003169 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003171 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003172 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003174 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003175 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003177 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003178 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003180 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003181 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003183 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003184 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003186 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003187 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003189 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003190 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191 break;
3192 default:
David Sedlákb3077192019-06-19 10:55:37 +02003193 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 return LY_EVALID;
3195 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003197 return ret;
3198}
3199
Michal Vaskoea5abea2018-09-18 13:10:54 +02003200/**
3201 * @brief Parse the choice statement.
3202 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003203 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003204 * @param[in,out] siblings Siblings to add to.
3205 *
3206 * @return LY_ERR values.
3207 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003208LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003209parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003211 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003213 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003214 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003215 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216
David Sedlák60adc092019-08-06 15:57:02 +02003217 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003218 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003219 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003220 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003223 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003224 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225
3226 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003227 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003229 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003230 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003232 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003233 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 +02003234 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003235 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003236 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003238 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003239 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003241 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003242 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 +02003243 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003244 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003245 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003247 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003248 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003250 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01003251 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 +02003252 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003253 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 break;
3255
Radek Krejcid6b76452019-09-03 17:03:03 +02003256 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003257 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003258 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003259 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003260 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003261 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003262 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003263 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003265 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003266 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003267 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003269 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003270 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003272 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003273 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003275 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003276 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003278 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003279 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003281 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003282 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283 break;
3284 default:
David Sedlákb3077192019-06-19 10:55:37 +02003285 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 return LY_EVALID;
3287 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003289 return ret;
3290}
3291
Michal Vaskoea5abea2018-09-18 13:10:54 +02003292/**
3293 * @brief Parse the container statement.
3294 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003295 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003296 * @param[in,out] siblings Siblings to add to.
3297 *
3298 * @return LY_ERR values.
3299 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003300LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003301parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003302{
3303 LY_ERR ret = 0;
3304 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003305 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003306 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 struct lysp_node_container *cont;
3308
David Sedlák60adc092019-08-06 15:57:02 +02003309 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003310 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003312 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003315 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003316 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317
3318 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003319 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003321 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003322 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003324 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003325 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 +02003326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003328 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003331 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 +02003332 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003333 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003334 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003336 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003337 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003339 case LY_STMT_PRESENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003340 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 +02003341 break;
3342
Radek Krejcid6b76452019-09-03 17:03:03 +02003343 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003344 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003345 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003346 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003347 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003349 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003350 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003352 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003353 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003354 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003355 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003356 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003358 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003359 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003360 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003361 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003362 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003364 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003365 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 break;
3367
Radek Krejcid6b76452019-09-03 17:03:03 +02003368 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003369 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003371 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003372 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003374 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003375 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003376 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003378 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003379 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003381 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003382 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003383 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003385 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003386 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003387 break;
3388 default:
David Sedlákb3077192019-06-19 10:55:37 +02003389 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 return LY_EVALID;
3391 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003393checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003394 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003395 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 +02003396 return ret;
3397}
3398
Michal Vaskoea5abea2018-09-18 13:10:54 +02003399/**
3400 * @brief Parse the list statement.
3401 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003402 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003403 * @param[in,out] siblings Siblings to add to.
3404 *
3405 * @return LY_ERR values.
3406 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003407LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003408parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003410 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003412 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 struct lysp_node_list *list;
3415
David Sedlák60adc092019-08-06 15:57:02 +02003416 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003417 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003419 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003422 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003423 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424
3425 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003426 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003429 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003432 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 +02003433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003434 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003435 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003437 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003438 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 +02003439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003440 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003441 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003443 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003444 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003446 case LY_STMT_KEY:
Radek Krejci33090f92020-12-17 20:12:46 +01003447 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 +02003448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003449 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003450 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003452 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003453 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003455 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003456 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003458 case LY_STMT_UNIQUE:
Radek Krejci33090f92020-12-17 20:12:46 +01003459 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003460 break;
3461
Radek Krejcid6b76452019-09-03 17:03:03 +02003462 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003463 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003464 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003465 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003466 LY_CHECK_RET(parse_any(ctx, kw, (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_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003469 LY_CHECK_RET(parse_choice(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_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003472 LY_CHECK_RET(parse_container(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_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003475 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003477 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003478 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003479 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003480 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003481 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003482 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003483 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003484 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 break;
3486
Radek Krejcid6b76452019-09-03 17:03:03 +02003487 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003488 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003490 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003491 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003492 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003493 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003494 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003495 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003497 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003498 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003500 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003501 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003502 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003504 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003505 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003506 break;
3507 default:
David Sedlákb3077192019-06-19 10:55:37 +02003508 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 return LY_EVALID;
3510 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003512 LY_CHECK_RET(ret);
3513checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003514 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003515 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 +02003516
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 return ret;
3518}
3519
Michal Vaskoea5abea2018-09-18 13:10:54 +02003520/**
3521 * @brief Parse the yin-element statement.
3522 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003523 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003524 * @param[in,out] flags Flags to write to.
3525 * @param[in,out] exts Extension instances to add to.
3526 *
3527 * @return LY_ERR values.
3528 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003530parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003532 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003534 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003535 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003536
3537 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003538 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539 return LY_EVALID;
3540 }
3541
3542 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003543 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544
Radek Krejcif13b87b2020-12-01 22:02:17 +01003545 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003547 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548 *flags |= LYS_YINELEM_FALSE;
3549 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003550 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 free(buf);
3552 return LY_EVALID;
3553 }
3554 free(buf);
3555
Radek Krejci33090f92020-12-17 20:12:46 +01003556 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003558 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003559 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003560 LY_CHECK_RET(ret);
3561 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003562 default:
David Sedlákb3077192019-06-19 10:55:37 +02003563 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 return LY_EVALID;
3565 }
3566 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 return ret;
3568}
3569
Michal Vaskoea5abea2018-09-18 13:10:54 +02003570/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003571 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003572 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003573 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003574 * @param[in,out] argument Value to write to.
3575 * @param[in,out] flags Flags to write to.
3576 * @param[in,out] exts Extension instances to add to.
3577 *
3578 * @return LY_ERR values.
3579 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003581parse_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 +02003582{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003583 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003585 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003586 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587
3588 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003589 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 return LY_EVALID;
3591 }
3592
3593 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003594 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003595 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596
Radek Krejci33090f92020-12-17 20:12:46 +01003597 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003599 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003600 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003602 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003603 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604 break;
3605 default:
David Sedlákb3077192019-06-19 10:55:37 +02003606 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003607 return LY_EVALID;
3608 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003610 return ret;
3611}
3612
Michal Vaskoea5abea2018-09-18 13:10:54 +02003613/**
3614 * @brief Parse the extension statement.
3615 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003616 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003617 * @param[in,out] extensions Extensions to add to.
3618 *
3619 * @return LY_ERR values.
3620 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003622parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003624 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003626 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003627 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 struct lysp_ext *ex;
3629
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003630 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631
3632 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003633 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003634 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635
Radek Krejci33090f92020-12-17 20:12:46 +01003636 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003638 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003639 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 +02003640 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003641 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003642 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 +02003643 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003644 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003645 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003647 case LY_STMT_ARGUMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003648 LY_CHECK_RET(parse_argument(ctx, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003650 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003651 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652 break;
3653 default:
David Sedlákb3077192019-06-19 10:55:37 +02003654 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003655 return LY_EVALID;
3656 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658 return ret;
3659}
3660
Michal Vaskoea5abea2018-09-18 13:10:54 +02003661/**
3662 * @brief Parse the deviate statement.
3663 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003664 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003665 * @param[in,out] deviates Deviates to add to.
3666 *
3667 * @return LY_ERR values.
3668 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003669LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003670parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003672 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003674 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003675 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003676 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003677 struct lysp_deviate_add *d_add = NULL;
3678 struct lysp_deviate_rpl *d_rpl = NULL;
3679 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003680 const char **d_units = NULL;
3681 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003682 struct lysp_restr **d_musts = NULL;
3683 uint16_t *d_flags = 0;
3684 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685
3686 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003687 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688
Radek Krejcif13b87b2020-12-01 22:02:17 +01003689 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003691 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003693 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003695 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003696 dev_mod = LYS_DEV_DELETE;
3697 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003698 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699 free(buf);
3700 return LY_EVALID;
3701 }
3702 free(buf);
3703
3704 /* create structure */
3705 switch (dev_mod) {
3706 case LYS_DEV_NOT_SUPPORTED:
3707 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003708 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709 break;
3710 case LYS_DEV_ADD:
3711 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003712 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003713 d = (struct lysp_deviate *)d_add;
3714 d_units = &d_add->units;
3715 d_uniques = &d_add->uniques;
3716 d_dflts = &d_add->dflts;
3717 d_musts = &d_add->musts;
3718 d_flags = &d_add->flags;
3719 d_min = &d_add->min;
3720 d_max = &d_add->max;
3721 break;
3722 case LYS_DEV_REPLACE:
3723 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003724 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003725 d = (struct lysp_deviate *)d_rpl;
3726 d_units = &d_rpl->units;
3727 d_flags = &d_rpl->flags;
3728 d_min = &d_rpl->min;
3729 d_max = &d_rpl->max;
3730 break;
3731 case LYS_DEV_DELETE:
3732 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003733 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 d = (struct lysp_deviate *)d_del;
3735 d_units = &d_del->units;
3736 d_uniques = &d_del->uniques;
3737 d_dflts = &d_del->dflts;
3738 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739 break;
3740 default:
3741 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003742 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743 }
3744 d->mod = dev_mod;
3745
3746 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003747 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748
Radek Krejci33090f92020-12-17 20:12:46 +01003749 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003751 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003752 switch (dev_mod) {
3753 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003754 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003755 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003756 return LY_EVALID;
3757 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003758 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759 break;
3760 }
3761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003762 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003763 switch (dev_mod) {
3764 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003765 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766 return LY_EVALID;
3767 case LYS_DEV_REPLACE:
Radek Krejci33090f92020-12-17 20:12:46 +01003768 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 +02003769 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003770 break;
3771 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003772 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003773 break;
3774 }
3775 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003776 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003777 switch (dev_mod) {
3778 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003779 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003780 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 return LY_EVALID;
3782 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003783 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003784 break;
3785 }
3786 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003787 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 switch (dev_mod) {
3789 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003790 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003791 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003792 return LY_EVALID;
3793 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003794 LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 break;
3796 }
3797 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003798 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799 switch (dev_mod) {
3800 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003801 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003802 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003803 return LY_EVALID;
3804 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003805 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003806 break;
3807 }
3808 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003809 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003810 switch (dev_mod) {
3811 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003812 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003813 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 return LY_EVALID;
3815 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003816 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003817 break;
3818 }
3819 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003820 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003821 switch (dev_mod) {
3822 case LYS_DEV_NOT_SUPPORTED:
3823 case LYS_DEV_ADD:
3824 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003825 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 return LY_EVALID;
3827 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003828 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003829 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003830 return LY_EVALID;
3831 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003833 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003834 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835 break;
3836 }
3837 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003838 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003839 switch (dev_mod) {
3840 case LYS_DEV_NOT_SUPPORTED:
3841 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003842 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003843 return LY_EVALID;
3844 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003845 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846 break;
3847 }
3848 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003849 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850 switch (dev_mod) {
3851 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003852 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003853 return LY_EVALID;
3854 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003855 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 +02003856 break;
3857 }
3858 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003859 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003860 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 break;
3862 default:
David Sedlákb3077192019-06-19 10:55:37 +02003863 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 return LY_EVALID;
3865 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003867 return ret;
3868}
3869
Michal Vaskoea5abea2018-09-18 13:10:54 +02003870/**
3871 * @brief Parse the deviation statement.
3872 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003873 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003874 * @param[in,out] deviations Deviations to add to.
3875 *
3876 * @return LY_ERR values.
3877 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003878LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003879parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003881 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003882 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003883 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003884 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885 struct lysp_deviation *dev;
3886
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003887 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888
3889 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003890 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003891 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003892 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003893
Radek Krejci33090f92020-12-17 20:12:46 +01003894 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003896 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003897 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 +02003898 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003899 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003900 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003902 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003903 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 +02003904 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003905 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003906 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907 break;
3908 default:
David Sedlákb3077192019-06-19 10:55:37 +02003909 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910 return LY_EVALID;
3911 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003913 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003914checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003915 /* mandatory substatements */
3916 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003917 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003918 return LY_EVALID;
3919 }
3920
3921 return ret;
3922}
3923
Michal Vaskoea5abea2018-09-18 13:10:54 +02003924/**
3925 * @brief Parse the feature statement.
3926 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003927 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003928 * @param[in,out] features Features to add to.
3929 *
3930 * @return LY_ERR values.
3931 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003932LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003933parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003935 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003937 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003938 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939 struct lysp_feature *feat;
3940
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003941 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942
3943 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003944 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003945 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003946
Radek Krejci33090f92020-12-17 20:12:46 +01003947 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003948 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003949 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003950 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 +02003951 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003952 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003953 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003955 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003956 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 +02003957 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003958 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003959 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003961 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003962 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003963 break;
3964 default:
David Sedlákb3077192019-06-19 10:55:37 +02003965 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003966 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003968 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969 return ret;
3970}
3971
Michal Vaskoea5abea2018-09-18 13:10:54 +02003972/**
3973 * @brief Parse the identity statement.
3974 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003975 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003976 * @param[in,out] identities Identities to add to.
3977 *
3978 * @return LY_ERR values.
3979 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003980LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003981parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003982{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003983 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003985 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003986 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 struct lysp_ident *ident;
3988
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003989 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990
3991 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003992 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003993 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003994
Radek Krejci33090f92020-12-17 20:12:46 +01003995 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003997 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003998 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 +02003999 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004000 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004001 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci33090f92020-12-17 20:12:46 +01004002 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004004 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004005 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 +02004006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004007 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004008 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004010 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004011 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004012 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 +01004013 return LY_EVALID;
4014 }
Radek Krejci33090f92020-12-17 20:12:46 +01004015 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 +02004016 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004017 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004018 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019 break;
4020 default:
David Sedlákb3077192019-06-19 10:55:37 +02004021 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004022 return LY_EVALID;
4023 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004025 return ret;
4026}
4027
Michal Vaskoea5abea2018-09-18 13:10:54 +02004028/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004029 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004030 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004031 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004032 * @param[in,out] mod Module to write to.
4033 *
4034 * @return LY_ERR values.
4035 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004036LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004037parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004038{
4039 LY_ERR ret = 0;
4040 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004041 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004042 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004044 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004045
Michal Vaskoc3781c32020-10-06 14:04:08 +02004046 mod->is_submod = 0;
4047
4048 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004049 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004050 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004051
Radek Krejci33090f92020-12-17 20:12:46 +01004052 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053
Radek Krejcie3846472018-10-15 15:24:51 +02004054#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004055 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 +02004056
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 switch (kw) {
4058 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004059 case LY_STMT_NAMESPACE:
4060 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004061 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004063 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004064 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004065 break;
4066 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004067 case LY_STMT_INCLUDE:
4068 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004069 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004070 break;
4071 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004072 case LY_STMT_ORGANIZATION:
4073 case LY_STMT_CONTACT:
4074 case LY_STMT_DESCRIPTION:
4075 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004076 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004077 break;
4078
4079 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004080 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004081 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004082 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004083 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004084 case LY_STMT_ANYDATA:
4085 case LY_STMT_ANYXML:
4086 case LY_STMT_AUGMENT:
4087 case LY_STMT_CHOICE:
4088 case LY_STMT_CONTAINER:
4089 case LY_STMT_DEVIATION:
4090 case LY_STMT_EXTENSION:
4091 case LY_STMT_FEATURE:
4092 case LY_STMT_GROUPING:
4093 case LY_STMT_IDENTITY:
4094 case LY_STMT_LEAF:
4095 case LY_STMT_LEAF_LIST:
4096 case LY_STMT_LIST:
4097 case LY_STMT_NOTIFICATION:
4098 case LY_STMT_RPC:
4099 case LY_STMT_TYPEDEF:
4100 case LY_STMT_USES:
4101 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102 mod_stmt = Y_MOD_BODY;
4103 break;
4104 default:
4105 /* error handled in the next switch */
4106 break;
4107 }
Radek Krejcie3846472018-10-15 15:24:51 +02004108#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004109
Radek Krejcie3846472018-10-15 15:24:51 +02004110 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111 switch (kw) {
4112 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004113 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004114 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004116 case LY_STMT_NAMESPACE:
Radek Krejci33090f92020-12-17 20:12:46 +01004117 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 +02004118 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004119 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +01004120 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 +02004121 break;
4122
4123 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004124 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004125 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004127 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004128 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004129 break;
4130
4131 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004132 case LY_STMT_ORGANIZATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004133 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 +02004134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004135 case LY_STMT_CONTACT:
Radek Krejci33090f92020-12-17 20:12:46 +01004136 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 +02004137 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004138 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01004139 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 +02004140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004141 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004142 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 +02004143 break;
4144
4145 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004146 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004147 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004148 break;
4149
4150 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004151 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004152 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004153 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004154 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004155 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004157 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004158 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004160 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004161 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004163 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004164 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004167 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004169 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004170 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004172 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004173 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004174 break;
4175
Radek Krejcid6b76452019-09-03 17:03:03 +02004176 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004177 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004179 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004180 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004182 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004183 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004186 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004189 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004191 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004192 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004194 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004195 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004197 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004198 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004200 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004201 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004203 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004204 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 break;
4206
4207 default:
David Sedlákb3077192019-06-19 10:55:37 +02004208 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004209 return LY_EVALID;
4210 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004211 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004212 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004213
Radek Krejci6d6556c2018-11-08 09:37:45 +01004214checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004215 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004216 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 +01004217
Michal Vasko7fbc8162018-09-17 10:35:16 +02004218 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004219 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004220 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004221 return LY_EVALID;
4222 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004223 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004224 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004225 }
4226
Radek Krejcie9e987e2018-10-31 12:50:27 +01004227 /* submodules share the namespace with the module names, so there must not be
4228 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004229 dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, mod->mod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004230 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004231 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004232 return LY_EVALID;
4233 }
4234
4235 return ret;
4236}
4237
4238/**
4239 * @brief Parse submodule substatements.
4240 *
4241 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004242 * @param[out] submod Parsed submodule structure.
4243 *
4244 * @return LY_ERR values.
4245 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004246LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004247parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004248{
4249 LY_ERR ret = 0;
4250 char *buf, *word;
4251 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004252 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004253 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4254 struct lysp_submodule *dup;
4255
Michal Vaskoc3781c32020-10-06 14:04:08 +02004256 submod->is_submod = 1;
4257
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004258 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004259 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004260 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004261
Radek Krejci33090f92020-12-17 20:12:46 +01004262 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004263
4264#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004265 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 +01004266
4267 switch (kw) {
4268 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004269 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004270 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004272 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004273 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4274 break;
4275 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004276 case LY_STMT_INCLUDE:
4277 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004278 CHECK_ORDER(Y_MOD_LINKAGE);
4279 break;
4280 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004281 case LY_STMT_ORGANIZATION:
4282 case LY_STMT_CONTACT:
4283 case LY_STMT_DESCRIPTION:
4284 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004285 CHECK_ORDER(Y_MOD_META);
4286 break;
4287
4288 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004289 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004290 CHECK_ORDER(Y_MOD_REVISION);
4291 break;
4292 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004293 case LY_STMT_ANYDATA:
4294 case LY_STMT_ANYXML:
4295 case LY_STMT_AUGMENT:
4296 case LY_STMT_CHOICE:
4297 case LY_STMT_CONTAINER:
4298 case LY_STMT_DEVIATION:
4299 case LY_STMT_EXTENSION:
4300 case LY_STMT_FEATURE:
4301 case LY_STMT_GROUPING:
4302 case LY_STMT_IDENTITY:
4303 case LY_STMT_LEAF:
4304 case LY_STMT_LEAF_LIST:
4305 case LY_STMT_LIST:
4306 case LY_STMT_NOTIFICATION:
4307 case LY_STMT_RPC:
4308 case LY_STMT_TYPEDEF:
4309 case LY_STMT_USES:
4310 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004311 mod_stmt = Y_MOD_BODY;
4312 break;
4313 default:
4314 /* error handled in the next switch */
4315 break;
4316 }
4317#undef CHECK_ORDER
4318
4319 prev_kw = kw;
4320 switch (kw) {
4321 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004322 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004323 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004325 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004326 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004327 break;
4328
4329 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004330 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004331 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004332 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004333 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004334 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004335 break;
4336
4337 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_ORGANIZATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004339 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 +01004340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004341 case LY_STMT_CONTACT:
Radek Krejci33090f92020-12-17 20:12:46 +01004342 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 +01004343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004344 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01004345 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 +01004346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004347 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004348 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 +01004349 break;
4350
4351 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004352 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004353 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004354 break;
4355
4356 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004357 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004358 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004359 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004361 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004364 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004367 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004370 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004373 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004376 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004379 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004380 break;
4381
Radek Krejcid6b76452019-09-03 17:03:03 +02004382 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004383 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004385 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004386 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004389 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004392 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004395 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004397 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004398 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004399 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004400 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004401 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004403 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004404 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004406 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004407 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004409 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004410 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004411 break;
4412
4413 default:
David Sedlákb3077192019-06-19 10:55:37 +02004414 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004415 return LY_EVALID;
4416 }
4417 }
4418 LY_CHECK_RET(ret);
4419
4420checks:
4421 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004422 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments,
Michal Vasko69730152020-10-09 16:30:07 +02004423 submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004424
4425 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004426 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004427 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004428 return LY_EVALID;
4429 }
4430
4431 /* submodules share the namespace with the module names, so there must not be
4432 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004433 dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, submod->name, NULL);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004434 /* main modules may have different revisions */
4435 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004436 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004437 return LY_EVALID;
4438 }
4439
Michal Vasko7fbc8162018-09-17 10:35:16 +02004440 return ret;
4441}
4442
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004443/**
4444 * @brief Skip any redundant characters, namely whitespaces and comments.
4445 *
4446 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004447 * @return LY_SUCCESS on success.
4448 * @return LY_EVALID on invalid comment.
4449 */
4450static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004451skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004452{
4453 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004454 while (ctx->in->current[0]) {
4455 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004456 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004457 ly_in_skip(ctx->in, 2);
4458 LY_CHECK_RET(skip_comment(ctx, 1));
4459 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004460 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004461 ly_in_skip(ctx->in, 2);
4462 LY_CHECK_RET(skip_comment(ctx, 2));
4463 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004464 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004465 if (ctx->in->current[0] == '\n') {
4466 LY_IN_NEW_LINE(ctx->in);
4467 }
Radek Krejci33090f92020-12-17 20:12:46 +01004468 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004469 } else {
4470 break;
4471 }
4472 }
4473
4474 return LY_SUCCESS;
4475}
4476
Radek Krejcid4557c62018-09-17 11:42:09 +02004477LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004478yang_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 +02004479 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004480{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004481 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004482 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004483 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004484 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004485 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004486
David Sedlák1b623122019-08-05 15:27:49 +02004487 /* create context */
4488 *context = calloc(1, sizeof **context);
4489 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004490 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004491 (*context)->unres = main_ctx->unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004492 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004493
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004494 mod_p = calloc(1, sizeof *mod_p);
4495 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4496 mod_p->mod = main_ctx->parsed_mod->mod;
4497 mod_p->parsing = 1;
4498 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4499
Radek Krejciddace2c2021-01-08 11:30:56 +01004500 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004501
David Sedlák1b623122019-08-05 15:27:49 +02004502 /* map the typedefs and groupings list from main context to the submodule's context */
4503 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4504 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4505
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004506 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004507 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004508 LY_CHECK_GOTO(ret, cleanup);
4509
Michal Vasko7fbc8162018-09-17 10:35:16 +02004510 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004511 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004512 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004513
Radek Krejcid6b76452019-09-03 17:03:03 +02004514 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004515 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004516 ret = LY_EINVAL;
4517 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004518 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004519 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004520 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004521 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004522 }
4523
Michal Vasko7fbc8162018-09-17 10:35:16 +02004524 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004525 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004526 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004527
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004528 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004529 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004530 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004531 if (in->current[0]) {
4532 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004533 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004534 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004535 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004536
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004537 mod_p->parsing = 0;
4538 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004539
Radek Krejcibbe09a92018-11-08 09:36:54 +01004540cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004541 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004542 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004543 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004544 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004545 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004546 }
4547
4548 return ret;
4549}
4550
4551LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004552yang_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 +01004553{
4554 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004555 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004556 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004557 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004558 struct lysp_module *mod_p = NULL;
4559
David Sedlák1b623122019-08-05 15:27:49 +02004560 /* create context */
4561 *context = calloc(1, sizeof **context);
4562 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004563 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004564 (*context)->unres = unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004565 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004566
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004567 mod_p = calloc(1, sizeof *mod_p);
4568 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4569 mod_p->mod = mod;
4570 mod_p->parsing = 1;
4571 (*context)->parsed_mod = mod_p;
4572
Radek Krejciddace2c2021-01-08 11:30:56 +01004573 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004574
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004575 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004576 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004577 LY_CHECK_GOTO(ret, cleanup);
4578
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004579 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004580 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004581 LY_CHECK_GOTO(ret, cleanup);
4582
Radek Krejcid6b76452019-09-03 17:03:03 +02004583 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004584 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 +01004585 ret = LY_EINVAL;
4586 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004587 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004588 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004589 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004590 goto cleanup;
4591 }
4592
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004593 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004594 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004595 LY_CHECK_GOTO(ret, cleanup);
4596
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004597 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004598 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004599 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004600 if (in->current[0]) {
4601 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004602 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004603 goto cleanup;
4604 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004605
4606 mod_p->parsing = 0;
4607 mod->parsed = mod_p;
4608
4609cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004610 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004611 if (ret) {
4612 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004613 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004614 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004615 }
4616
Michal Vasko7fbc8162018-09-17 10:35:16 +02004617 return ret;
4618}