blob: c09700b111dce37ac4097e5f1f17d651c0be2a3c [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"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020028#include "path.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "set.h"
31#include "tree.h"
32#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020033#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020034
Radek Krejciceaf2122019-01-02 15:03:26 +010035/**
36 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
37 * @param[in] CTX yang parser context to access libyang context.
38 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
39 * @param[out] TARGET variable where to store the pointer to the inserted value.
40 * @param[in] WORD string to store.
41 * @param[in] LEN length of the string in WORD to store.
42 */
Radek Krejci9fcacc12018-10-11 15:59:11 +020043#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
44 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
Radek Krejcif09e4e82019-06-14 15:08:11 +020045 else {(TARGET) = lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020046
Radek Krejciceaf2122019-01-02 15:03:26 +010047/**
Michal Vasko63f3d842020-07-08 10:10:14 +020048 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Radek Krejciceaf2122019-01-02 15:03:26 +010049 * @param[in] CTX yang parser context to update its indent value.
Michal Vasko63f3d842020-07-08 10:10:14 +020050 * @param[in,out] IN input structure
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 */
Michal Vasko63f3d842020-07-08 10:10:14 +020053#define MOVE_INPUT(CTX, IN, COUNT) ly_in_skip(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 Vasko63f3d842020-07-08 10:10:14 +020059 * @param[in] IN Input structure to read from.
Michal Vaskoea5abea2018-09-18 13:10:54 +020060 * @param[out] KW YANG keyword read.
61 * @param[out] WORD Pointer to the keyword itself.
62 * @param[out] WORD_LEN Length of the keyword.
63 * @param[out] ERR Variable for error storing.
64 *
65 * @return In case there are no substatements or a fatal error encountered.
66 */
Michal Vasko63f3d842020-07-08 10:10:14 +020067#define YANG_READ_SUBSTMT_FOR(CTX, IN, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
68 LY_CHECK_RET(get_keyword(CTX, IN, &KW, &WORD, &WORD_LEN)); \
Radek Krejcid6b76452019-09-03 17:03:03 +020069 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010070 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010071 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020072 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020073 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020074 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020075 return LY_EVALID; \
76 } \
Michal Vasko63f3d842020-07-08 10:10:14 +020077 for (ERR = get_keyword(CTX, IN, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020078 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Michal Vasko63f3d842020-07-08 10:10:14 +020079 ERR = get_keyword(CTX, IN, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020080
Michal Vasko63f3d842020-07-08 10:10:14 +020081LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020082 struct lysp_node **siblings);
Michal Vasko63f3d842020-07-08 10:10:14 +020083LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
84LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
85LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
86LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
87LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020088
Michal Vaskoea5abea2018-09-18 13:10:54 +020089/**
90 * @brief Add another character to dynamic buffer, a low-level function.
91 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020092 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020093 *
Radek Krejci404251e2018-10-09 12:06:44 +020094 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +020095 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +020096 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020097 * @param[in,out] buf Buffer to use, can be moved by realloc().
98 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +020099 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200100 *
101 * @return LY_ERR values.
102 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200103LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200104buf_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 +0200105{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200106 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200107 *buf_len += 16;
108 *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;
119}
120
Michal Vaskoea5abea2018-09-18 13:10:54 +0200121/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200122 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
123 *
124 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200125 * @param[in,out] in Input structure.
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
Michal Vasko63f3d842020-07-08 10:10:14 +0200141buf_store_char(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200142 char **word_b, size_t *buf_len, int need_buf, int *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200144 unsigned int c;
145 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) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200151 LY_CHECK_ERR_RET(ly_getutf8(&in->current, &c, &len),
152 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, in->current[-len]), LY_EVALID);
153 in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200154 if (c == '\n') {
155 ctx->indent = 0;
156 } else {
157 /* note - even the multibyte character is count as 1 */
158 ++ctx->indent;
159 }
160
Radek Krejci44ceedc2018-10-02 15:54:31 +0200161 /* check character validity */
162 switch (arg) {
163 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100164 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200165 break;
166 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100167 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200168 break;
169 case Y_STR_ARG:
170 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100171 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200172 break;
173 }
174
Michal Vasko7fbc8162018-09-17 10:35:16 +0200175 if (word_b && *word_b) {
176 /* add another character into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200177 if (buf_add_char(ctx->ctx, in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200178 return LY_EMEM;
179 }
180
181 /* in case of realloc */
182 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200183 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200184 /* first time we need a buffer, copy everything read up to now */
185 if (*word_len) {
186 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200187 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200188 *buf_len = *word_len;
189 memcpy(*word_b, *word_p, *word_len);
190 }
191
192 /* add this new character into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200193 if (buf_add_char(ctx->ctx, in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200194 return LY_EMEM;
195 }
196
197 /* in case of realloc */
198 *word_p = *word_b;
199 } else {
200 /* just remember the first character pointer */
201 if (!*word_p) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200202 *word_p = (char *)in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200203 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200204 /* ... and update the word's length */
205 (*word_len) += len;
Michal Vasko63f3d842020-07-08 10:10:14 +0200206 ly_in_skip(in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200207 }
208
209 return LY_SUCCESS;
210}
211
Michal Vaskoea5abea2018-09-18 13:10:54 +0200212/**
213 * @brief Skip YANG comment in data.
214 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200215 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200216 * @param[in,out] in Input structure.
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
Michal Vasko63f3d842020-07-08 10:10:14 +0200223skip_comment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200224{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200225 /* internal statuses: 0 - comment ended,
226 * 1 - in line comment,
227 * 2 - in block comment,
228 * 3 - in block comment with last read character '*'
229 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200230 while (in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200231 switch (comment) {
232 case 1:
Michal Vasko63f3d842020-07-08 10:10:14 +0200233 if (in->current[0] == '\n') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200234 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200235 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200236 }
237 break;
238 case 2:
Michal Vasko63f3d842020-07-08 10:10:14 +0200239 if (in->current[0] == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200240 comment = 3;
Michal Vasko63f3d842020-07-08 10:10:14 +0200241 } else if (in->current[0] == '\n') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200242 ++ctx->line;
243 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200244 break;
245 case 3:
Michal Vasko63f3d842020-07-08 10:10:14 +0200246 if (in->current[0] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200247 comment = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200248 } else if (in->current[0] != '*') {
249 if (in->current[0] == '\n') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200250 ++ctx->line;
251 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200252 comment = 2;
253 }
254 break;
255 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200256 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200257 }
258
Michal Vasko63f3d842020-07-08 10:10:14 +0200259 if (in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200260 ctx->indent = 0;
261 } else {
262 ++ctx->indent;
263 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200264 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200265 }
266
Michal Vasko63f3d842020-07-08 10:10:14 +0200267 if (!in->current[0] && (comment > 1)) {
David Sedlákb3077192019-06-19 10:55:37 +0200268 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200269 return LY_EVALID;
270 }
271
272 return LY_SUCCESS;
273}
274
Michal Vaskoea5abea2018-09-18 13:10:54 +0200275/**
276 * @brief Read a quoted string from data.
277 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200278 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200279 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200280 * @param[in] arg Type of YANG keyword argument expected.
281 * @param[out] word_p Pointer to the read quoted string.
282 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
283 * set to NULL. Otherwise equal to \p word_p.
284 * @param[out] word_len Length of the read quoted string.
285 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
286 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
287 * indenation in the final quoted string.
288 *
289 * @return LY_ERR values.
290 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200291static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200292read_qstring(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200293 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200294{
295 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
296 * 4 - string finished, now skipping whitespaces looking for +,
297 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200298 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200299 const char *c;
David Sedlák40bb13b2019-07-10 14:34:18 +0200300 int prefix = 0;
Michal Vasko90edde42019-11-25 15:25:07 +0100301 unsigned int trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200302
Michal Vasko63f3d842020-07-08 10:10:14 +0200303 if (in->current[0] == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200304 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200305 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200306 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200307 assert(in->current[0] == '\'');
Michal Vasko7fbc8162018-09-17 10:35:16 +0200308 string = 1;
309 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200310 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200311
Michal Vasko63f3d842020-07-08 10:10:14 +0200312 while (in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200313 switch (string) {
314 case 1:
Michal Vasko63f3d842020-07-08 10:10:14 +0200315 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200316 case '\'':
317 /* string may be finished, but check for + */
318 string = 4;
Michal Vasko63f3d842020-07-08 10:10:14 +0200319 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200320 break;
321 default:
322 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200323 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200324 break;
325 }
326 break;
327 case 2:
Michal Vasko63f3d842020-07-08 10:10:14 +0200328 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200329 case '\"':
330 /* string may be finished, but check for + */
331 string = 4;
Michal Vasko63f3d842020-07-08 10:10:14 +0200332 MOVE_INPUT(ctx, in, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200333 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200334 break;
335 case '\\':
336 /* special character following */
337 string = 3;
Radek Krejciff13cd12019-10-25 15:34:24 +0200338
339 /* the backslash sequence is substituted, so we will need a buffer to store the result */
340 need_buf = 1;
341
342 /* move forward to the escaped character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200343 ++in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200344
345 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
346 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
347 trailing_ws = 0;
348
349 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
350 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200351 break;
352 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200353 if (current_indent < block_indent) {
354 ++current_indent;
Michal Vasko63f3d842020-07-08 10:10:14 +0200355 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200356 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100357 /* check and store whitespace character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200358 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100359 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200360 }
361 break;
362 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200363 if (current_indent < block_indent) {
364 assert(need_buf);
365 current_indent += 8;
366 ctx->indent += 8;
Michal Vaskod989ba02020-08-24 10:59:24 +0200367 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200368 /* store leftover spaces from the tab */
Michal Vasko63f3d842020-07-08 10:10:14 +0200369 c = in->current;
370 in->current = " ";
371 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
372 in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100373 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200374 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200375 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200376 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100377 /* check and store whitespace character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200378 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100379 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200380 /* additional characters for indentation - only 1 was count in buf_store_char */
381 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200382 }
383 break;
384 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200385 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200386 /* we will be removing the indents so we need our own buffer */
387 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200388
389 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200390 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200391
Radek Krejciff13cd12019-10-25 15:34:24 +0200392 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200393 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200394 }
395
396 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200397 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200398
399 /* maintain line number */
400 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200401
402 /* reset context indentation counter for possible string after this one */
403 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200404 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200405 break;
406 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200407 /* first non-whitespace character, stop eating indentation */
408 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200409
410 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200411 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200412 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200413 break;
414 }
415 break;
416 case 3:
417 /* string encoded characters */
Michal Vasko63f3d842020-07-08 10:10:14 +0200418 c = in->current;
419 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200420 case 'n':
Michal Vasko63f3d842020-07-08 10:10:14 +0200421 in->current = "\n";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200422 break;
423 case 't':
Michal Vasko63f3d842020-07-08 10:10:14 +0200424 in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200425 break;
426 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200427 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200428 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 break;
430 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200431 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
432 in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433 return LY_EVALID;
434 }
435
436 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200437 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438
439 string = 2;
Michal Vasko63f3d842020-07-08 10:10:14 +0200440 in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 break;
442 case 4:
Michal Vasko63f3d842020-07-08 10:10:14 +0200443 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200444 case '+':
445 /* string continues */
446 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200447 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200448 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200449 case '\n':
450 ++ctx->line;
Radek Krejci0f969882020-08-21 16:56:47 +0200451 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200452 case ' ':
453 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 /* just skip */
455 break;
456 default:
457 /* string is finished */
458 goto string_end;
459 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200460 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200461 break;
462 case 5:
Michal Vasko63f3d842020-07-08 10:10:14 +0200463 switch (in->current[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200464 case '\n':
465 ++ctx->line;
Radek Krejci0f969882020-08-21 16:56:47 +0200466 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 case ' ':
468 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200469 /* skip */
470 break;
471 case '\'':
472 string = 1;
473 break;
474 case '\"':
475 string = 2;
476 break;
477 default:
478 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200479 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 return LY_EVALID;
481 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200482 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200483 break;
484 default:
485 return LY_EINT;
486 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 }
488
489string_end:
Radek Krejci4e199f52019-05-28 09:09:28 +0200490 if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) {
491 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200492 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200493 return LY_EVALID;
494 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200495 return LY_SUCCESS;
496}
497
Michal Vaskoea5abea2018-09-18 13:10:54 +0200498/**
499 * @brief Get another YANG string from the raw data.
500 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200501 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200502 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200503 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200504 * @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 +0200505 * @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 +0200506 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
507 * set to NULL. Otherwise equal to \p word_p.
508 * @param[out] word_len Length of the read string.
509 *
510 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200511 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200512LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200513get_argument(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200514 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200515{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200516 size_t buf_len = 0;
David Sedlák40bb13b2019-07-10 14:34:18 +0200517 int prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200518 /* word buffer - dynamically allocated */
519 *word_b = NULL;
520
521 /* word pointer - just a pointer to data */
522 *word_p = NULL;
523
524 *word_len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200525 while (in->current[0]) {
526 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200527 case '\'':
528 case '\"':
529 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200530 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Michal Vasko63f3d842020-07-08 10:10:14 +0200531 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200532 "unquoted string character, optsep, semicolon or opening brace");
533 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200534 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200535 if (flags) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200536 (*flags) |= in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200537 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200538 LY_CHECK_RET(read_qstring(ctx, in, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200539 goto str_end;
540 case '/':
Michal Vasko63f3d842020-07-08 10:10:14 +0200541 if (in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200542 /* one-line comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200543 MOVE_INPUT(ctx, in, 2);
544 LY_CHECK_RET(skip_comment(ctx, in, 1));
545 } else if (in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200546 /* block comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200547 MOVE_INPUT(ctx, in, 2);
548 LY_CHECK_RET(skip_comment(ctx, in, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200549 } else {
550 /* not a comment after all */
Michal Vasko63f3d842020-07-08 10:10:14 +0200551 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200552 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200553 break;
554 case ' ':
555 if (*word_len) {
556 /* word is finished */
557 goto str_end;
558 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200559 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560 break;
561 case '\t':
562 if (*word_len) {
563 /* word is finished */
564 goto str_end;
565 }
566 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200567 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200568
Michal Vasko63f3d842020-07-08 10:10:14 +0200569 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200570 break;
571 case '\n':
572 if (*word_len) {
573 /* word is finished */
574 goto str_end;
575 }
576 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200577 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200578
579 /* track line numbers */
580 ++ctx->line;
581
Michal Vasko63f3d842020-07-08 10:10:14 +0200582 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200583 break;
584 case ';':
585 case '{':
586 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
587 /* word is finished */
588 goto str_end;
589 }
590
Michal Vasko63f3d842020-07-08 10:10:14 +0200591 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200592 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200593 case '}':
594 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200595 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200596 "unquoted string character, optsep, semicolon or opening brace");
597 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200598 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200599 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600 break;
601 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200602 }
603
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200604 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200605 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200606 return LY_EVALID;
607
Michal Vasko7fbc8162018-09-17 10:35:16 +0200608str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200609 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200610 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200611 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
612 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
613 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200614 *word_p = *word_b;
615 }
616
617 return LY_SUCCESS;
618}
619
Michal Vaskoea5abea2018-09-18 13:10:54 +0200620/**
621 * @brief Get another YANG keyword from the raw data.
622 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200623 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200624 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200625 * @param[out] kw YANG keyword read.
626 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
627 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
628 *
629 * @return LY_ERR values.
630 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200631LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200632get_keyword(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200633{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200634 int prefix;
635 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200636 unsigned int c;
637 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200638
639 if (word_p) {
640 *word_p = NULL;
641 *word_len = 0;
642 }
643
644 /* first skip "optsep", comments */
Michal Vasko63f3d842020-07-08 10:10:14 +0200645 while (in->current[0]) {
646 switch (in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200647 case '/':
Michal Vasko63f3d842020-07-08 10:10:14 +0200648 if (in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200649 /* one-line comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200650 MOVE_INPUT(ctx, in, 2);
651 LY_CHECK_RET(skip_comment(ctx, in, 1));
652 } else if (in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200653 /* block comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200654 MOVE_INPUT(ctx, in, 2);
655 LY_CHECK_RET(skip_comment(ctx, in, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200656 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200657 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200658 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200659 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200660 }
Radek Krejci13028282019-06-11 14:56:48 +0200661 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200662 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200663 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200664 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200665 ctx->indent = 0;
666 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200667 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200668 /* skip whitespaces (optsep) */
669 ++ctx->indent;
670 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200671 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200673 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200674 break;
675 default:
676 /* either a keyword start or an invalid character */
677 goto keyword_start;
678 }
679
Michal Vasko63f3d842020-07-08 10:10:14 +0200680 ly_in_skip(in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200681 }
682
683keyword_start:
Michal Vasko63f3d842020-07-08 10:10:14 +0200684 word_start = in->current;
685 *kw = lysp_match_kw(ctx, in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686
Radek Krejcid6b76452019-09-03 17:03:03 +0200687 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 +0200688 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689 }
690
Radek Krejcid6b76452019-09-03 17:03:03 +0200691 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200692 /* make sure we have the whole keyword */
Michal Vasko63f3d842020-07-08 10:10:14 +0200693 switch (in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200694 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200696 case ' ':
697 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200698 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200699 case ':':
700 /* keyword is not actually a keyword, but prefix of an extension.
701 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
702 * and we will be checking the keyword (extension instance) itself */
703 prefix = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200704 MOVE_INPUT(ctx, in, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200705 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200706 case '{':
707 /* allowed only for input and output statements which can be without arguments */
Radek Krejcid6b76452019-09-03 17:03:03 +0200708 if (*kw == LY_STMT_INPUT || *kw == LY_STMT_OUTPUT) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200709 break;
710 }
Radek Krejci0f969882020-08-21 16:56:47 +0200711 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200712 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200713 MOVE_INPUT(ctx, in, 1);
714 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(in->current - word_start), word_start,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200715 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200716 return LY_EVALID;
717 }
718 } else {
719 /* still can be an extension */
720 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200721extension:
Michal Vasko63f3d842020-07-08 10:10:14 +0200722 while (in->current[0] && (in->current[0] != ' ') && (in->current[0] != '\t') && (in->current[0] != '\n')
723 && (in->current[0] != '{') && (in->current[0] != ';')) {
724 LY_CHECK_ERR_RET(ly_getutf8(&in->current, &c, &len),
725 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200726 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200727 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200728 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
729 in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200731 if (!in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200732 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200733 return LY_EVALID;
734 }
735
736 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200737 if (prefix != 2) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200738 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200739 return LY_EVALID;
740 }
741
Radek Krejcid6b76452019-09-03 17:03:03 +0200742 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200743 }
Radek Krejci626df482018-10-11 15:06:31 +0200744success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200745 if (word_p) {
746 *word_p = (char *)word_start;
Michal Vasko63f3d842020-07-08 10:10:14 +0200747 *word_len = in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 }
749
750 return LY_SUCCESS;
751}
752
Michal Vaskoea5abea2018-09-18 13:10:54 +0200753/**
754 * @brief Parse extension instance substatements.
755 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200756 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200757 * @param[in,out] in Input structure.
Radek Krejci335332a2019-09-05 13:03:35 +0200758 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200759 * @param[in] word Extension instance substatement name (keyword).
760 * @param[in] word_len Extension instance substatement name length.
761 * @param[in,out] child Children of this extension instance to add to.
762 *
763 * @return LY_ERR values.
764 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200765static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200766parse_ext_substmt(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200767 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200768{
769 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100770 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200771 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200772 struct lysp_stmt *stmt, *par_child;
773
774 stmt = calloc(1, sizeof *stmt);
775 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
776
Radek Krejcibb9b1982019-04-08 14:24:59 +0200777 /* insert into parent statements */
778 if (!*child) {
779 *child = stmt;
780 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200781 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200782 par_child->next = stmt;
783 }
784
Radek Krejci44ceedc2018-10-02 15:54:31 +0200785 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci335332a2019-09-05 13:03:35 +0200786 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200787
788 /* get optional argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200789 LY_CHECK_RET(get_argument(ctx, in, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200790
Radek Krejci0ae092d2018-09-20 16:43:19 +0200791 if (word) {
792 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200793 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200794 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200795 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200796 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200797 }
798
Michal Vasko63f3d842020-07-08 10:10:14 +0200799 YANG_READ_SUBSTMT_FOR(ctx, in, child_kw, word, word_len, ret, ) {
800 LY_CHECK_RET(parse_ext_substmt(ctx, in, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200801 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200802 return ret;
803}
804
Michal Vaskoea5abea2018-09-18 13:10:54 +0200805/**
806 * @brief Parse extension instance.
807 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200808 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200809 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200810 * @param[in] ext_name Extension instance substatement name (keyword).
811 * @param[in] ext_name_len Extension instance substatement name length.
812 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
813 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
814 * @param[in,out] exts Extension instances to add to.
815 *
816 * @return LY_ERR values.
817 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200819parse_ext(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt,
Radek Krejci0f969882020-08-21 16:56:47 +0200820 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100822 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200823 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200824 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200825 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200826 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200827
Radek Krejci2c4e7172018-10-19 15:56:26 +0200828 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200829
830 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200831 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200832 e->insubstmt = insubstmt;
833 e->insubstmt_index = insubstmt_index;
834
835 /* get optional argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200836 LY_CHECK_RET(get_argument(ctx, in, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837
Radek Krejci0ae092d2018-09-20 16:43:19 +0200838 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200839 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200840 }
841
Michal Vaskod989ba02020-08-24 10:59:24 +0200842 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200843 LY_CHECK_RET(parse_ext_substmt(ctx, in, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200844 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845 return ret;
846}
847
Michal Vaskoea5abea2018-09-18 13:10:54 +0200848/**
849 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
850 * description, etc...
851 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200852 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200853 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200854 * @param[in] substmt Type of this substatement.
855 * @param[in] substmt_index Index of this substatement.
856 * @param[in,out] value Place to store the parsed value.
857 * @param[in] arg Type of the YANG keyword argument (of the value).
858 * @param[in,out] exts Extension instances to add to.
859 *
860 * @return LY_ERR values.
861 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200862static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200863parse_text_field(struct lys_yang_parser_ctx *ctx, struct ly_in *in, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200864 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200865{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100866 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200867 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200868 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200869 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870
871 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200872 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200873 return LY_EVALID;
874 }
875
876 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200877 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878
879 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200880 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881
Michal Vaskod989ba02020-08-24 10:59:24 +0200882 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200883 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200884 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200885 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 break;
887 default:
David Sedlákb3077192019-06-19 10:55:37 +0200888 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200889 return LY_EVALID;
890 }
891 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892 return ret;
893}
894
Michal Vaskoea5abea2018-09-18 13:10:54 +0200895/**
896 * @brief Parse the yang-version statement.
897 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200898 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200899 * @param[in,out] in Input structure.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100900 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200901 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200902 *
903 * @return LY_ERR values.
904 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200906parse_yangversion(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200907{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100908 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200909 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200910 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200911 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100913 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200914 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200915 return LY_EVALID;
916 }
917
918 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200919 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200920
921 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200923 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100924 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200925 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200926 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927 free(buf);
928 return LY_EVALID;
929 }
930 free(buf);
931
Michal Vaskod989ba02020-08-24 10:59:24 +0200932 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200934 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200935 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936 break;
937 default:
David Sedlákb3077192019-06-19 10:55:37 +0200938 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939 return LY_EVALID;
940 }
941 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200942 return ret;
943}
944
Michal Vaskoea5abea2018-09-18 13:10:54 +0200945/**
946 * @brief Parse the belongs-to statement.
947 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200948 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200949 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200950 * @param[in,out] belongsto Place to store the parsed value.
951 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
952 * @param[in,out] exts Extension instances to add to.
953 *
954 * @return LY_ERR values.
955 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200956static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200957parse_belongsto(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **belongsto, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200958{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100959 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200961 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200962 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200963
964 if (*belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +0200965 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200966 return LY_EVALID;
967 }
968
969 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200970 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200971
Radek Krejci44ceedc2018-10-02 15:54:31 +0200972 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200973
Michal Vasko63f3d842020-07-08 10:10:14 +0200974 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200975 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200976 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +0200977 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200978 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200979 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200980 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981 break;
982 default:
David Sedlákb3077192019-06-19 10:55:37 +0200983 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 return LY_EVALID;
985 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200986 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200987 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100988checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989 /* mandatory substatements */
990 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200991 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992 return LY_EVALID;
993 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200994 return ret;
995}
996
Michal Vaskoea5abea2018-09-18 13:10:54 +0200997/**
998 * @brief Parse the revision-date statement.
999 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001000 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001001 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001002 * @param[in,out] rev Array to store the parsed value in.
1003 * @param[in,out] exts Extension instances to add to.
1004 *
1005 * @return LY_ERR values.
1006 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001008parse_revisiondate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001010 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001012 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001013 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014
1015 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001016 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001017 return LY_EVALID;
1018 }
1019
1020 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001021 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001022
1023 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001024 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025 free(buf);
1026 return LY_EVALID;
1027 }
1028
1029 /* store value and spend buf if allocated */
1030 strncpy(rev, word, word_len);
1031 free(buf);
1032
Michal Vaskod989ba02020-08-24 10:59:24 +02001033 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001034 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001035 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001036 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037 break;
1038 default:
David Sedlákb3077192019-06-19 10:55:37 +02001039 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001040 return LY_EVALID;
1041 }
1042 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001043 return ret;
1044}
1045
Michal Vaskoea5abea2018-09-18 13:10:54 +02001046/**
1047 * @brief Parse the include statement.
1048 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001049 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001050 * @param[in] module_name Name of the module to check name collisions.
Michal Vasko63f3d842020-07-08 10:10:14 +02001051 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001052 * @param[in,out] includes Parsed includes to add to.
1053 *
1054 * @return LY_ERR values.
1055 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001057parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct ly_in *in, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058{
Radek Krejcid33273d2018-10-25 14:55:52 +02001059 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001060 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001061 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001062 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001063 struct lysp_include *inc;
1064
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001065 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001066
1067 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001068 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069
Radek Krejci086c7132018-10-26 15:29:04 +02001070 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1071
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001072 /* submodules share the namespace with the module names, so there must not be
1073 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001074 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001075 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001076 return LY_EVALID;
1077 }
1078
Michal Vaskod989ba02020-08-24 10:59:24 +02001079 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001080 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001081 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001082 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko63f3d842020-07-08 10:10:14 +02001083 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001084 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001085 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001086 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko63f3d842020-07-08 10:10:14 +02001087 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001089 case LY_STMT_REVISION_DATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001090 LY_CHECK_RET(parse_revisiondate(ctx, in, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001091 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001092 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001093 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094 break;
1095 default:
David Sedlákb3077192019-06-19 10:55:37 +02001096 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097 return LY_EVALID;
1098 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001099 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001100 return ret;
1101}
1102
Michal Vaskoea5abea2018-09-18 13:10:54 +02001103/**
1104 * @brief Parse the import statement.
1105 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001106 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001107 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vasko63f3d842020-07-08 10:10:14 +02001108 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001109 * @param[in,out] imports Parsed imports to add to.
1110 *
1111 * @return LY_ERR values.
1112 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001114parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct ly_in *in, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001115{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001116 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001117 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001118 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001119 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001120 struct lysp_import *imp;
1121
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001122 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001123
1124 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001125 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001126 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127
Michal Vasko63f3d842020-07-08 10:10:14 +02001128 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001129 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001130 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02001131 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskob36053d2020-03-26 15:49:30 +01001132 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 +02001133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001134 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001135 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko63f3d842020-07-08 10:10:14 +02001136 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001138 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001139 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko63f3d842020-07-08 10:10:14 +02001140 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001142 case LY_STMT_REVISION_DATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001143 LY_CHECK_RET(parse_revisiondate(ctx, in, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001145 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001146 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147 break;
1148 default:
David Sedlákb3077192019-06-19 10:55:37 +02001149 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150 return LY_EVALID;
1151 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001152 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001153 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001154checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001156 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157
1158 return ret;
1159}
1160
Michal Vaskoea5abea2018-09-18 13:10:54 +02001161/**
1162 * @brief Parse the revision statement.
1163 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001164 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001165 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001166 * @param[in,out] revs Parsed revisions to add to.
1167 *
1168 * @return LY_ERR values.
1169 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001170static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001171parse_revision(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001173 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001174 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001175 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001176 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001177 struct lysp_revision *rev;
1178
Radek Krejci2c4e7172018-10-19 15:56:26 +02001179 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180
1181 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001182 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183
1184 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001185 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001186 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001187 return LY_EVALID;
1188 }
1189
Radek Krejcib7db73a2018-10-24 14:18:40 +02001190 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191 free(buf);
1192
Michal Vaskod989ba02020-08-24 10:59:24 +02001193 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001195 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001196 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001198 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001199 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001201 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001202 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001203 break;
1204 default:
David Sedlákb3077192019-06-19 10:55:37 +02001205 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206 return LY_EVALID;
1207 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001209 return ret;
1210}
1211
Michal Vaskoea5abea2018-09-18 13:10:54 +02001212/**
1213 * @brief Parse a generic text field that can have more instances such as base.
1214 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001215 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001216 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001217 * @param[in] substmt Type of this substatement.
1218 * @param[in,out] texts Parsed values to add to.
1219 * @param[in] arg Type of the expected argument.
1220 * @param[in,out] exts Extension instances to add to.
1221 *
1222 * @return LY_ERR values.
1223 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001224static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001225parse_text_fields(struct lys_yang_parser_ctx *ctx, struct ly_in *in, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
Radek Krejci0f969882020-08-21 16:56:47 +02001226 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001228 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001229 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001230 const char **item;
1231 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001232 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233
1234 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001235 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236
1237 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001238 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239
Radek Krejci151a5b72018-10-19 14:21:44 +02001240 INSERT_WORD(ctx, buf, *item, word, word_len);
Michal Vaskod989ba02020-08-24 10:59:24 +02001241 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001243 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001244 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 break;
1246 default:
David Sedlákb3077192019-06-19 10:55:37 +02001247 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248 return LY_EVALID;
1249 }
1250 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251 return ret;
1252}
1253
Michal Vaskoea5abea2018-09-18 13:10:54 +02001254/**
1255 * @brief Parse the config statement.
1256 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001257 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001258 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001259 * @param[in,out] flags Flags to add to.
1260 * @param[in,out] exts Extension instances to add to.
1261 *
1262 * @return LY_ERR values.
1263 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001264static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001265parse_config(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001267 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001269 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001270 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001271
1272 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001273 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001274 return LY_EVALID;
1275 }
1276
1277 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001278 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001279
1280 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1281 *flags |= LYS_CONFIG_W;
1282 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1283 *flags |= LYS_CONFIG_R;
1284 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001285 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001286 free(buf);
1287 return LY_EVALID;
1288 }
1289 free(buf);
1290
Michal Vaskod989ba02020-08-24 10:59:24 +02001291 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001292 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001293 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001294 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001295 break;
1296 default:
David Sedlákb3077192019-06-19 10:55:37 +02001297 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001298 return LY_EVALID;
1299 }
1300 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001301 return ret;
1302}
1303
Michal Vaskoea5abea2018-09-18 13:10:54 +02001304/**
1305 * @brief Parse the mandatory statement.
1306 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001307 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001308 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001309 * @param[in,out] flags Flags to add to.
1310 * @param[in,out] exts Extension instances to add to.
1311 *
1312 * @return LY_ERR values.
1313 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001315parse_mandatory(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001317 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001319 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001320 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001321
1322 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001323 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324 return LY_EVALID;
1325 }
1326
1327 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001328 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329
1330 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1331 *flags |= LYS_MAND_TRUE;
1332 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1333 *flags |= LYS_MAND_FALSE;
1334 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001335 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336 free(buf);
1337 return LY_EVALID;
1338 }
1339 free(buf);
1340
Michal Vaskod989ba02020-08-24 10:59:24 +02001341 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001342 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001343 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001344 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345 break;
1346 default:
David Sedlákb3077192019-06-19 10:55:37 +02001347 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 return LY_EVALID;
1349 }
1350 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001351 return ret;
1352}
1353
Michal Vaskoea5abea2018-09-18 13:10:54 +02001354/**
1355 * @brief Parse a restriction such as range or length.
1356 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001357 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001358 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001359 * @param[in] restr_kw Type of this particular restriction.
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
Michal Vasko63f3d842020-07-08 10:10:14 +02001365parse_restr(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt restr_kw, struct lysp_restr *restr)
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 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001373 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374
Michal Vaskob36053d2020-03-26 15:49:30 +01001375 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001376 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Michal Vaskod989ba02020-08-24 10:59:24 +02001377 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001378 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001379 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001380 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001382 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001383 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001385 case LY_STMT_ERROR_APP_TAG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001386 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001388 case LY_STMT_ERROR_MESSAGE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001389 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001391 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001392 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001393 break;
1394 default:
David Sedlákb3077192019-06-19 10:55:37 +02001395 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001396 return LY_EVALID;
1397 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001398 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001399 return ret;
1400}
1401
Michal Vaskoea5abea2018-09-18 13:10:54 +02001402/**
1403 * @brief Parse a restriction that can have more instances such as must.
1404 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001405 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001406 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001407 * @param[in] restr_kw Type of this particular restriction.
1408 * @param[in,out] restrs Restrictions to add to.
1409 *
1410 * @return LY_ERR values.
1411 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001412static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001413parse_restrs(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001414{
1415 struct lysp_restr *restr;
1416
Radek Krejci2c4e7172018-10-19 15:56:26 +02001417 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02001418 return parse_restr(ctx, in, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001419}
1420
Michal Vaskoea5abea2018-09-18 13:10:54 +02001421/**
1422 * @brief Parse the status statement.
1423 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001424 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001425 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001426 * @param[in,out] flags Flags to add to.
1427 * @param[in,out] exts Extension instances to add to.
1428 *
1429 * @return LY_ERR values.
1430 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001432parse_status(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001433{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001434 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001436 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001437 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001438
1439 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001440 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001441 return LY_EVALID;
1442 }
1443
1444 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001445 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001446
1447 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1448 *flags |= LYS_STATUS_CURR;
1449 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1450 *flags |= LYS_STATUS_DEPRC;
1451 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1452 *flags |= LYS_STATUS_OBSLT;
1453 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001454 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001455 free(buf);
1456 return LY_EVALID;
1457 }
1458 free(buf);
1459
Michal Vaskod989ba02020-08-24 10:59:24 +02001460 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001462 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001463 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001464 break;
1465 default:
David Sedlákb3077192019-06-19 10:55:37 +02001466 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001467 return LY_EVALID;
1468 }
1469 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001470 return ret;
1471}
1472
Michal Vaskoea5abea2018-09-18 13:10:54 +02001473/**
1474 * @brief Parse the when statement.
1475 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001476 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001477 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001478 * @param[in,out] when_p When pointer to parse to.
1479 *
1480 * @return LY_ERR values.
1481 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001482LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001483parse_when(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001485 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001487 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001488 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489 struct lysp_when *when;
1490
1491 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001492 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493 return LY_EVALID;
1494 }
1495
1496 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001497 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498
1499 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001500 LY_CHECK_ERR_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len), free(when), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01001501 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci44ceedc2018-10-02 15:54:31 +02001502 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503
Radek Krejcif09e4e82019-06-14 15:08:11 +02001504 *when_p = when;
1505
Michal Vaskod989ba02020-08-24 10:59:24 +02001506 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001507 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001508 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001509 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001511 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001512 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001514 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001515 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516 break;
1517 default:
David Sedlákb3077192019-06-19 10:55:37 +02001518 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 return LY_EVALID;
1520 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001521 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001522 return ret;
1523}
1524
Michal Vaskoea5abea2018-09-18 13:10:54 +02001525/**
1526 * @brief Parse the anydata or anyxml statement.
1527 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001528 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001529 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001530 * @param[in] kw Type of this particular keyword.
1531 * @param[in,out] siblings Siblings to add to.
1532 *
1533 * @return LY_ERR values.
1534 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001535LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001536parse_any(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001538 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001539 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001540 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001541 struct lysp_node_anydata *any;
1542
David Sedlák60adc092019-08-06 15:57:02 +02001543 /* create new structure and insert into siblings */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02001544 LY_LIST_NEW_RET(ctx->ctx, siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001545
Radek Krejcid6b76452019-09-03 17:03:03 +02001546 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001547 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001548
Michal Vasko7fbc8162018-09-17 10:35:16 +02001549 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02001550 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001551 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552
1553 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02001554 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001556 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001557 LY_CHECK_RET(parse_config(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001559 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001560 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001561 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001562 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001563 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001565 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02001566 LY_CHECK_RET(parse_mandatory(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001568 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02001569 LY_CHECK_RET(parse_restrs(ctx, in, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001570 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001571 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001572 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001573 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001574 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02001575 LY_CHECK_RET(parse_status(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001577 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001578 LY_CHECK_RET(parse_when(ctx, in, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001579 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001580 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001581 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001582 break;
1583 default:
David Sedlákb3077192019-06-19 10:55:37 +02001584 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejci0f969882020-08-21 16:56:47 +02001585 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 return LY_EVALID;
1587 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001589 return ret;
1590}
1591
Michal Vaskoea5abea2018-09-18 13:10:54 +02001592/**
1593 * @brief Parse the value or position statement. Substatement of type enum statement.
1594 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001595 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001596 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001597 * @param[in] val_kw Type of this particular keyword.
1598 * @param[in,out] value Value to write to.
1599 * @param[in,out] flags Flags to write to.
1600 * @param[in,out] exts Extension instances to add to.
1601 *
1602 * @return LY_ERR values.
1603 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001604LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001605parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt val_kw, int64_t *value, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001606 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001608 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001609 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001610 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001611 long int num = 0;
1612 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001613 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001614
1615 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001616 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001617 return LY_EVALID;
1618 }
1619 *flags |= LYS_SET_VALUE;
1620
1621 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001622 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001623
Radek Krejcid6b76452019-09-03 17:03:03 +02001624 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 +02001625 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001626 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627 }
1628
1629 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001630 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001632 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001633 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001634 goto error;
1635 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 } else {
1637 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001638 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001639 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001640 goto error;
1641 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001642 }
1643 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001644 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001645 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001646 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647 }
1648 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001649 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001650 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001651 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001652 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653 *value = num;
1654 } else {
1655 *value = unum;
1656 }
1657 free(buf);
1658
Michal Vaskod989ba02020-08-24 10:59:24 +02001659 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001660 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001661 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001662 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, val_kw == LY_STMT_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663 break;
1664 default:
David Sedlákb3077192019-06-19 10:55:37 +02001665 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666 return LY_EVALID;
1667 }
1668 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001669 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001670
1671error:
1672 free(buf);
1673 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001674}
1675
Michal Vaskoea5abea2018-09-18 13:10:54 +02001676/**
1677 * @brief Parse the enum or bit statement. Substatement of type statement.
1678 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001679 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001680 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001681 * @param[in] enum_kw Type of this particular keyword.
1682 * @param[in,out] enums Enums or bits to add to.
1683 *
1684 * @return LY_ERR values.
1685 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001687parse_type_enum(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001688{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001689 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001691 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001692 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693 struct lysp_type_enum *enm;
1694
Radek Krejci2c4e7172018-10-19 15:56:26 +02001695 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696
1697 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001698 LY_CHECK_RET(get_argument(ctx, in, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejcid6b76452019-09-03 17:03:03 +02001699 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001700 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001701 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001702 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001703
Radek Krejci44ceedc2018-10-02 15:54:31 +02001704 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001705 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1706
Michal Vaskod989ba02020-08-24 10:59:24 +02001707 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001708 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001709 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001710 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001711 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001712 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001713 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Michal Vasko63f3d842020-07-08 10:10:14 +02001714 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001716 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001717 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001719 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02001720 LY_CHECK_RET(parse_status(ctx, in, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001721 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001722 case LY_STMT_VALUE:
1723 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
David Sedlák9fb515f2019-07-11 10:33:58 +02001724 ly_stmt2str(enum_kw)), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +02001725 LY_CHECK_RET(parse_type_enum_value_pos(ctx, in, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001726 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001727 case LY_STMT_POSITION:
1728 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
David Sedlák9fb515f2019-07-11 10:33:58 +02001729 ly_stmt2str(enum_kw)), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +02001730 LY_CHECK_RET(parse_type_enum_value_pos(ctx, in, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001731 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001732 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001733 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734 break;
1735 default:
David Sedlákb3077192019-06-19 10:55:37 +02001736 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 return LY_EVALID;
1738 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740 return ret;
1741}
1742
Michal Vaskoea5abea2018-09-18 13:10:54 +02001743/**
1744 * @brief Parse the fraction-digits statement. Substatement of type statement.
1745 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001746 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001747 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001748 * @param[in,out] fracdig Value to write to.
1749 * @param[in,out] exts Extension instances to add to.
1750 *
1751 * @return LY_ERR values.
1752 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001754parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001755{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001756 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001757 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001758 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001760 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001761
1762 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001763 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001764 return LY_EVALID;
1765 }
1766
1767 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001768 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001769
1770 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001771 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 free(buf);
1773 return LY_EVALID;
1774 }
1775
1776 errno = 0;
1777 num = strtoul(word, &ptr, 10);
1778 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001779 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001780 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001781 free(buf);
1782 return LY_EVALID;
1783 }
1784 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001785 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001786 free(buf);
1787 return LY_EVALID;
1788 }
1789 *fracdig = num;
1790 free(buf);
1791
Michal Vaskod989ba02020-08-24 10:59:24 +02001792 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001793 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001794 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001795 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001796 break;
1797 default:
David Sedlákb3077192019-06-19 10:55:37 +02001798 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799 return LY_EVALID;
1800 }
1801 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001802 return ret;
1803}
1804
Michal Vaskoea5abea2018-09-18 13:10:54 +02001805/**
1806 * @brief Parse the require-instance statement. Substatement of type statement.
1807 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001808 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001809 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001810 * @param[in,out] reqinst Value to write to.
1811 * @param[in,out] flags Flags to write to.
1812 * @param[in,out] exts Extension instances to add to.
1813 *
1814 * @return LY_ERR values.
1815 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001817parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001818 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001820 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001822 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001823 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824
1825 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001826 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001827 return LY_EVALID;
1828 }
1829 *flags |= LYS_SET_REQINST;
1830
1831 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001832 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001833
1834 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1835 *reqinst = 1;
1836 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001837 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001838 free(buf);
1839 return LY_EVALID;
1840 }
1841 free(buf);
1842
Michal Vaskod989ba02020-08-24 10:59:24 +02001843 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001845 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001846 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001847 break;
1848 default:
David Sedlákb3077192019-06-19 10:55:37 +02001849 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001850 return LY_EVALID;
1851 }
1852 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001853 return ret;
1854}
1855
Michal Vaskoea5abea2018-09-18 13:10:54 +02001856/**
1857 * @brief Parse the modifier statement. Substatement of type pattern statement.
1858 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001859 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001860 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001861 * @param[in,out] pat Value to write to.
1862 * @param[in,out] exts Extension instances to add to.
1863 *
1864 * @return LY_ERR values.
1865 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001866static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001867parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **pat,
Radek Krejci0f969882020-08-21 16:56:47 +02001868 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001869{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001870 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001872 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001873 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874
1875 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001876 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877 return LY_EVALID;
1878 }
1879
1880 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001881 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001882
1883 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001884 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 free(buf);
1886 return LY_EVALID;
1887 }
1888 free(buf);
1889
1890 /* replace the value in the dictionary */
1891 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001892 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001893 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001894 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001895
1896 assert(buf[0] == 0x06);
1897 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02001898 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899
Michal Vaskod989ba02020-08-24 10:59:24 +02001900 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001901 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001902 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001903 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904 break;
1905 default:
David Sedlákb3077192019-06-19 10:55:37 +02001906 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001907 return LY_EVALID;
1908 }
1909 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910 return ret;
1911}
1912
Michal Vaskoea5abea2018-09-18 13:10:54 +02001913/**
1914 * @brief Parse the pattern statement. Substatement of type statement.
1915 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001916 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001917 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001918 * @param[in,out] patterns Restrictions to add to.
1919 *
1920 * @return LY_ERR values.
1921 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001923parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001925 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001927 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001928 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929 struct lysp_restr *restr;
1930
Radek Krejci2c4e7172018-10-19 15:56:26 +02001931 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932
1933 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001934 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001935
1936 /* add special meaning first byte */
1937 if (buf) {
1938 buf = realloc(buf, word_len + 2);
1939 word = buf;
1940 } else {
1941 buf = malloc(word_len + 2);
1942 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001943 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001944 memmove(buf + 1, word, word_len);
1945 buf[0] = 0x06; /* pattern's default regular-match flag */
1946 buf[word_len + 1] = '\0'; /* terminating NULL byte */
1947 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001948
Michal Vaskod989ba02020-08-24 10:59:24 +02001949 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001951 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001952 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001954 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001955 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001957 case LY_STMT_ERROR_APP_TAG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001958 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001959 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001960 case LY_STMT_ERROR_MESSAGE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001961 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001963 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02001964 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko63f3d842020-07-08 10:10:14 +02001965 LY_CHECK_RET(parse_type_pattern_modifier(ctx, in, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001966 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001967 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001968 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969 break;
1970 default:
David Sedlákb3077192019-06-19 10:55:37 +02001971 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001972 return LY_EVALID;
1973 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001974 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975 return ret;
1976}
1977
Michal Vaskoea5abea2018-09-18 13:10:54 +02001978/**
1979 * @brief Parse the type statement.
1980 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001981 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001982 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001983 * @param[in,out] type Type to wrote to.
1984 *
1985 * @return LY_ERR values.
1986 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001988parse_type(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001990 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02001992 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001993 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001994 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995 struct lysp_type *nest_type;
1996
1997 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02001998 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001999 return LY_EVALID;
2000 }
2001
2002 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002003 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002004 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002005
Michal Vaskod989ba02020-08-24 10:59:24 +02002006 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002007 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002008 case LY_STMT_BASE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002009 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002010 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002012 case LY_STMT_BIT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002013 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002014 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002016 case LY_STMT_ENUM:
Michal Vasko63f3d842020-07-08 10:10:14 +02002017 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002018 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002020 case LY_STMT_FRACTION_DIGITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002021 LY_CHECK_RET(parse_type_fracdigits(ctx, in, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002022 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002024 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002026 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027 return LY_EVALID;
2028 }
2029 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002030 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002031
Michal Vasko63f3d842020-07-08 10:10:14 +02002032 LY_CHECK_RET(parse_restr(ctx, in, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002033 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002035 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002036 if (type->path) {
2037 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2038 return LY_EVALID;
2039 }
2040
Michal Vasko63f3d842020-07-08 10:10:14 +02002041 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts));
Michal Vasko6b26e742020-07-17 15:02:10 +02002042 ret = ly_path_parse(ctx->ctx, NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
Michal Vasko004d3152020-06-11 19:59:22 +02002043 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
2044 lydict_remove(ctx->ctx, str_path);
2045 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002046 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002048 case LY_STMT_PATTERN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002049 LY_CHECK_RET(parse_type_pattern(ctx, in, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002050 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002051 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002052 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002054 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055 return LY_EVALID;
2056 }
2057 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002058 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002059
Michal Vasko63f3d842020-07-08 10:10:14 +02002060 LY_CHECK_RET(parse_restr(ctx, in, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002061 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002063 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002064 LY_CHECK_RET(parse_type_reqinstance(ctx, in, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002065 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002067 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002068 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02002069 LY_CHECK_RET(parse_type(ctx, in, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002070 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002072 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002073 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 break;
2075 default:
David Sedlákb3077192019-06-19 10:55:37 +02002076 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002077 return LY_EVALID;
2078 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002079 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080 return ret;
2081}
2082
Michal Vaskoea5abea2018-09-18 13:10:54 +02002083/**
2084 * @brief Parse the leaf statement.
2085 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002086 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002087 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002088 * @param[in,out] siblings Siblings to add to.
2089 *
2090 * @return LY_ERR values.
2091 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002092LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002093parse_leaf(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002095 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002097 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002098 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002099 struct lysp_node_leaf *leaf;
2100
David Sedlák60adc092019-08-06 15:57:02 +02002101 /* create new leaf structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002102 LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002104 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002107 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002108 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109
2110 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002111 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002113 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002114 LY_CHECK_RET(parse_config(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002116 case LY_STMT_DEFAULT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002117 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002118 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002119 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002120 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002122 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002123 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002125 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002126 LY_CHECK_RET(parse_mandatory(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002128 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002129 LY_CHECK_RET(parse_restrs(ctx, in, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002131 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002132 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002134 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002135 LY_CHECK_RET(parse_status(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002137 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002138 LY_CHECK_RET(parse_type(ctx, in, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002140 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002141 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002143 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002144 LY_CHECK_RET(parse_when(ctx, in, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002146 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002147 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148 break;
2149 default:
David Sedlákb3077192019-06-19 10:55:37 +02002150 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151 return LY_EVALID;
2152 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002154 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002155checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 /* mandatory substatements */
2157 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002158 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 return LY_EVALID;
2160 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002161 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
David Sedlákb3077192019-06-19 10:55:37 +02002162 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002163 return LY_EVALID;
2164 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165
2166 return ret;
2167}
2168
Michal Vaskoea5abea2018-09-18 13:10:54 +02002169/**
2170 * @brief Parse the max-elements statement.
2171 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002172 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002173 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002174 * @param[in,out] max Value to write to.
2175 * @param[in,out] flags Flags to write to.
2176 * @param[in,out] exts Extension instances to add to.
2177 *
2178 * @return LY_ERR values.
2179 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002180LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002181parse_maxelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *max, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002182 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002184 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002186 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002188 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189
2190 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002191 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 return LY_EVALID;
2193 }
2194 *flags |= LYS_SET_MAX;
2195
2196 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002197 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002198
2199 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002200 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002201 free(buf);
2202 return LY_EVALID;
2203 }
2204
Radek Krejci7f9b6512019-09-18 13:11:09 +02002205 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002206 errno = 0;
2207 num = strtoul(word, &ptr, 10);
2208 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002209 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002210 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211 free(buf);
2212 return LY_EVALID;
2213 }
2214 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002215 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002216 free(buf);
2217 return LY_EVALID;
2218 }
2219
2220 *max = num;
2221 }
2222 free(buf);
2223
Michal Vaskod989ba02020-08-24 10:59:24 +02002224 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002225 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002226 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002227 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228 break;
2229 default:
David Sedlákb3077192019-06-19 10:55:37 +02002230 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002231 return LY_EVALID;
2232 }
2233 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234 return ret;
2235}
2236
Michal Vaskoea5abea2018-09-18 13:10:54 +02002237/**
2238 * @brief Parse the min-elements statement.
2239 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002240 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002241 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002242 * @param[in,out] min Value to write to.
2243 * @param[in,out] flags Flags to write to.
2244 * @param[in,out] exts Extension instances to add to.
2245 *
2246 * @return LY_ERR values.
2247 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002248LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002249parse_minelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *min, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002250 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002252 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002254 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002256 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002257
2258 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002259 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 return LY_EVALID;
2261 }
2262 *flags |= LYS_SET_MIN;
2263
2264 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002265 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002266
2267 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002268 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269 free(buf);
2270 return LY_EVALID;
2271 }
2272
2273 errno = 0;
2274 num = strtoul(word, &ptr, 10);
2275 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002276 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002277 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 free(buf);
2279 return LY_EVALID;
2280 }
2281 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002282 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 free(buf);
2284 return LY_EVALID;
2285 }
2286 *min = num;
2287 free(buf);
2288
Michal Vaskod989ba02020-08-24 10:59:24 +02002289 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002291 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002292 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 break;
2294 default:
David Sedlákb3077192019-06-19 10:55:37 +02002295 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002296 return LY_EVALID;
2297 }
2298 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299 return ret;
2300}
2301
Michal Vaskoea5abea2018-09-18 13:10:54 +02002302/**
2303 * @brief Parse the ordered-by statement.
2304 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002305 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002306 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002307 * @param[in,out] flags Flags to write to.
2308 * @param[in,out] exts Extension instances to add to.
2309 *
2310 * @return LY_ERR values.
2311 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002313parse_orderedby(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002315 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002317 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002318 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319
2320 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002321 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002322 return LY_EVALID;
2323 }
2324
2325 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002326 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002327
2328 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2329 *flags |= LYS_ORDBY_SYSTEM;
2330 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2331 *flags |= LYS_ORDBY_USER;
2332 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002333 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 free(buf);
2335 return LY_EVALID;
2336 }
2337 free(buf);
2338
Michal Vaskod989ba02020-08-24 10:59:24 +02002339 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002341 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002342 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343 break;
2344 default:
David Sedlákb3077192019-06-19 10:55:37 +02002345 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 return LY_EVALID;
2347 }
2348 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349 return ret;
2350}
2351
Michal Vaskoea5abea2018-09-18 13:10:54 +02002352/**
2353 * @brief Parse the leaf-list statement.
2354 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002355 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002356 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002357 * @param[in,out] siblings Siblings to add to.
2358 *
2359 * @return LY_ERR values.
2360 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002361LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002362parse_leaflist(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002364 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002366 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002367 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368 struct lysp_node_leaflist *llist;
2369
David Sedlák60adc092019-08-06 15:57:02 +02002370 /* create new leaf-list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002371 LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002373 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002374
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002376 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002377 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378
2379 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002380 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002381 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002382 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002383 LY_CHECK_RET(parse_config(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002385 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002386 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Michal Vasko63f3d842020-07-08 10:10:14 +02002387 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002389 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002390 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002392 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002393 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002395 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002396 LY_CHECK_RET(parse_maxelements(ctx, in, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002398 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002399 LY_CHECK_RET(parse_minelements(ctx, in, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002401 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002402 LY_CHECK_RET(parse_restrs(ctx, in, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002404 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002405 LY_CHECK_RET(parse_orderedby(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002407 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002408 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002410 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002411 LY_CHECK_RET(parse_status(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002413 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002414 LY_CHECK_RET(parse_type(ctx, in, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002416 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002417 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002419 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002420 LY_CHECK_RET(parse_when(ctx, in, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002422 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002423 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002424 break;
2425 default:
David Sedlákb3077192019-06-19 10:55:37 +02002426 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427 return LY_EVALID;
2428 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002429 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002430 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002431checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432 /* mandatory substatements */
2433 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002434 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435 return LY_EVALID;
2436 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002437 if ((llist->min) && (llist->dflts)) {
David Sedlákb3077192019-06-19 10:55:37 +02002438 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
Radek Krejci0e5d8382018-11-28 16:37:53 +01002439 return LY_EVALID;
2440 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002441 if (llist->max && llist->min > llist->max) {
David Sedlákb3077192019-06-19 10:55:37 +02002442 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejcidf6cad12018-11-28 17:10:55 +01002443 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2444 llist->min, llist->max);
2445 return LY_EVALID;
2446 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002447
2448 return ret;
2449}
2450
Michal Vaskoea5abea2018-09-18 13:10:54 +02002451/**
2452 * @brief Parse the refine statement.
2453 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002454 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002455 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002456 * @param[in,out] refines Refines to add to.
2457 *
2458 * @return LY_ERR values.
2459 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002461parse_refine(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002462{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002463 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002464 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002465 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002466 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002467 struct lysp_refine *rf;
2468
Radek Krejci2c4e7172018-10-19 15:56:26 +02002469 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002470
2471 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002472 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002473 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002474 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475
Michal Vaskod989ba02020-08-24 10:59:24 +02002476 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002478 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002479 LY_CHECK_RET(parse_config(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002481 case LY_STMT_DEFAULT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002482 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002484 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002485 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002486 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002487 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002488 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Michal Vasko63f3d842020-07-08 10:10:14 +02002489 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002491 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002492 LY_CHECK_RET(parse_maxelements(ctx, in, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002494 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002495 LY_CHECK_RET(parse_minelements(ctx, in, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002496 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002497 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002498 LY_CHECK_RET(parse_restrs(ctx, in, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002500 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002501 LY_CHECK_RET(parse_mandatory(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002503 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002504 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002506 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002507 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002509 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002510 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002511 break;
2512 default:
David Sedlákb3077192019-06-19 10:55:37 +02002513 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002514 return LY_EVALID;
2515 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 return ret;
2518}
2519
Michal Vaskoea5abea2018-09-18 13:10:54 +02002520/**
2521 * @brief Parse the typedef statement.
2522 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002523 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002524 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002525 * @param[in,out] typedefs Typedefs to add to.
2526 *
2527 * @return LY_ERR values.
2528 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002529static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002530parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct ly_in *in, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002532 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002534 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002535 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002536 struct lysp_tpdf *tpdf;
2537
Radek Krejci2c4e7172018-10-19 15:56:26 +02002538 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539
2540 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002541 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002542 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543
2544 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002545 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002547 case LY_STMT_DEFAULT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002548 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002549 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002550 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002551 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002553 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002554 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002556 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002557 LY_CHECK_RET(parse_status(ctx, in, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002559 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002560 LY_CHECK_RET(parse_type(ctx, in, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002561 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002562 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002563 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002565 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002566 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567 break;
2568 default:
David Sedlákb3077192019-06-19 10:55:37 +02002569 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570 return LY_EVALID;
2571 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002573 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002574checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 /* mandatory substatements */
2576 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002577 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578 return LY_EVALID;
2579 }
2580
Radek Krejcibbe09a92018-11-08 09:36:54 +01002581 /* store data for collision check */
Michal Vasko1bf09392020-03-27 12:38:10 +01002582 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
David Sedláke8b74df2019-08-14 14:18:22 +02002583 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0) == -1, LY_EMEM);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002584 }
2585
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586 return ret;
2587}
2588
Michal Vaskoea5abea2018-09-18 13:10:54 +02002589/**
2590 * @brief Parse the input or output statement.
2591 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002592 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002593 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002594 * @param[in] kw Type of this particular keyword
2595 * @param[in,out] inout_p Input/output pointer to write to.
2596 *
2597 * @return LY_ERR values.
2598 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002599static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002600parse_inout(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt inout_kw, struct lysp_node *parent, struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002601{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002602 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002604 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002605 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002607 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002608 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 return LY_EVALID;
2610 }
2611
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002612 /* initiate structure */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002613 inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002614 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615
2616 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002617 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002619 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002620 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002621 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002622 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02002623 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002626 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02002629 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002631 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02002632 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002634 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002635 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002637 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002638 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002640 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02002641 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002643 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02002644 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, in, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002646 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002647 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Michal Vasko63f3d842020-07-08 10:10:14 +02002648 LY_CHECK_RET(parse_restrs(ctx, in, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002650 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02002651 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002653 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002654 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655 break;
2656 default:
David Sedlákb3077192019-06-19 10:55:37 +02002657 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658 return LY_EVALID;
2659 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002661 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002662
Radek Krejci7fc68292019-06-12 13:51:09 +02002663checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002664 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002665 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 +02002666
Michal Vaskob83af8a2020-01-06 09:49:22 +01002667 if (!inout_p->data) {
2668 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2669 return LY_EVALID;
2670 }
2671
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672 return ret;
2673}
2674
Michal Vaskoea5abea2018-09-18 13:10:54 +02002675/**
2676 * @brief Parse the action statement.
2677 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002678 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002679 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002680 * @param[in,out] actions Actions to add to.
2681 *
2682 * @return LY_ERR values.
2683 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002684LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002685parse_action(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002686{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002687 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002689 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002690 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 struct lysp_action *act;
2692
Radek Krejci2c4e7172018-10-19 15:56:26 +02002693 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694
2695 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002696 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002697 INSERT_WORD(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002698 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002699 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700
Michal Vasko63f3d842020-07-08 10:10:14 +02002701 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002703 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002704 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002705 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002706 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002707 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002708 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002709 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002710 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002711 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002712 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002713 LY_CHECK_RET(parse_status(ctx, in, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002714 break;
2715
Radek Krejcid6b76452019-09-03 17:03:03 +02002716 case LY_STMT_INPUT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002717 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002719 case LY_STMT_OUTPUT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002720 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 break;
2722
Radek Krejcid6b76452019-09-03 17:03:03 +02002723 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02002724 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, in, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002725 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002726 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02002727 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002729 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002730 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731 break;
2732 default:
David Sedlákb3077192019-06-19 10:55:37 +02002733 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734 return LY_EVALID;
2735 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002737 LY_CHECK_RET(ret);
2738checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002739 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002740 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002741
Michal Vasko7fbc8162018-09-17 10:35:16 +02002742 return ret;
2743}
2744
Michal Vaskoea5abea2018-09-18 13:10:54 +02002745/**
2746 * @brief Parse the notification statement.
2747 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002748 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002749 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002750 * @param[in,out] notifs Notifications to add to.
2751 *
2752 * @return LY_ERR values.
2753 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002754LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002755parse_notif(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002756{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002757 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002759 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002760 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 struct lysp_notif *notif;
2762
Radek Krejci2c4e7172018-10-19 15:56:26 +02002763 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764
2765 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002766 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002767 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002768 notif->nodetype = LYS_NOTIF;
2769 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770
Michal Vasko63f3d842020-07-08 10:10:14 +02002771 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002773 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002774 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002776 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002777 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002779 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002780 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002781 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002782 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002783 LY_CHECK_RET(parse_status(ctx, in, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784 break;
2785
Radek Krejcid6b76452019-09-03 17:03:03 +02002786 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002787 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002788 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002789 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02002790 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002791 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002792 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002793 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002795 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02002796 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002798 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02002799 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002801 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002802 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002803 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002804 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002805 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002807 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02002808 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002809 break;
2810
Radek Krejcid6b76452019-09-03 17:03:03 +02002811 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002812 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Michal Vasko63f3d842020-07-08 10:10:14 +02002813 LY_CHECK_RET(parse_restrs(ctx, in, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002815 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02002816 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, in, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002818 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02002819 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002821 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002822 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823 break;
2824 default:
David Sedlákb3077192019-06-19 10:55:37 +02002825 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 return LY_EVALID;
2827 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002829 LY_CHECK_RET(ret);
2830checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002831 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002832 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002833
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834 return ret;
2835}
2836
Michal Vaskoea5abea2018-09-18 13:10:54 +02002837/**
2838 * @brief Parse the grouping statement.
2839 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002840 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002841 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002842 * @param[in,out] groupings Groupings to add to.
2843 *
2844 * @return LY_ERR values.
2845 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002846LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002847parse_grouping(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002849 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002850 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002851 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002852 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853 struct lysp_grp *grp;
2854
Radek Krejci2c4e7172018-10-19 15:56:26 +02002855 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856
2857 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002858 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002859 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002860 grp->nodetype = LYS_GROUPING;
2861 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862
Michal Vasko63f3d842020-07-08 10:10:14 +02002863 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002865 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002866 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002868 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002869 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002871 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002872 LY_CHECK_RET(parse_status(ctx, in, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873 break;
2874
Radek Krejcid6b76452019-09-03 17:03:03 +02002875 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002876 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002877 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002878 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02002879 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002881 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002882 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002884 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02002885 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002886 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002887 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02002888 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002890 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002891 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002893 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002894 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002896 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02002897 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 break;
2899
Radek Krejcid6b76452019-09-03 17:03:03 +02002900 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02002901 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, in, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002902 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002903 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002904 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Michal Vasko63f3d842020-07-08 10:10:14 +02002905 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002906 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002907 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02002908 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002910 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002911 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Michal Vasko63f3d842020-07-08 10:10:14 +02002912 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002914 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002915 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
2917 default:
David Sedlákb3077192019-06-19 10:55:37 +02002918 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 return LY_EVALID;
2920 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002922 LY_CHECK_RET(ret);
2923checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002924 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002925 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 +02002926
Michal Vasko7fbc8162018-09-17 10:35:16 +02002927 return ret;
2928}
2929
Michal Vaskoea5abea2018-09-18 13:10:54 +02002930/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002931 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002932 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002933 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002934 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002935 * @param[in,out] augments Augments to add to.
2936 *
2937 * @return LY_ERR values.
2938 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002939LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002940parse_augment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002942 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002944 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002945 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 struct lysp_augment *aug;
2947
Radek Krejci2c4e7172018-10-19 15:56:26 +02002948 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949
2950 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002951 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002952 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002953 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002954 aug->nodetype = LYS_AUGMENT;
2955 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002956
Michal Vasko63f3d842020-07-08 10:10:14 +02002957 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002959 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002960 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002962 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002963 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002965 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002966 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002968 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002969 LY_CHECK_RET(parse_status(ctx, in, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002971 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002972 LY_CHECK_RET(parse_when(ctx, in, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973 break;
2974
Radek Krejcid6b76452019-09-03 17:03:03 +02002975 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002976 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02002977 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002978 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02002979 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002980 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002981 case LY_STMT_CASE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002982 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002984 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002985 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002986 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002987 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02002988 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002989 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002990 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02002991 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002993 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002994 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002996 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002997 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002999 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02003000 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 break;
3002
Radek Krejcid6b76452019-09-03 17:03:03 +02003003 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003004 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Michal Vasko63f3d842020-07-08 10:10:14 +02003005 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003007 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003008 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Michal Vasko63f3d842020-07-08 10:10:14 +02003009 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003011 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003012 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 break;
3014 default:
David Sedlákb3077192019-06-19 10:55:37 +02003015 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 return LY_EVALID;
3017 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003019 LY_CHECK_RET(ret);
3020checks:
3021 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003022 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 +02003023
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 return ret;
3025}
3026
Michal Vaskoea5abea2018-09-18 13:10:54 +02003027/**
3028 * @brief Parse the uses statement.
3029 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003030 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003031 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003032 * @param[in,out] siblings Siblings to add to.
3033 *
3034 * @return LY_ERR values.
3035 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003036LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003037parse_uses(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003038{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003039 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003041 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003042 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043 struct lysp_node_uses *uses;
3044
David Sedlák60adc092019-08-06 15:57:02 +02003045 /* create uses structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003046 LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003048 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003051 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003052 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053
3054 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003055 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003057 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003058 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003059 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003060 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003061 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003063 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003064 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003066 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003067 LY_CHECK_RET(parse_status(ctx, in, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003068 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003069 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003070 LY_CHECK_RET(parse_when(ctx, in, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071 break;
3072
Radek Krejcid6b76452019-09-03 17:03:03 +02003073 case LY_STMT_REFINE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003074 LY_CHECK_RET(parse_refine(ctx, in, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003076 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003077 LY_CHECK_RET(parse_augment(ctx, in, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003079 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003080 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081 break;
3082 default:
David Sedlákb3077192019-06-19 10:55:37 +02003083 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084 return LY_EVALID;
3085 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003087checks:
3088 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003089 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003090
Michal Vasko7fbc8162018-09-17 10:35:16 +02003091 return ret;
3092}
3093
Michal Vaskoea5abea2018-09-18 13:10:54 +02003094/**
3095 * @brief Parse the case statement.
3096 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003097 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003098 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003099 * @param[in,out] siblings Siblings to add to.
3100 *
3101 * @return LY_ERR values.
3102 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003103LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003104parse_case(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003106 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003108 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003109 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 struct lysp_node_case *cas;
3111
David Sedlák60adc092019-08-06 15:57:02 +02003112 /* create new case structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003113 LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003114 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003115 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003116
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003118 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003119 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003120
3121 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02003122 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003124 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003125 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003127 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003128 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003130 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003131 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003133 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003134 LY_CHECK_RET(parse_status(ctx, in, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003136 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003137 LY_CHECK_RET(parse_when(ctx, in, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003138 break;
3139
Radek Krejcid6b76452019-09-03 17:03:03 +02003140 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003141 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003142 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003143 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02003144 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003146 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003147 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003149 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02003150 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003152 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02003153 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003155 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003156 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003158 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003159 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003161 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02003162 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003164 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003165 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 break;
3167 default:
David Sedlákb3077192019-06-19 10:55:37 +02003168 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003169 return LY_EVALID;
3170 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 return ret;
3173}
3174
Michal Vaskoea5abea2018-09-18 13:10:54 +02003175/**
3176 * @brief Parse the choice statement.
3177 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003178 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003179 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003180 * @param[in,out] siblings Siblings to add to.
3181 *
3182 * @return LY_ERR values.
3183 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003184LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003185parse_choice(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003186{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003187 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003189 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003190 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003191 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192
David Sedlák60adc092019-08-06 15:57:02 +02003193 /* create new choice structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003194 LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003195 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003196 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003197
Michal Vasko7fbc8162018-09-17 10:35:16 +02003198 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003199 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003200 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201
3202 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003203 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003204 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003205 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003206 LY_CHECK_RET(parse_config(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003208 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003209 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003211 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003212 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003214 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003215 LY_CHECK_RET(parse_mandatory(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003217 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003218 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003220 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003221 LY_CHECK_RET(parse_status(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003223 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003224 LY_CHECK_RET(parse_when(ctx, in, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003226 case LY_STMT_DEFAULT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003227 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_PREF_IDENTIF_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 break;
3229
Radek Krejcid6b76452019-09-03 17:03:03 +02003230 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003231 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003232 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003233 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02003234 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003236 case LY_STMT_CASE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003237 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003239 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003240 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Michal Vasko63f3d842020-07-08 10:10:14 +02003241 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003243 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02003244 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003246 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02003247 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003249 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003250 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003252 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003253 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003255 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003256 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257 break;
3258 default:
David Sedlákb3077192019-06-19 10:55:37 +02003259 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 return LY_EVALID;
3261 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003263 LY_CHECK_RET(ret);
3264checks:
3265 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
David Sedlákb3077192019-06-19 10:55:37 +02003266 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
Radek Krejcia9026eb2018-12-12 16:04:47 +01003267 return LY_EVALID;
3268 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003269 return ret;
3270}
3271
Michal Vaskoea5abea2018-09-18 13:10:54 +02003272/**
3273 * @brief Parse the container statement.
3274 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003275 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003276 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003277 * @param[in,out] siblings Siblings to add to.
3278 *
3279 * @return LY_ERR values.
3280 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003281LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003282parse_container(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283{
3284 LY_ERR ret = 0;
3285 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003286 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003287 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288 struct lysp_node_container *cont;
3289
David Sedlák60adc092019-08-06 15:57:02 +02003290 /* create new container structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003291 LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003293 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003296 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003297 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003298
3299 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003300 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003302 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003303 LY_CHECK_RET(parse_config(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003305 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003306 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003308 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003309 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003311 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003312 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003314 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003315 LY_CHECK_RET(parse_status(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003317 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003318 LY_CHECK_RET(parse_when(ctx, in, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003320 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003321 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322 break;
3323
Radek Krejcid6b76452019-09-03 17:03:03 +02003324 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003325 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003326 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02003328 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003331 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003333 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02003334 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003336 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02003337 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003339 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003340 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003342 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003343 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003345 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02003346 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003347 break;
3348
Radek Krejcid6b76452019-09-03 17:03:03 +02003349 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02003350 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, in, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003352 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003353 LY_CHECK_RET(parse_restrs(ctx, in, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003354 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003355 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003356 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Michal Vasko63f3d842020-07-08 10:10:14 +02003357 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003359 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02003360 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003362 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003363 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Michal Vasko63f3d842020-07-08 10:10:14 +02003364 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003366 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003367 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368 break;
3369 default:
David Sedlákb3077192019-06-19 10:55:37 +02003370 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 return LY_EVALID;
3372 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003373 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003374checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003375 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003376 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 +02003377 return ret;
3378}
3379
Michal Vaskoea5abea2018-09-18 13:10:54 +02003380/**
3381 * @brief Parse the list statement.
3382 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003383 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003384 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003385 * @param[in,out] siblings Siblings to add to.
3386 *
3387 * @return LY_ERR values.
3388 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003389LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003390parse_list(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003391{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003392 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003394 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003395 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396 struct lysp_node_list *list;
3397
David Sedlák60adc092019-08-06 15:57:02 +02003398 /* create new list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003399 LY_LIST_NEW_RET(ctx->ctx, siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003400 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003401 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003404 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003405 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406
3407 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003408 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003410 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003411 LY_CHECK_RET(parse_config(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003414 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003416 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003417 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003419 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003420 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003422 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003423 LY_CHECK_RET(parse_status(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003425 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003426 LY_CHECK_RET(parse_when(ctx, in, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_KEY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003429 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003432 LY_CHECK_RET(parse_maxelements(ctx, in, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003434 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003435 LY_CHECK_RET(parse_minelements(ctx, in, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003437 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003438 LY_CHECK_RET(parse_orderedby(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003440 case LY_STMT_UNIQUE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003441 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442 break;
3443
Radek Krejcid6b76452019-09-03 17:03:03 +02003444 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003445 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003446 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02003448 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003451 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02003454 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003456 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02003457 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003459 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003460 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003462 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003463 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003465 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02003466 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 break;
3468
Radek Krejcid6b76452019-09-03 17:03:03 +02003469 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02003470 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, in, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003472 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003473 LY_CHECK_RET(parse_restrs(ctx, in, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003475 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003476 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Michal Vasko63f3d842020-07-08 10:10:14 +02003477 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003479 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02003480 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003481 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003482 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003483 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Michal Vasko63f3d842020-07-08 10:10:14 +02003484 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003486 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003487 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003488 break;
3489 default:
David Sedlákb3077192019-06-19 10:55:37 +02003490 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491 return LY_EVALID;
3492 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003494 LY_CHECK_RET(ret);
3495checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003496 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003497 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 +02003498
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003499 if (list->max && list->min > list->max) {
David Sedlákb3077192019-06-19 10:55:37 +02003500 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003501 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3502 list->min, list->max);
3503 return LY_EVALID;
3504 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505
3506 return ret;
3507}
3508
Michal Vaskoea5abea2018-09-18 13:10:54 +02003509/**
3510 * @brief Parse the yin-element statement.
3511 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003512 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003513 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003514 * @param[in,out] flags Flags to write to.
3515 * @param[in,out] exts Extension instances to add to.
3516 *
3517 * @return LY_ERR values.
3518 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003519static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003520parse_yinelement(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003522 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003524 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003525 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526
3527 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003528 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529 return LY_EVALID;
3530 }
3531
3532 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003533 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534
3535 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3536 *flags |= LYS_YINELEM_TRUE;
3537 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3538 *flags |= LYS_YINELEM_FALSE;
3539 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003540 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 free(buf);
3542 return LY_EVALID;
3543 }
3544 free(buf);
3545
Michal Vaskod989ba02020-08-24 10:59:24 +02003546 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003548 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003549 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003550 LY_CHECK_RET(ret);
3551 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003552 default:
David Sedlákb3077192019-06-19 10:55:37 +02003553 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003554 return LY_EVALID;
3555 }
3556 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557 return ret;
3558}
3559
Michal Vaskoea5abea2018-09-18 13:10:54 +02003560/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003561 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003562 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003563 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003564 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003565 * @param[in,out] argument Value to write to.
3566 * @param[in,out] flags Flags to write to.
3567 * @param[in,out] exts Extension instances to add to.
3568 *
3569 * @return LY_ERR values.
3570 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003572parse_argument(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003574 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003575 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003576 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003577 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578
3579 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003580 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581 return LY_EVALID;
3582 }
3583
3584 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003585 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003586 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587
Michal Vaskod989ba02020-08-24 10:59:24 +02003588 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003590 case LY_STMT_YIN_ELEMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003591 LY_CHECK_RET(parse_yinelement(ctx, in, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003593 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003594 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 break;
3596 default:
David Sedlákb3077192019-06-19 10:55:37 +02003597 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 return LY_EVALID;
3599 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601 return ret;
3602}
3603
Michal Vaskoea5abea2018-09-18 13:10:54 +02003604/**
3605 * @brief Parse the extension statement.
3606 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003607 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003608 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003609 * @param[in,out] extensions Extensions to add to.
3610 *
3611 * @return LY_ERR values.
3612 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003614parse_extension(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003616 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003618 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003619 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 struct lysp_ext *ex;
3621
Radek Krejci2c4e7172018-10-19 15:56:26 +02003622 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623
3624 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003625 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003626 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627
Michal Vaskod989ba02020-08-24 10:59:24 +02003628 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003630 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003631 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003633 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003634 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003636 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003637 LY_CHECK_RET(parse_status(ctx, in, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003639 case LY_STMT_ARGUMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003640 LY_CHECK_RET(parse_argument(ctx, in, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003642 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003643 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 break;
3645 default:
David Sedlákb3077192019-06-19 10:55:37 +02003646 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 return LY_EVALID;
3648 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650 return ret;
3651}
3652
Michal Vaskoea5abea2018-09-18 13:10:54 +02003653/**
3654 * @brief Parse the deviate statement.
3655 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003656 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003657 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003658 * @param[in,out] deviates Deviates to add to.
3659 *
3660 * @return LY_ERR values.
3661 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003662LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003663parse_deviate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003665 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003666 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003667 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003668 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003669 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003670 struct lysp_deviate_add *d_add = NULL;
3671 struct lysp_deviate_rpl *d_rpl = NULL;
3672 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003673 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3674 struct lysp_restr **d_musts = NULL;
3675 uint16_t *d_flags = 0;
3676 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003677
3678 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003679 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003680
3681 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3682 dev_mod = LYS_DEV_NOT_SUPPORTED;
3683 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3684 dev_mod = LYS_DEV_ADD;
3685 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3686 dev_mod = LYS_DEV_REPLACE;
3687 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3688 dev_mod = LYS_DEV_DELETE;
3689 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003690 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003691 free(buf);
3692 return LY_EVALID;
3693 }
3694 free(buf);
3695
3696 /* create structure */
3697 switch (dev_mod) {
3698 case LYS_DEV_NOT_SUPPORTED:
3699 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003700 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701 break;
3702 case LYS_DEV_ADD:
3703 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003704 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003705 d = (struct lysp_deviate *)d_add;
3706 d_units = &d_add->units;
3707 d_uniques = &d_add->uniques;
3708 d_dflts = &d_add->dflts;
3709 d_musts = &d_add->musts;
3710 d_flags = &d_add->flags;
3711 d_min = &d_add->min;
3712 d_max = &d_add->max;
3713 break;
3714 case LYS_DEV_REPLACE:
3715 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003716 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717 d = (struct lysp_deviate *)d_rpl;
3718 d_units = &d_rpl->units;
3719 d_flags = &d_rpl->flags;
3720 d_min = &d_rpl->min;
3721 d_max = &d_rpl->max;
3722 break;
3723 case LYS_DEV_DELETE:
3724 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003725 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726 d = (struct lysp_deviate *)d_del;
3727 d_units = &d_del->units;
3728 d_uniques = &d_del->uniques;
3729 d_dflts = &d_del->dflts;
3730 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003731 break;
3732 default:
3733 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003734 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 }
3736 d->mod = dev_mod;
3737
3738 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003739 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003740
Michal Vaskod989ba02020-08-24 10:59:24 +02003741 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003743 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003744 switch (dev_mod) {
3745 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003746 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003747 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748 return LY_EVALID;
3749 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003750 LY_CHECK_RET(parse_config(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003751 break;
3752 }
3753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003754 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755 switch (dev_mod) {
3756 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003757 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003758 return LY_EVALID;
3759 case LYS_DEV_REPLACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003760 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 break;
3762 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003763 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764 break;
3765 }
3766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003767 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 switch (dev_mod) {
3769 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003770 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003771 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 return LY_EVALID;
3773 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003774 LY_CHECK_RET(parse_mandatory(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 break;
3776 }
3777 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003778 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 switch (dev_mod) {
3780 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003781 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003782 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 return LY_EVALID;
3784 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003785 LY_CHECK_RET(parse_maxelements(ctx, in, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786 break;
3787 }
3788 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003789 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003790 switch (dev_mod) {
3791 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003792 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003793 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794 return LY_EVALID;
3795 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003796 LY_CHECK_RET(parse_minelements(ctx, in, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003797 break;
3798 }
3799 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003800 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 switch (dev_mod) {
3802 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003803 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003804 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805 return LY_EVALID;
3806 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003807 LY_CHECK_RET(parse_restrs(ctx, in, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 break;
3809 }
3810 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003811 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 switch (dev_mod) {
3813 case LYS_DEV_NOT_SUPPORTED:
3814 case LYS_DEV_ADD:
3815 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003816 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003817 return LY_EVALID;
3818 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003819 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003820 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003821 return LY_EVALID;
3822 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003824 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02003825 LY_CHECK_RET(parse_type(ctx, in, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 break;
3827 }
3828 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003829 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 switch (dev_mod) {
3831 case LYS_DEV_NOT_SUPPORTED:
3832 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003833 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 return LY_EVALID;
3835 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003836 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003837 break;
3838 }
3839 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003840 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 switch (dev_mod) {
3842 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003843 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844 return LY_EVALID;
3845 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003846 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847 break;
3848 }
3849 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003850 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003851 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 break;
3853 default:
David Sedlákb3077192019-06-19 10:55:37 +02003854 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855 return LY_EVALID;
3856 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003857 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858 return ret;
3859}
3860
Michal Vaskoea5abea2018-09-18 13:10:54 +02003861/**
3862 * @brief Parse the deviation statement.
3863 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003864 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003865 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003866 * @param[in,out] deviations Deviations to add to.
3867 *
3868 * @return LY_ERR values.
3869 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003870LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003871parse_deviation(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003873 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003875 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003876 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 struct lysp_deviation *dev;
3878
Radek Krejci2c4e7172018-10-19 15:56:26 +02003879 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880
3881 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003882 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003883 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci44ceedc2018-10-02 15:54:31 +02003884 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885
Michal Vasko63f3d842020-07-08 10:10:14 +02003886 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003888 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003889 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003890 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003891 case LY_STMT_DEVIATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003892 LY_CHECK_RET(parse_deviate(ctx, in, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003893 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003894 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003895 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003896 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003897 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003898 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003899 break;
3900 default:
David Sedlákb3077192019-06-19 10:55:37 +02003901 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 return LY_EVALID;
3903 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003905 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003906checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907 /* mandatory substatements */
3908 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003909 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910 return LY_EVALID;
3911 }
3912
3913 return ret;
3914}
3915
Michal Vaskoea5abea2018-09-18 13:10:54 +02003916/**
3917 * @brief Parse the feature statement.
3918 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003919 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003920 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003921 * @param[in,out] features Features to add to.
3922 *
3923 * @return LY_ERR values.
3924 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003925LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003926parse_feature(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003927{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003928 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003930 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003931 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003932 struct lysp_feature *feat;
3933
Radek Krejci2c4e7172018-10-19 15:56:26 +02003934 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003935
3936 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003937 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003938 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003939
Michal Vaskod989ba02020-08-24 10:59:24 +02003940 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003941 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003942 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003943 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003945 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003946 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003948 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003949 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003951 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003952 LY_CHECK_RET(parse_status(ctx, in, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003954 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003955 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956 break;
3957 default:
David Sedlákb3077192019-06-19 10:55:37 +02003958 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003959 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962 return ret;
3963}
3964
Michal Vaskoea5abea2018-09-18 13:10:54 +02003965/**
3966 * @brief Parse the identity statement.
3967 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003968 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003969 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003970 * @param[in,out] identities Identities to add to.
3971 *
3972 * @return LY_ERR values.
3973 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003974LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003975parse_identity(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003977 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003979 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003980 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981 struct lysp_ident *ident;
3982
Radek Krejci2c4e7172018-10-19 15:56:26 +02003983 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984
3985 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003986 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003987 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003988
Michal Vaskod989ba02020-08-24 10:59:24 +02003989 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003991 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003992 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003994 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02003995 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Michal Vasko63f3d842020-07-08 10:10:14 +02003996 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003998 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003999 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004001 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02004002 LY_CHECK_RET(parse_status(ctx, in, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004004 case LY_STMT_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004005 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02004006 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 +01004007 return LY_EVALID;
4008 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004009 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004011 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004012 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 break;
4014 default:
David Sedlákb3077192019-06-19 10:55:37 +02004015 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016 return LY_EVALID;
4017 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004018 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019 return ret;
4020}
4021
Michal Vaskoea5abea2018-09-18 13:10:54 +02004022/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004023 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004024 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004025 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004026 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004027 * @param[in,out] mod Module to write to.
4028 *
4029 * @return LY_ERR values.
4030 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004031LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004032parse_module(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033{
4034 LY_ERR ret = 0;
4035 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004036 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004037 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004038 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004039 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040
4041 /* (sub)module name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004042 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004043 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004044
Michal Vasko63f3d842020-07-08 10:10:14 +02004045 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004046
Radek Krejcie3846472018-10-15 15:24:51 +02004047#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004048 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 +02004049
Michal Vasko7fbc8162018-09-17 10:35:16 +02004050 switch (kw) {
4051 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004052 case LY_STMT_NAMESPACE:
4053 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004054 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4055 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004056 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004057 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004058 break;
4059 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004060 case LY_STMT_INCLUDE:
4061 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004062 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004063 break;
4064 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004065 case LY_STMT_ORGANIZATION:
4066 case LY_STMT_CONTACT:
4067 case LY_STMT_DESCRIPTION:
4068 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004069 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004070 break;
4071
4072 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004073 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004074 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004075 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004077 case LY_STMT_ANYDATA:
4078 case LY_STMT_ANYXML:
4079 case LY_STMT_AUGMENT:
4080 case LY_STMT_CHOICE:
4081 case LY_STMT_CONTAINER:
4082 case LY_STMT_DEVIATION:
4083 case LY_STMT_EXTENSION:
4084 case LY_STMT_FEATURE:
4085 case LY_STMT_GROUPING:
4086 case LY_STMT_IDENTITY:
4087 case LY_STMT_LEAF:
4088 case LY_STMT_LEAF_LIST:
4089 case LY_STMT_LIST:
4090 case LY_STMT_NOTIFICATION:
4091 case LY_STMT_RPC:
4092 case LY_STMT_TYPEDEF:
4093 case LY_STMT_USES:
4094 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095 mod_stmt = Y_MOD_BODY;
4096 break;
4097 default:
4098 /* error handled in the next switch */
4099 break;
4100 }
Radek Krejcie3846472018-10-15 15:24:51 +02004101#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102
Radek Krejcie3846472018-10-15 15:24:51 +02004103 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004104 switch (kw) {
4105 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004106 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004107 LY_CHECK_RET(parse_yangversion(ctx, in, &mod->mod->version, &mod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004108 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004109 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004110 case LY_STMT_NAMESPACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004111 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004113 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02004114 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 break;
4116
4117 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004118 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004119 LY_CHECK_RET(parse_include(ctx, mod->mod->name, in, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004121 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004122 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, in, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004123 break;
4124
4125 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004126 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004127 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004128 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004129 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004130 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004132 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004133 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->mod->dsc, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004136 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004137 break;
4138
4139 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004140 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004141 LY_CHECK_RET(parse_revision(ctx, in, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004142 break;
4143
4144 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004145 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004146 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004147 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004148 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004149 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004151 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004152 LY_CHECK_RET(parse_choice(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004154 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004155 LY_CHECK_RET(parse_container(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004157 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004158 LY_CHECK_RET(parse_leaf(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004160 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004161 LY_CHECK_RET(parse_leaflist(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004163 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004164 LY_CHECK_RET(parse_list(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004167 LY_CHECK_RET(parse_uses(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 break;
4169
Radek Krejcid6b76452019-09-03 17:03:03 +02004170 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004171 LY_CHECK_RET(parse_augment(ctx, in, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004173 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004174 LY_CHECK_RET(parse_deviation(ctx, in, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004176 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004177 LY_CHECK_RET(parse_extension(ctx, in, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004179 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004180 LY_CHECK_RET(parse_feature(ctx, in, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004182 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004183 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004186 LY_CHECK_RET(parse_identity(ctx, in, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004189 LY_CHECK_RET(parse_notif(ctx, in, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004191 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004192 LY_CHECK_RET(parse_action(ctx, in, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004194 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004195 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004197 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004198 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 break;
4200
4201 default:
David Sedlákb3077192019-06-19 10:55:37 +02004202 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004203 return LY_EVALID;
4204 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004206 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004207
Radek Krejci6d6556c2018-11-08 09:37:45 +01004208checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004209 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004210 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 +01004211
Michal Vasko7fbc8162018-09-17 10:35:16 +02004212 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004213 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004214 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004215 return LY_EVALID;
4216 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004217 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004218 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219 }
4220
Radek Krejcie9e987e2018-10-31 12:50:27 +01004221 /* submodules share the namespace with the module names, so there must not be
4222 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004223 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4224 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004225 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004226 return LY_EVALID;
4227 }
4228
4229 return ret;
4230}
4231
4232/**
4233 * @brief Parse submodule substatements.
4234 *
4235 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004236 * @param[in,out] in Input structure.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004237 * @param[out] submod Parsed submodule structure.
4238 *
4239 * @return LY_ERR values.
4240 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004241LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004242parse_submodule(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004243{
4244 LY_ERR ret = 0;
4245 char *buf, *word;
4246 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004247 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004248 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4249 struct lysp_submodule *dup;
4250
4251 /* submodule name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004252 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004253 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4254
Michal Vasko63f3d842020-07-08 10:10:14 +02004255 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004256
4257#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004258 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 +01004259
4260 switch (kw) {
4261 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004262 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004263 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004265 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004266 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4267 break;
4268 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004269 case LY_STMT_INCLUDE:
4270 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004271 CHECK_ORDER(Y_MOD_LINKAGE);
4272 break;
4273 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004274 case LY_STMT_ORGANIZATION:
4275 case LY_STMT_CONTACT:
4276 case LY_STMT_DESCRIPTION:
4277 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004278 CHECK_ORDER(Y_MOD_META);
4279 break;
4280
4281 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004282 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004283 CHECK_ORDER(Y_MOD_REVISION);
4284 break;
4285 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004286 case LY_STMT_ANYDATA:
4287 case LY_STMT_ANYXML:
4288 case LY_STMT_AUGMENT:
4289 case LY_STMT_CHOICE:
4290 case LY_STMT_CONTAINER:
4291 case LY_STMT_DEVIATION:
4292 case LY_STMT_EXTENSION:
4293 case LY_STMT_FEATURE:
4294 case LY_STMT_GROUPING:
4295 case LY_STMT_IDENTITY:
4296 case LY_STMT_LEAF:
4297 case LY_STMT_LEAF_LIST:
4298 case LY_STMT_LIST:
4299 case LY_STMT_NOTIFICATION:
4300 case LY_STMT_RPC:
4301 case LY_STMT_TYPEDEF:
4302 case LY_STMT_USES:
4303 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004304 mod_stmt = Y_MOD_BODY;
4305 break;
4306 default:
4307 /* error handled in the next switch */
4308 break;
4309 }
4310#undef CHECK_ORDER
4311
4312 prev_kw = kw;
4313 switch (kw) {
4314 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004315 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004316 LY_CHECK_RET(parse_yangversion(ctx, in, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004317 ctx->mod_version = submod->version;
4318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004319 case LY_STMT_BELONGS_TO:
Michal Vasko63f3d842020-07-08 10:10:14 +02004320 LY_CHECK_RET(parse_belongsto(ctx, in, &submod->belongsto, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004321 break;
4322
4323 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004324 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004325 LY_CHECK_RET(parse_include(ctx, submod->name, in, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004327 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004328 LY_CHECK_RET(parse_import(ctx, submod->prefix, in, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004329 break;
4330
4331 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004332 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004333 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004336 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004339 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004342 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004343 break;
4344
4345 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004346 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004347 LY_CHECK_RET(parse_revision(ctx, in, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004348 break;
4349
4350 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004351 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004352 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004353 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004354 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004355 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004357 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004358 LY_CHECK_RET(parse_choice(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004361 LY_CHECK_RET(parse_container(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004364 LY_CHECK_RET(parse_leaf(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004367 LY_CHECK_RET(parse_leaflist(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004370 LY_CHECK_RET(parse_list(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004373 LY_CHECK_RET(parse_uses(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004374 break;
4375
Radek Krejcid6b76452019-09-03 17:03:03 +02004376 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004377 LY_CHECK_RET(parse_augment(ctx, in, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004379 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004380 LY_CHECK_RET(parse_deviation(ctx, in, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004382 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004383 LY_CHECK_RET(parse_extension(ctx, in, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004385 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004386 LY_CHECK_RET(parse_feature(ctx, in, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004389 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004392 LY_CHECK_RET(parse_identity(ctx, in, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004395 LY_CHECK_RET(parse_notif(ctx, in, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004397 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004398 LY_CHECK_RET(parse_action(ctx, in, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004399 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004400 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004401 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004403 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004404 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004405 break;
4406
4407 default:
David Sedlákb3077192019-06-19 10:55:37 +02004408 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004409 return LY_EVALID;
4410 }
4411 }
4412 LY_CHECK_RET(ret);
4413
4414checks:
4415 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004416 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments,
4417 submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004418
4419 /* mandatory substatements */
4420 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004421 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004422 return LY_EVALID;
4423 }
4424
4425 /* submodules share the namespace with the module names, so there must not be
4426 * a submodule of the same name in the context, no need for revision matching */
4427 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4428 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004429 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004430 return LY_EVALID;
4431 }
4432
Michal Vasko7fbc8162018-09-17 10:35:16 +02004433 return ret;
4434}
4435
Radek Krejcid4557c62018-09-17 11:42:09 +02004436LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004437yang_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 +02004438 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004439{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004440 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004441 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004442 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004443 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004444 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004445
David Sedlák1b623122019-08-05 15:27:49 +02004446 /* create context */
4447 *context = calloc(1, sizeof **context);
4448 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004449 (*context)->format = LYS_IN_YANG;
Michal Vasko7c8439f2020-08-05 13:25:19 +02004450 (*context)->main_mod = main_ctx->main_mod;
David Sedlák1b623122019-08-05 15:27:49 +02004451 (*context)->ctx = ly_ctx;
Radek Krejci99435242019-09-05 16:19:15 +02004452 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004453 (*context)->line = 1;
4454
4455 /* map the typedefs and groupings list from main context to the submodule's context */
4456 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4457 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4458
Michal Vasko7fbc8162018-09-17 10:35:16 +02004459 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004460 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004461 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004462
Radek Krejcid6b76452019-09-03 17:03:03 +02004463 if (kw == LY_STMT_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004464 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004465 ret = LY_EINVAL;
4466 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004467 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004468 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004469 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004470 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004471 }
4472
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004473 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004474 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004475 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004476
4477 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004478 ret = parse_submodule(*context, in, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004479 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004480
4481 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004482 while (isspace(in->current[0])) {
4483 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004484 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004485 if (in->current[0]) {
4486 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004487 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004488 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004489 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004490
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004491 mod_p->parsing = 0;
4492 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004493
Radek Krejcibbe09a92018-11-08 09:36:54 +01004494cleanup:
4495 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004496 lysp_submodule_free((*context)->ctx, mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004497 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004498 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004499 }
4500
4501 return ret;
4502}
4503
4504LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004505yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004506{
4507 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004508 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004509 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004510 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004511 struct lysp_module *mod_p = NULL;
4512
David Sedlák1b623122019-08-05 15:27:49 +02004513 /* create context */
4514 *context = calloc(1, sizeof **context);
4515 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004516 (*context)->format = LYS_IN_YANG;
Michal Vasko7c8439f2020-08-05 13:25:19 +02004517 (*context)->main_mod = mod;
David Sedlák1b623122019-08-05 15:27:49 +02004518 (*context)->ctx = mod->ctx;
Radek Krejci335332a2019-09-05 13:03:35 +02004519 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004520 (*context)->line = 1;
4521
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004522 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004523 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004524 LY_CHECK_GOTO(ret, cleanup);
4525
Radek Krejcid6b76452019-09-03 17:03:03 +02004526 if (kw == LY_STMT_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004527 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004528 ret = LY_EINVAL;
4529 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004530 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004531 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004532 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004533 goto cleanup;
4534 }
4535
4536 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004537 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004538 mod_p->mod = mod;
4539 mod_p->parsing = 1;
4540
4541 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004542 ret = parse_module(*context, in, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004543 LY_CHECK_GOTO(ret, cleanup);
4544
4545 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004546 while (isspace(in->current[0])) {
4547 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004548 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004549 if (in->current[0]) {
4550 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004551 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004552 goto cleanup;
4553 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004554
4555 mod_p->parsing = 0;
4556 mod->parsed = mod_p;
4557
4558cleanup:
4559 if (ret) {
4560 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004561 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004562 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004563 }
4564
Michal Vasko7fbc8162018-09-17 10:35:16 +02004565 return ret;
4566}