blob: 15ae77cdf64184027bd7ab36c00348f9e491018d [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"
Radek Krejcica376bd2020-06-11 16:04:06 +020028#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020029#include "path.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 Krejci011e4aa2020-09-04 15:22:31 +020043#define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \
44 if (BUF) {LY_CHECK_RET(lydict_insert_zc((CTX)->ctx, WORD, &(TARGET)));}\
45 else {LY_CHECK_RET(lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN, &(TARGET)));}
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 Krejci857189e2020-09-01 13:26:36 +0200142 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200144 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200145 size_t len;
146
Radek Krejcif29b7c32019-04-09 16:17:49 +0200147 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
148 assert(!need_buf || (need_buf && word_b));
149
Radek Krejci44ceedc2018-10-02 15:54:31 +0200150 /* get UTF8 code point (and number of bytes coding the character) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200151 LY_CHECK_ERR_RET(ly_getutf8(&in->current, &c, &len),
Michal Vasko69730152020-10-09 16:30:07 +0200152 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, in->current[-len]), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +0200153 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
Radek Krejci1deb5be2020-08-26 16:43:36 +0200223skip_comment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t 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 Krejci1deb5be2020-08-26 16:43:36 +0200298 uint8_t string;
299 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200300 ly_bool need_buf = 0;
301 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200302 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200303 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200304
Michal Vasko63f3d842020-07-08 10:10:14 +0200305 if (in->current[0] == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200306 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200307 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200308 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200309 assert(in->current[0] == '\'');
Michal Vasko7fbc8162018-09-17 10:35:16 +0200310 string = 1;
311 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200312 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200313
Michal Vasko63f3d842020-07-08 10:10:14 +0200314 while (in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200315 switch (string) {
316 case 1:
Michal Vasko63f3d842020-07-08 10:10:14 +0200317 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200318 case '\'':
319 /* string may be finished, but check for + */
320 string = 4;
Michal Vasko63f3d842020-07-08 10:10:14 +0200321 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200322 break;
323 default:
324 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200325 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 +0200326 break;
327 }
328 break;
329 case 2:
Michal Vasko63f3d842020-07-08 10:10:14 +0200330 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200331 case '\"':
332 /* string may be finished, but check for + */
333 string = 4;
Michal Vasko63f3d842020-07-08 10:10:14 +0200334 MOVE_INPUT(ctx, in, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200335 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200336 break;
337 case '\\':
338 /* special character following */
339 string = 3;
Radek Krejciff13cd12019-10-25 15:34:24 +0200340
341 /* the backslash sequence is substituted, so we will need a buffer to store the result */
342 need_buf = 1;
343
344 /* move forward to the escaped character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200345 ++in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200346
347 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
348 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
349 trailing_ws = 0;
350
351 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
352 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200353 break;
354 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200355 if (current_indent < block_indent) {
356 ++current_indent;
Michal Vasko63f3d842020-07-08 10:10:14 +0200357 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200358 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100359 /* check and store whitespace character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200360 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 +0100361 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200362 }
363 break;
364 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200365 if (current_indent < block_indent) {
366 assert(need_buf);
367 current_indent += 8;
368 ctx->indent += 8;
Michal Vaskod989ba02020-08-24 10:59:24 +0200369 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200370 /* store leftover spaces from the tab */
Michal Vasko63f3d842020-07-08 10:10:14 +0200371 c = in->current;
372 in->current = " ";
373 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
374 in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100375 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200376 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200377 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200378 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100379 /* check and store whitespace character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200380 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 +0100381 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200382 /* additional characters for indentation - only 1 was count in buf_store_char */
383 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200384 }
385 break;
386 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200387 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200388 /* we will be removing the indents so we need our own buffer */
389 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200390
391 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200392 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200393
Radek Krejciff13cd12019-10-25 15:34:24 +0200394 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200395 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200396 }
397
398 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200399 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 +0200400
401 /* maintain line number */
402 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200403
404 /* reset context indentation counter for possible string after this one */
405 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200406 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200407 break;
408 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200409 /* first non-whitespace character, stop eating indentation */
410 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200411
412 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200413 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 +0200414 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200415 break;
416 }
417 break;
418 case 3:
419 /* string encoded characters */
Michal Vasko63f3d842020-07-08 10:10:14 +0200420 c = in->current;
421 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200422 case 'n':
Michal Vasko63f3d842020-07-08 10:10:14 +0200423 in->current = "\n";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200424 break;
425 case 't':
Michal Vasko63f3d842020-07-08 10:10:14 +0200426 in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200427 break;
428 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200430 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200431 break;
432 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200433 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Michal Vasko69730152020-10-09 16:30:07 +0200434 in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200435 return LY_EVALID;
436 }
437
438 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200439 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 +0200440
441 string = 2;
Michal Vasko63f3d842020-07-08 10:10:14 +0200442 in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200443 break;
444 case 4:
Michal Vasko63f3d842020-07-08 10:10:14 +0200445 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200446 case '+':
447 /* string continues */
448 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200449 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200450 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200451 case '\n':
452 ++ctx->line;
Radek Krejci0f969882020-08-21 16:56:47 +0200453 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 case ' ':
455 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200456 /* just skip */
457 break;
458 default:
459 /* string is finished */
460 goto string_end;
461 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200462 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200463 break;
464 case 5:
Michal Vasko63f3d842020-07-08 10:10:14 +0200465 switch (in->current[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200466 case '\n':
467 ++ctx->line;
Radek Krejci0f969882020-08-21 16:56:47 +0200468 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200469 case ' ':
470 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200471 /* skip */
472 break;
473 case '\'':
474 string = 1;
475 break;
476 case '\"':
477 string = 2;
478 break;
479 default:
480 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200481 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200482 return LY_EVALID;
483 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200484 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200485 break;
486 default:
487 return LY_EINT;
488 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200489 }
490
491string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200492 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200493 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200494 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200495 return LY_EVALID;
496 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200497 return LY_SUCCESS;
498}
499
Michal Vaskoea5abea2018-09-18 13:10:54 +0200500/**
501 * @brief Get another YANG string from the raw data.
502 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200503 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200504 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200505 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200506 * @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 +0200507 * @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 +0200508 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
509 * set to NULL. Otherwise equal to \p word_p.
510 * @param[out] word_len Length of the read string.
511 *
512 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200513 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200514LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200515get_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 +0200516 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200517{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200518 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200519 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200520
Michal Vasko7fbc8162018-09-17 10:35:16 +0200521 /* word buffer - dynamically allocated */
522 *word_b = NULL;
523
524 /* word pointer - just a pointer to data */
525 *word_p = NULL;
526
527 *word_len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200528 while (in->current[0]) {
529 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200530 case '\'':
531 case '\"':
532 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200533 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Michal Vasko63f3d842020-07-08 10:10:14 +0200534 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200535 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200536 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200538 if (flags) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200539 (*flags) |= in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200540 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200541 LY_CHECK_RET(read_qstring(ctx, in, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200542 goto str_end;
543 case '/':
Michal Vasko63f3d842020-07-08 10:10:14 +0200544 if (in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200545 /* one-line comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200546 MOVE_INPUT(ctx, in, 2);
547 LY_CHECK_RET(skip_comment(ctx, in, 1));
548 } else if (in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200549 /* block comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200550 MOVE_INPUT(ctx, in, 2);
551 LY_CHECK_RET(skip_comment(ctx, in, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200552 } else {
553 /* not a comment after all */
Michal Vasko63f3d842020-07-08 10:10:14 +0200554 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 +0200555 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200556 break;
557 case ' ':
558 if (*word_len) {
559 /* word is finished */
560 goto str_end;
561 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200562 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200563 break;
564 case '\t':
565 if (*word_len) {
566 /* word is finished */
567 goto str_end;
568 }
569 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200570 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200571
Michal Vasko63f3d842020-07-08 10:10:14 +0200572 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200573 break;
574 case '\n':
575 if (*word_len) {
576 /* word is finished */
577 goto str_end;
578 }
579 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200580 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200581
582 /* track line numbers */
583 ++ctx->line;
584
Michal Vasko63f3d842020-07-08 10:10:14 +0200585 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200586 break;
587 case ';':
588 case '{':
589 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
590 /* word is finished */
591 goto str_end;
592 }
593
Michal Vasko63f3d842020-07-08 10:10:14 +0200594 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200595 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200596 case '}':
597 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200598 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200599 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200600 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200601 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200602 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 +0200603 break;
604 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200605 }
606
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200607 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200608 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200609 return LY_EVALID;
610
Michal Vasko7fbc8162018-09-17 10:35:16 +0200611str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200612 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200613 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200614 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
615 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
616 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200617 *word_p = *word_b;
618 }
619
620 return LY_SUCCESS;
621}
622
Michal Vaskoea5abea2018-09-18 13:10:54 +0200623/**
624 * @brief Get another YANG keyword from the raw data.
625 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200626 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200627 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200628 * @param[out] kw YANG keyword read.
629 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
630 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
631 *
632 * @return LY_ERR values.
633 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200634LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200635get_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 +0200636{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200637 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200638 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200639 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200640
641 if (word_p) {
642 *word_p = NULL;
643 *word_len = 0;
644 }
645
646 /* first skip "optsep", comments */
Michal Vasko63f3d842020-07-08 10:10:14 +0200647 while (in->current[0]) {
648 switch (in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200649 case '/':
Michal Vasko63f3d842020-07-08 10:10:14 +0200650 if (in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200651 /* one-line comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200652 MOVE_INPUT(ctx, in, 2);
653 LY_CHECK_RET(skip_comment(ctx, in, 1));
654 } else if (in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655 /* block comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200656 MOVE_INPUT(ctx, in, 2);
657 LY_CHECK_RET(skip_comment(ctx, in, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200658 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200659 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200660 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200661 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200662 }
Radek Krejci13028282019-06-11 14:56:48 +0200663 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200664 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200665 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200666 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200667 ctx->indent = 0;
668 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200669 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200670 /* skip whitespaces (optsep) */
671 ++ctx->indent;
672 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200673 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200674 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200675 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200676 break;
677 default:
678 /* either a keyword start or an invalid character */
679 goto keyword_start;
680 }
681
Michal Vasko63f3d842020-07-08 10:10:14 +0200682 ly_in_skip(in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200683 }
684
685keyword_start:
Michal Vasko63f3d842020-07-08 10:10:14 +0200686 word_start = in->current;
687 *kw = lysp_match_kw(ctx, in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688
Michal Vasko69730152020-10-09 16:30:07 +0200689 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 +0200690 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200691 }
692
Radek Krejcid6b76452019-09-03 17:03:03 +0200693 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200694 /* make sure we have the whole keyword */
Michal Vasko63f3d842020-07-08 10:10:14 +0200695 switch (in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200696 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200697 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200698 case ' ':
699 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200700 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200701 case ':':
702 /* keyword is not actually a keyword, but prefix of an extension.
703 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
704 * and we will be checking the keyword (extension instance) itself */
705 prefix = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200706 MOVE_INPUT(ctx, in, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200707 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200708 case '{':
709 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200710 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200711 break;
712 }
Radek Krejci0f969882020-08-21 16:56:47 +0200713 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200714 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200715 MOVE_INPUT(ctx, in, 1);
716 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200717 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200718 return LY_EVALID;
719 }
720 } else {
721 /* still can be an extension */
722 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200723extension:
Michal Vasko69730152020-10-09 16:30:07 +0200724 while (in->current[0] && (in->current[0] != ' ') && (in->current[0] != '\t') && (in->current[0] != '\n') &&
725 (in->current[0] != '{') && (in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200726 uint32_t c = 0;
727
Michal Vasko63f3d842020-07-08 10:10:14 +0200728 LY_CHECK_ERR_RET(ly_getutf8(&in->current, &c, &len),
Michal Vasko69730152020-10-09 16:30:07 +0200729 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200730 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200731 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200732 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Michal Vasko69730152020-10-09 16:30:07 +0200733 in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200734 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200735 if (!in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200736 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737 return LY_EVALID;
738 }
739
740 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200741 if (prefix != 2) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200742 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200743 return LY_EVALID;
744 }
745
Radek Krejcid6b76452019-09-03 17:03:03 +0200746 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200747 }
Radek Krejci626df482018-10-11 15:06:31 +0200748success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200749 if (word_p) {
750 *word_p = (char *)word_start;
Michal Vasko63f3d842020-07-08 10:10:14 +0200751 *word_len = in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 }
753
754 return LY_SUCCESS;
755}
756
Michal Vaskoea5abea2018-09-18 13:10:54 +0200757/**
758 * @brief Parse extension instance substatements.
759 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200760 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200761 * @param[in,out] in Input structure.
Radek Krejci335332a2019-09-05 13:03:35 +0200762 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200763 * @param[in] word Extension instance substatement name (keyword).
764 * @param[in] word_len Extension instance substatement name length.
765 * @param[in,out] child Children of this extension instance to add to.
766 *
767 * @return LY_ERR values.
768 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200769static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200770parse_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 +0200771 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200772{
773 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100774 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200775 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200776 struct lysp_stmt *stmt, *par_child;
777
778 stmt = calloc(1, sizeof *stmt);
779 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
780
Radek Krejcibb9b1982019-04-08 14:24:59 +0200781 /* insert into parent statements */
782 if (!*child) {
783 *child = stmt;
784 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200785 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200786 par_child->next = stmt;
787 }
788
Radek Krejci011e4aa2020-09-04 15:22:31 +0200789 LY_CHECK_RET(lydict_insert(ctx->ctx, word, word_len, &stmt->stmt));
Radek Krejci335332a2019-09-05 13:03:35 +0200790 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200791
792 /* get optional argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200793 LY_CHECK_RET(get_argument(ctx, in, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200794
Radek Krejci0ae092d2018-09-20 16:43:19 +0200795 if (word) {
796 if (buf) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200797 LY_CHECK_RET(lydict_insert_zc(ctx->ctx, word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200798 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200799 LY_CHECK_RET(lydict_insert(ctx->ctx, word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200800 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200801 }
802
Michal Vasko63f3d842020-07-08 10:10:14 +0200803 YANG_READ_SUBSTMT_FOR(ctx, in, child_kw, word, word_len, ret, ) {
804 LY_CHECK_RET(parse_ext_substmt(ctx, in, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200805 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200806 return ret;
807}
808
Michal Vaskoea5abea2018-09-18 13:10:54 +0200809/**
810 * @brief Parse extension instance.
811 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200812 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200813 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200814 * @param[in] ext_name Extension instance substatement name (keyword).
815 * @param[in] ext_name_len Extension instance substatement name length.
816 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
817 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
818 * @param[in,out] exts Extension instances to add to.
819 *
820 * @return LY_ERR values.
821 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200822static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200823parse_ext(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char *ext_name, size_t ext_name_len, LYEXT_SUBSTMT insubstmt,
Radek Krejci0f969882020-08-21 16:56:47 +0200824 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200825{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100826 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200827 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200828 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200829 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200830 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200831
Radek Krejci2c4e7172018-10-19 15:56:26 +0200832 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200833
834 /* store name and insubstmt info */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200835 LY_CHECK_RET(lydict_insert(ctx->ctx, ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200836 e->insubstmt = insubstmt;
837 e->insubstmt_index = insubstmt_index;
838
839 /* get optional argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200840 LY_CHECK_RET(get_argument(ctx, in, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841
Radek Krejci0ae092d2018-09-20 16:43:19 +0200842 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200843 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200844 }
845
Michal Vaskod989ba02020-08-24 10:59:24 +0200846 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200847 LY_CHECK_RET(parse_ext_substmt(ctx, in, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200848 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200849 return ret;
850}
851
Michal Vaskoea5abea2018-09-18 13:10:54 +0200852/**
853 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
854 * description, etc...
855 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200856 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200857 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200858 * @param[in] substmt Type of this substatement.
859 * @param[in] substmt_index Index of this substatement.
860 * @param[in,out] value Place to store the parsed value.
861 * @param[in] arg Type of the YANG keyword argument (of the value).
862 * @param[in,out] exts Extension instances to add to.
863 *
864 * @return LY_ERR values.
865 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200866static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200867parse_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 +0200868 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200869{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100870 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200871 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200872 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200873 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200874
875 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200876 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200877 return LY_EVALID;
878 }
879
880 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200881 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200882
883 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200884 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200885
Michal Vaskod989ba02020-08-24 10:59:24 +0200886 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200887 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200888 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200889 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200890 break;
891 default:
David Sedlákb3077192019-06-19 10:55:37 +0200892 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200893 return LY_EVALID;
894 }
895 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200896 return ret;
897}
898
Michal Vaskoea5abea2018-09-18 13:10:54 +0200899/**
900 * @brief Parse the yang-version statement.
901 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200902 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200903 * @param[in,out] in Input structure.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100904 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200905 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200906 *
907 * @return LY_ERR values.
908 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200909static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200910parse_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 +0200911{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100912 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200913 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200914 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200915 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100917 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200918 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200919 return LY_EVALID;
920 }
921
922 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200923 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200924
Radek Krejci96e48da2020-09-04 13:18:06 +0200925 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100926 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100928 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200929 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200930 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931 free(buf);
932 return LY_EVALID;
933 }
934 free(buf);
935
Michal Vaskod989ba02020-08-24 10:59:24 +0200936 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200937 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200938 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200939 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200940 break;
941 default:
David Sedlákb3077192019-06-19 10:55:37 +0200942 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200943 return LY_EVALID;
944 }
945 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200946 return ret;
947}
948
Michal Vaskoea5abea2018-09-18 13:10:54 +0200949/**
950 * @brief Parse the belongs-to statement.
951 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200952 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200953 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200954 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
955 * @param[in,out] exts Extension instances to add to.
956 *
957 * @return LY_ERR values.
958 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200959static LY_ERR
Michal Vaskoc3781c32020-10-06 14:04:08 +0200960parse_belongsto(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200961{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100962 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200963 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200964 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200965 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200966
Michal Vaskoc3781c32020-10-06 14:04:08 +0200967 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200968 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200969 return LY_EVALID;
970 }
971
Michal Vaskoc3781c32020-10-06 14:04:08 +0200972 /* get value, it must match the main module */
Michal Vasko63f3d842020-07-08 10:10:14 +0200973 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vaskoc3781c32020-10-06 14:04:08 +0200974 if (ly_strncmp(ctx->main_mod->name, word, word_len)) {
975 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Submodule \"belongs-to\" value \"%.*s\" does not match its module name \"%s\".",
976 (int)word_len, word, ctx->main_mod->name);
977 free(buf);
978 return LY_EVALID;
979 }
980 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200981
Michal Vasko63f3d842020-07-08 10:10:14 +0200982 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200983 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200984 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +0200985 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200986 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200987 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200988 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989 break;
990 default:
David Sedlákb3077192019-06-19 10:55:37 +0200991 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992 return LY_EVALID;
993 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200994 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200995 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100996checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200997 /* mandatory substatements */
998 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200999 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001000 return LY_EVALID;
1001 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001002 return ret;
1003}
1004
Michal Vaskoea5abea2018-09-18 13:10:54 +02001005/**
1006 * @brief Parse the revision-date statement.
1007 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001008 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001009 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001010 * @param[in,out] rev Array to store the parsed value in.
1011 * @param[in,out] exts Extension instances to add to.
1012 *
1013 * @return LY_ERR values.
1014 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001015static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001016parse_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 +02001017{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001018 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001019 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001020 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001021 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001022
1023 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001024 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025 return LY_EVALID;
1026 }
1027
1028 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001029 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001030
1031 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001032 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001033 free(buf);
1034 return LY_EVALID;
1035 }
1036
1037 /* store value and spend buf if allocated */
1038 strncpy(rev, word, word_len);
1039 free(buf);
1040
Michal Vaskod989ba02020-08-24 10:59:24 +02001041 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001042 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001043 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001044 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001045 break;
1046 default:
David Sedlákb3077192019-06-19 10:55:37 +02001047 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001048 return LY_EVALID;
1049 }
1050 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051 return ret;
1052}
1053
Michal Vaskoea5abea2018-09-18 13:10:54 +02001054/**
1055 * @brief Parse the include statement.
1056 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001057 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001058 * @param[in] module_name Name of the module to check name collisions.
Michal Vasko63f3d842020-07-08 10:10:14 +02001059 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001060 * @param[in,out] includes Parsed includes to add to.
1061 *
1062 * @return LY_ERR values.
1063 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001065parse_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 +02001066{
Radek Krejcid33273d2018-10-25 14:55:52 +02001067 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001068 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001069 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001070 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071 struct lysp_include *inc;
1072
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001073 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001074
1075 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001076 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001077
Radek Krejci011e4aa2020-09-04 15:22:31 +02001078 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001079
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001080 /* submodules share the namespace with the module names, so there must not be
1081 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001082 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001083 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001084 return LY_EVALID;
1085 }
1086
Michal Vaskod989ba02020-08-24 10:59:24 +02001087 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001088 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001089 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001090 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko63f3d842020-07-08 10:10:14 +02001091 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 +02001092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001093 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001094 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko63f3d842020-07-08 10:10:14 +02001095 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 +02001096 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001097 case LY_STMT_REVISION_DATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001098 LY_CHECK_RET(parse_revisiondate(ctx, in, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001100 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001101 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102 break;
1103 default:
David Sedlákb3077192019-06-19 10:55:37 +02001104 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001105 return LY_EVALID;
1106 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001108 return ret;
1109}
1110
Michal Vaskoea5abea2018-09-18 13:10:54 +02001111/**
1112 * @brief Parse the import statement.
1113 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001114 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001115 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vasko63f3d842020-07-08 10:10:14 +02001116 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001117 * @param[in,out] imports Parsed imports to add to.
1118 *
1119 * @return LY_ERR values.
1120 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001121static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001122parse_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 +02001123{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001124 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001125 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001126 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001127 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001128 struct lysp_import *imp;
1129
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001130 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001131
1132 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001133 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001134 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135
Michal Vasko63f3d842020-07-08 10:10:14 +02001136 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001138 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02001139 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 +01001140 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 +02001141 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001142 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001143 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko63f3d842020-07-08 10:10:14 +02001144 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 +02001145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001146 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001147 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko63f3d842020-07-08 10:10:14 +02001148 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 +02001149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001150 case LY_STMT_REVISION_DATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001151 LY_CHECK_RET(parse_revisiondate(ctx, in, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001153 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001154 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155 break;
1156 default:
David Sedlákb3077192019-06-19 10:55:37 +02001157 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158 return LY_EVALID;
1159 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001161 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001162checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001163 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001164 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001165
1166 return ret;
1167}
1168
Michal Vaskoea5abea2018-09-18 13:10:54 +02001169/**
1170 * @brief Parse the revision statement.
1171 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001172 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001173 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001174 * @param[in,out] revs Parsed revisions to add to.
1175 *
1176 * @return LY_ERR values.
1177 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001179parse_revision(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001181 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001183 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001184 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185 struct lysp_revision *rev;
1186
Radek Krejci2c4e7172018-10-19 15:56:26 +02001187 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001188
1189 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001190 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191
1192 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001193 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001194 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195 return LY_EVALID;
1196 }
1197
Radek Krejcib7db73a2018-10-24 14:18:40 +02001198 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001199 free(buf);
1200
Michal Vaskod989ba02020-08-24 10:59:24 +02001201 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001202 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001203 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001204 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 +02001205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001206 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001207 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 +02001208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001209 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001210 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 break;
1212 default:
David Sedlákb3077192019-06-19 10:55:37 +02001213 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 return LY_EVALID;
1215 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001216 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 return ret;
1218}
1219
Michal Vaskoea5abea2018-09-18 13:10:54 +02001220/**
1221 * @brief Parse a generic text field that can have more instances such as base.
1222 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001223 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001224 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001225 * @param[in] substmt Type of this substatement.
1226 * @param[in,out] texts Parsed values to add to.
1227 * @param[in] arg Type of the expected argument.
1228 * @param[in,out] exts Extension instances to add to.
1229 *
1230 * @return LY_ERR values.
1231 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001232static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001233parse_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 +02001234 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001235{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001236 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001238 const char **item;
1239 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001240 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241
1242 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001243 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001244
1245 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001246 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001247
Radek Krejci011e4aa2020-09-04 15:22:31 +02001248 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Michal Vaskod989ba02020-08-24 10:59:24 +02001249 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001250 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001251 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001252 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001253 break;
1254 default:
David Sedlákb3077192019-06-19 10:55:37 +02001255 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001256 return LY_EVALID;
1257 }
1258 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001259 return ret;
1260}
1261
Michal Vaskoea5abea2018-09-18 13:10:54 +02001262/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001263 * @brief Parse a generic text field that can have more instances such as base.
1264 *
1265 * @param[in] ctx yang parser context for logging.
1266 * @param[in,out] in Input structure.
1267 * @param[in] substmt Type of this substatement.
1268 * @param[in,out] qnames Parsed qnames to add to.
1269 * @param[in] arg Type of the expected argument.
1270 * @param[in,out] exts Extension instances to add to.
1271 *
1272 * @return LY_ERR values.
1273 */
1274static LY_ERR
1275parse_qnames(struct lys_yang_parser_ctx *ctx, struct ly_in *in, LYEXT_SUBSTMT substmt, struct lysp_qname **qnames,
1276 enum yang_arg arg, struct lysp_ext_instance **exts)
1277{
1278 LY_ERR ret = LY_SUCCESS;
1279 char *buf, *word;
1280 struct lysp_qname *item;
1281 size_t word_len;
1282 enum ly_stmt kw;
1283
1284 /* allocate new pointer */
1285 LY_ARRAY_NEW_RET(ctx->ctx, *qnames, item, LY_EMEM);
1286
1287 /* get value */
1288 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
1289
1290 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
1291 item->mod = ctx->main_mod;
1292 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
1293 switch (kw) {
1294 case LY_STMT_EXTENSION_INSTANCE:
1295 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
1296 break;
1297 default:
1298 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
1299 return LY_EVALID;
1300 }
1301 }
1302 return ret;
1303}
1304
1305/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001306 * @brief Parse the config statement.
1307 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001308 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001309 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001310 * @param[in,out] flags Flags to add to.
1311 * @param[in,out] exts Extension instances to add to.
1312 *
1313 * @return LY_ERR values.
1314 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001316parse_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 +02001317{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001318 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001320 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001321 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001322
1323 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001324 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325 return LY_EVALID;
1326 }
1327
1328 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001329 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330
1331 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1332 *flags |= LYS_CONFIG_W;
1333 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1334 *flags |= LYS_CONFIG_R;
1335 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001336 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337 free(buf);
1338 return LY_EVALID;
1339 }
1340 free(buf);
1341
Michal Vaskod989ba02020-08-24 10:59:24 +02001342 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001343 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001344 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001345 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346 break;
1347 default:
David Sedlákb3077192019-06-19 10:55:37 +02001348 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001349 return LY_EVALID;
1350 }
1351 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001352 return ret;
1353}
1354
Michal Vaskoea5abea2018-09-18 13:10:54 +02001355/**
1356 * @brief Parse the mandatory statement.
1357 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001358 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001359 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001360 * @param[in,out] flags Flags to add to.
1361 * @param[in,out] exts Extension instances to add to.
1362 *
1363 * @return LY_ERR values.
1364 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001365static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001366parse_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 +02001367{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001368 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001369 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001370 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001371 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001372
1373 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001374 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001375 return LY_EVALID;
1376 }
1377
1378 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001379 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001380
1381 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1382 *flags |= LYS_MAND_TRUE;
1383 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1384 *flags |= LYS_MAND_FALSE;
1385 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001386 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001387 free(buf);
1388 return LY_EVALID;
1389 }
1390 free(buf);
1391
Michal Vaskod989ba02020-08-24 10:59:24 +02001392 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001393 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001394 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001395 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001396 break;
1397 default:
David Sedlákb3077192019-06-19 10:55:37 +02001398 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001399 return LY_EVALID;
1400 }
1401 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001402 return ret;
1403}
1404
Michal Vaskoea5abea2018-09-18 13:10:54 +02001405/**
1406 * @brief Parse a restriction such as range or length.
1407 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001408 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001409 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001410 * @param[in] restr_kw Type of this particular restriction.
1411 * @param[in,out] exts Extension instances to add to.
1412 *
1413 * @return LY_ERR values.
1414 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001416parse_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 +02001417{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001418 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001419 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001420 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001421 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001422
1423 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001424 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001425
Michal Vaskob36053d2020-03-26 15:49:30 +01001426 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001427 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
1428 restr->arg.mod = ctx->main_mod;
Michal Vaskod989ba02020-08-24 10:59:24 +02001429 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001430 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001431 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001432 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 +02001433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001434 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001435 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 +02001436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001437 case LY_STMT_ERROR_APP_TAG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001438 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 +02001439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001440 case LY_STMT_ERROR_MESSAGE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001441 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 +02001442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001443 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001444 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001445 break;
1446 default:
David Sedlákb3077192019-06-19 10:55:37 +02001447 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448 return LY_EVALID;
1449 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001451 return ret;
1452}
1453
Michal Vaskoea5abea2018-09-18 13:10:54 +02001454/**
1455 * @brief Parse a restriction that can have more instances such as must.
1456 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001457 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001458 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001459 * @param[in] restr_kw Type of this particular restriction.
1460 * @param[in,out] restrs Restrictions to add to.
1461 *
1462 * @return LY_ERR values.
1463 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001464static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001465parse_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 +02001466{
1467 struct lysp_restr *restr;
1468
Radek Krejci2c4e7172018-10-19 15:56:26 +02001469 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02001470 return parse_restr(ctx, in, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001471}
1472
Michal Vaskoea5abea2018-09-18 13:10:54 +02001473/**
1474 * @brief Parse the status 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] flags Flags to add to.
1479 * @param[in,out] exts Extension instances to add to.
1480 *
1481 * @return LY_ERR values.
1482 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001484parse_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 +02001485{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001486 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001488 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001489 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001490
1491 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001492 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493 return LY_EVALID;
1494 }
1495
1496 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001497 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498
1499 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1500 *flags |= LYS_STATUS_CURR;
1501 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1502 *flags |= LYS_STATUS_DEPRC;
1503 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1504 *flags |= LYS_STATUS_OBSLT;
1505 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001506 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001507 free(buf);
1508 return LY_EVALID;
1509 }
1510 free(buf);
1511
Michal Vaskod989ba02020-08-24 10:59:24 +02001512 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513 switch (kw) {
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_STATUS, 0, 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), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 return LY_EVALID;
1520 }
1521 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001522 return ret;
1523}
1524
Michal Vaskoea5abea2018-09-18 13:10:54 +02001525/**
1526 * @brief Parse the when 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,out] when_p When pointer to parse to.
1531 *
1532 * @return LY_ERR values.
1533 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001534LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001535parse_when(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001537 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001539 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001540 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001541 struct lysp_when *when;
1542
1543 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001544 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001545 return LY_EVALID;
1546 }
1547
1548 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001549 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001550 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551
1552 /* get value */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001553 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001554 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001555 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556
Michal Vaskod989ba02020-08-24 10:59:24 +02001557 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001558 switch (kw) {
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, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001561 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001562 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001563 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 +02001564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001565 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001566 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 break;
1568 default:
David Sedlákb3077192019-06-19 10:55:37 +02001569 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001570 return LY_EVALID;
1571 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001573 return ret;
1574}
1575
Michal Vaskoea5abea2018-09-18 13:10:54 +02001576/**
1577 * @brief Parse the anydata or anyxml statement.
1578 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001579 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001580 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001581 * @param[in] kw Type of this particular keyword.
1582 * @param[in,out] siblings Siblings to add to.
1583 *
1584 * @return LY_ERR values.
1585 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001586LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001587parse_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 +02001588{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001589 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001590 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001591 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001592 struct lysp_node_anydata *any;
1593
David Sedlák60adc092019-08-06 15:57:02 +02001594 /* create new structure and insert into siblings */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02001595 LY_LIST_NEW_RET(ctx->ctx, siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001596
Radek Krejcid6b76452019-09-03 17:03:03 +02001597 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001598 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001599
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02001601 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001602 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603
1604 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02001605 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001607 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001608 LY_CHECK_RET(parse_config(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001610 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001611 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 +02001612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001613 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001614 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001616 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02001617 LY_CHECK_RET(parse_mandatory(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001619 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02001620 LY_CHECK_RET(parse_restrs(ctx, in, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001622 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001623 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 +02001624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02001626 LY_CHECK_RET(parse_status(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001628 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001629 LY_CHECK_RET(parse_when(ctx, in, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001631 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001632 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 break;
1634 default:
David Sedlákb3077192019-06-19 10:55:37 +02001635 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejci0f969882020-08-21 16:56:47 +02001636 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001637 return LY_EVALID;
1638 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640 return ret;
1641}
1642
Michal Vaskoea5abea2018-09-18 13:10:54 +02001643/**
1644 * @brief Parse the value or position statement. Substatement of type enum statement.
1645 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001646 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001647 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001648 * @param[in] val_kw Type of this particular keyword.
1649 * @param[in,out] value Value to write to.
1650 * @param[in,out] flags Flags to write to.
1651 * @param[in,out] exts Extension instances to add to.
1652 *
1653 * @return LY_ERR values.
1654 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001655LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001656parse_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 +02001657 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001659 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001660 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001661 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001662 long int num = 0;
1663 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001664 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001665
1666 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001667 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001668 return LY_EVALID;
1669 }
1670 *flags |= LYS_SET_VALUE;
1671
1672 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001673 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001674
Radek Krejcid6b76452019-09-03 17:03:03 +02001675 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 +02001676 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001677 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001678 }
1679
1680 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001681 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001682 num = strtol(word, &ptr, 10);
Michal Vasko69730152020-10-09 16:30:07 +02001683 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001684 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001685 goto error;
1686 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687 } else {
1688 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001689 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001690 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001691 goto error;
1692 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693 }
1694 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001695 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001696 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001697 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001698 }
1699 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001700 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001701 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001702 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001703 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001704 *value = num;
1705 } else {
1706 *value = unum;
1707 }
1708 free(buf);
1709
Michal Vaskod989ba02020-08-24 10:59:24 +02001710 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001711 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001712 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001713 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 +02001714 break;
1715 default:
David Sedlákb3077192019-06-19 10:55:37 +02001716 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001717 return LY_EVALID;
1718 }
1719 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001720 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001721
1722error:
1723 free(buf);
1724 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001725}
1726
Michal Vaskoea5abea2018-09-18 13:10:54 +02001727/**
1728 * @brief Parse the enum or bit statement. Substatement of type statement.
1729 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001730 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001731 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001732 * @param[in] enum_kw Type of this particular keyword.
1733 * @param[in,out] enums Enums or bits to add to.
1734 *
1735 * @return LY_ERR values.
1736 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001738parse_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 +02001739{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001740 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001741 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001742 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001743 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001744 struct lysp_type_enum *enm;
1745
Radek Krejci2c4e7172018-10-19 15:56:26 +02001746 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001747
1748 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001749 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 +02001750 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001751 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001752 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001753 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001754
Radek Krejci011e4aa2020-09-04 15:22:31 +02001755 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001756 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1757
Michal Vaskod989ba02020-08-24 10:59:24 +02001758 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001760 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001761 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 +02001762 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001763 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001764 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001765 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001767 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001768 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 +02001769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001770 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02001771 LY_CHECK_RET(parse_status(ctx, in, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001773 case LY_STMT_VALUE:
1774 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Michal Vasko69730152020-10-09 16:30:07 +02001775 ly_stmt2str(enum_kw)), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +02001776 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 +02001777 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001778 case LY_STMT_POSITION:
1779 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Michal Vasko69730152020-10-09 16:30:07 +02001780 ly_stmt2str(enum_kw)), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +02001781 LY_CHECK_RET(parse_type_enum_value_pos(ctx, in, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001782 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001783 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001784 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001785 break;
1786 default:
David Sedlákb3077192019-06-19 10:55:37 +02001787 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001788 return LY_EVALID;
1789 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001790 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001791 return ret;
1792}
1793
Michal Vaskoea5abea2018-09-18 13:10:54 +02001794/**
1795 * @brief Parse the fraction-digits statement. Substatement of type statement.
1796 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001797 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001798 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001799 * @param[in,out] fracdig Value to write to.
1800 * @param[in,out] exts Extension instances to add to.
1801 *
1802 * @return LY_ERR values.
1803 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001804static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001805parse_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 +02001806{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001807 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001808 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001809 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001810 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001811 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001812
1813 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001814 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815 return LY_EVALID;
1816 }
1817
1818 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001819 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001820
1821 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001822 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001823 free(buf);
1824 return LY_EVALID;
1825 }
1826
1827 errno = 0;
1828 num = strtoul(word, &ptr, 10);
1829 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001830 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001831 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001832 free(buf);
1833 return LY_EVALID;
1834 }
1835 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001836 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001837 free(buf);
1838 return LY_EVALID;
1839 }
1840 *fracdig = num;
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_FRACDIGITS, 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), "fraction-digits");
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 require-instance statement. Substatement of type 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] reqinst Value to write to.
1862 * @param[in,out] flags Flags to write to.
1863 * @param[in,out] exts Extension instances to add to.
1864 *
1865 * @return LY_ERR values.
1866 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001868parse_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 +02001869 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001870{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001871 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001872 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001873 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001874 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001875
1876 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001877 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001878 return LY_EVALID;
1879 }
1880 *flags |= LYS_SET_REQINST;
1881
1882 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001883 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001884
1885 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1886 *reqinst = 1;
1887 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001888 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001889 free(buf);
1890 return LY_EVALID;
1891 }
1892 free(buf);
1893
Michal Vaskod989ba02020-08-24 10:59:24 +02001894 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001895 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001896 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001897 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001898 break;
1899 default:
David Sedlákb3077192019-06-19 10:55:37 +02001900 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001901 return LY_EVALID;
1902 }
1903 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904 return ret;
1905}
1906
Michal Vaskoea5abea2018-09-18 13:10:54 +02001907/**
1908 * @brief Parse the modifier statement. Substatement of type pattern statement.
1909 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001910 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001911 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001912 * @param[in,out] pat Value to write to.
1913 * @param[in,out] exts Extension instances to add to.
1914 *
1915 * @return LY_ERR values.
1916 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001917static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001918parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **pat,
Radek Krejci0f969882020-08-21 16:56:47 +02001919 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001920{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001921 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001923 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001924 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925
1926 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001927 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928 return LY_EVALID;
1929 }
1930
1931 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001932 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001933
1934 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001935 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001936 free(buf);
1937 return LY_EVALID;
1938 }
1939 free(buf);
1940
1941 /* replace the value in the dictionary */
1942 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001943 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001944 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001945 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946
1947 assert(buf[0] == 0x06);
1948 buf[0] = 0x15;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001949 LY_CHECK_RET(lydict_insert_zc(ctx->ctx, buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950
Michal Vaskod989ba02020-08-24 10:59:24 +02001951 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001952 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001953 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001954 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001955 break;
1956 default:
David Sedlákb3077192019-06-19 10:55:37 +02001957 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001958 return LY_EVALID;
1959 }
1960 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001961 return ret;
1962}
1963
Michal Vaskoea5abea2018-09-18 13:10:54 +02001964/**
1965 * @brief Parse the pattern statement. Substatement of type statement.
1966 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001967 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001968 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001969 * @param[in,out] patterns Restrictions to add to.
1970 *
1971 * @return LY_ERR values.
1972 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001974parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001976 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001978 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001979 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001980 struct lysp_restr *restr;
1981
Radek Krejci2c4e7172018-10-19 15:56:26 +02001982 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001983
1984 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001985 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001986
1987 /* add special meaning first byte */
1988 if (buf) {
1989 buf = realloc(buf, word_len + 2);
1990 word = buf;
1991 } else {
1992 buf = malloc(word_len + 2);
1993 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001994 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001995 memmove(buf + 1, word, word_len);
1996 buf[0] = 0x06; /* pattern's default regular-match flag */
1997 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko7f45cf22020-10-01 12:49:44 +02001998 LY_CHECK_RET(lydict_insert_zc(ctx->ctx, buf, &restr->arg.str));
1999 restr->arg.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002000
Michal Vaskod989ba02020-08-24 10:59:24 +02002001 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002002 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002003 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002004 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 +02002005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002006 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002007 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 +02002008 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002009 case LY_STMT_ERROR_APP_TAG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002010 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 +02002011 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002012 case LY_STMT_ERROR_MESSAGE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002013 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 +02002014 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002015 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002016 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002017 LY_CHECK_RET(parse_type_pattern_modifier(ctx, in, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002019 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002020 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002021 break;
2022 default:
David Sedlákb3077192019-06-19 10:55:37 +02002023 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002024 return LY_EVALID;
2025 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027 return ret;
2028}
2029
Michal Vaskoea5abea2018-09-18 13:10:54 +02002030/**
2031 * @brief Parse the type statement.
2032 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002033 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002034 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002035 * @param[in,out] type Type to wrote to.
2036 *
2037 * @return LY_ERR values.
2038 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002040parse_type(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002042 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002044 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002045 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002046 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047 struct lysp_type *nest_type;
2048
2049 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002050 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002051 return LY_EVALID;
2052 }
2053
2054 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002055 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002056 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002057
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002058 /* set module */
2059 type->mod = ctx->main_mod;
2060
Michal Vaskod989ba02020-08-24 10:59:24 +02002061 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002063 case LY_STMT_BASE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002064 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 +01002065 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002067 case LY_STMT_BIT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002068 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002069 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002071 case LY_STMT_ENUM:
Michal Vasko63f3d842020-07-08 10:10:14 +02002072 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002073 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002075 case LY_STMT_FRACTION_DIGITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002076 LY_CHECK_RET(parse_type_fracdigits(ctx, in, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002077 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002079 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002081 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 return LY_EVALID;
2083 }
2084 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002085 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086
Michal Vasko63f3d842020-07-08 10:10:14 +02002087 LY_CHECK_RET(parse_restr(ctx, in, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002088 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002090 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002091 if (type->path) {
2092 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2093 return LY_EVALID;
2094 }
2095
Michal Vasko63f3d842020-07-08 10:10:14 +02002096 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 +02002097 ret = ly_path_parse(ctx->ctx, NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
Michal Vasko69730152020-10-09 16:30:07 +02002098 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko004d3152020-06-11 19:59:22 +02002099 lydict_remove(ctx->ctx, str_path);
2100 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002101 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002103 case LY_STMT_PATTERN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002104 LY_CHECK_RET(parse_type_pattern(ctx, in, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002105 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002107 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002108 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002109 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002110 return LY_EVALID;
2111 }
2112 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002113 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114
Michal Vasko63f3d842020-07-08 10:10:14 +02002115 LY_CHECK_RET(parse_restr(ctx, in, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002116 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002118 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002119 LY_CHECK_RET(parse_type_reqinstance(ctx, in, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002120 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002122 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002123 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02002124 LY_CHECK_RET(parse_type(ctx, in, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002125 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002127 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002128 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 break;
2130 default:
David Sedlákb3077192019-06-19 10:55:37 +02002131 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132 return LY_EVALID;
2133 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 return ret;
2136}
2137
Michal Vaskoea5abea2018-09-18 13:10:54 +02002138/**
2139 * @brief Parse the leaf statement.
2140 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002141 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002142 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002143 * @param[in,out] siblings Siblings to add to.
2144 *
2145 * @return LY_ERR values.
2146 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002147LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002148parse_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 +02002149{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002150 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002152 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002153 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154 struct lysp_node_leaf *leaf;
2155
David Sedlák60adc092019-08-06 15:57:02 +02002156 /* create new leaf structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002157 LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002158 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002159 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002160
Michal Vasko7fbc8162018-09-17 10:35:16 +02002161 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002162 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002163 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002164
2165 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002166 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002167 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002168 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002169 LY_CHECK_RET(parse_config(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002171 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002172 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
2173 leaf->dflt.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002175 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002176 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 +02002177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002178 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002179 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002181 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002182 LY_CHECK_RET(parse_mandatory(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002184 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002185 LY_CHECK_RET(parse_restrs(ctx, in, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002187 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002188 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 +02002189 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002190 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002191 LY_CHECK_RET(parse_status(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002193 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002194 LY_CHECK_RET(parse_type(ctx, in, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002196 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002197 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 +02002198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002199 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002200 LY_CHECK_RET(parse_when(ctx, in, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002202 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002203 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002204 break;
2205 default:
David Sedlákb3077192019-06-19 10:55:37 +02002206 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002207 return LY_EVALID;
2208 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002210 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002211checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 /* mandatory substatements */
2213 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002214 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002215 return LY_EVALID;
2216 }
2217
2218 return ret;
2219}
2220
Michal Vaskoea5abea2018-09-18 13:10:54 +02002221/**
2222 * @brief Parse the max-elements statement.
2223 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002224 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002225 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002226 * @param[in,out] max Value to write to.
2227 * @param[in,out] flags Flags to write to.
2228 * @param[in,out] exts Extension instances to add to.
2229 *
2230 * @return LY_ERR values.
2231 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002232LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002233parse_maxelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *max, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002234 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002236 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002238 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002239 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002240 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241
2242 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002243 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002244 return LY_EVALID;
2245 }
2246 *flags |= LYS_SET_MAX;
2247
2248 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002249 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250
2251 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002252 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253 free(buf);
2254 return LY_EVALID;
2255 }
2256
Radek Krejci7f9b6512019-09-18 13:11:09 +02002257 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258 errno = 0;
2259 num = strtoul(word, &ptr, 10);
2260 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002261 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002262 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 free(buf);
2264 return LY_EVALID;
2265 }
2266 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002267 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002268 free(buf);
2269 return LY_EVALID;
2270 }
2271
2272 *max = num;
2273 }
2274 free(buf);
2275
Michal Vaskod989ba02020-08-24 10:59:24 +02002276 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002278 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002279 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002280 break;
2281 default:
David Sedlákb3077192019-06-19 10:55:37 +02002282 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 return LY_EVALID;
2284 }
2285 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 return ret;
2287}
2288
Michal Vaskoea5abea2018-09-18 13:10:54 +02002289/**
2290 * @brief Parse the min-elements statement.
2291 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002292 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002293 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002294 * @param[in,out] min Value to write to.
2295 * @param[in,out] flags Flags to write to.
2296 * @param[in,out] exts Extension instances to add to.
2297 *
2298 * @return LY_ERR values.
2299 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002300LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002301parse_minelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *min, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002302 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002303{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002304 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002305 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002306 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002308 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309
2310 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002311 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312 return LY_EVALID;
2313 }
2314 *flags |= LYS_SET_MIN;
2315
2316 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002317 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002318
2319 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002320 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002321 free(buf);
2322 return LY_EVALID;
2323 }
2324
2325 errno = 0;
2326 num = strtoul(word, &ptr, 10);
2327 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002328 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002329 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 free(buf);
2331 return LY_EVALID;
2332 }
2333 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002334 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002335 free(buf);
2336 return LY_EVALID;
2337 }
2338 *min = num;
2339 free(buf);
2340
Michal Vaskod989ba02020-08-24 10:59:24 +02002341 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002342 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002343 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002344 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002345 break;
2346 default:
David Sedlákb3077192019-06-19 10:55:37 +02002347 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348 return LY_EVALID;
2349 }
2350 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002351 return ret;
2352}
2353
Michal Vaskoea5abea2018-09-18 13:10:54 +02002354/**
2355 * @brief Parse the ordered-by statement.
2356 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002357 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002358 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002359 * @param[in,out] flags Flags to write to.
2360 * @param[in,out] exts Extension instances to add to.
2361 *
2362 * @return LY_ERR values.
2363 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002365parse_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 +02002366{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002367 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002369 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002370 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002371
2372 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002373 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002374 return LY_EVALID;
2375 }
2376
2377 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002378 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002379
2380 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2381 *flags |= LYS_ORDBY_SYSTEM;
2382 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2383 *flags |= LYS_ORDBY_USER;
2384 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002385 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002386 free(buf);
2387 return LY_EVALID;
2388 }
2389 free(buf);
2390
Michal Vaskod989ba02020-08-24 10:59:24 +02002391 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002392 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002393 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002394 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002395 break;
2396 default:
David Sedlákb3077192019-06-19 10:55:37 +02002397 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002398 return LY_EVALID;
2399 }
2400 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002401 return ret;
2402}
2403
Michal Vaskoea5abea2018-09-18 13:10:54 +02002404/**
2405 * @brief Parse the leaf-list statement.
2406 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002407 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002408 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002409 * @param[in,out] siblings Siblings to add to.
2410 *
2411 * @return LY_ERR values.
2412 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002413LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002414parse_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 +02002415{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002416 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002417 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002418 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002419 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420 struct lysp_node_leaflist *llist;
2421
David Sedlák60adc092019-08-06 15:57:02 +02002422 /* create new leaf-list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002423 LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002424 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002425 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002428 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002429 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002430
2431 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002432 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002433 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002434 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002435 LY_CHECK_RET(parse_config(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002437 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002438 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002439 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002441 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002442 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 +02002443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002444 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002445 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002447 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002448 LY_CHECK_RET(parse_maxelements(ctx, in, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002450 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002451 LY_CHECK_RET(parse_minelements(ctx, in, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002453 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002454 LY_CHECK_RET(parse_restrs(ctx, in, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002456 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002457 LY_CHECK_RET(parse_orderedby(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002459 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002460 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 +02002461 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002462 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002463 LY_CHECK_RET(parse_status(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002464 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002465 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002466 LY_CHECK_RET(parse_type(ctx, in, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002467 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002468 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002469 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 +02002470 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002471 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002472 LY_CHECK_RET(parse_when(ctx, in, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002474 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002475 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002476 break;
2477 default:
David Sedlákb3077192019-06-19 10:55:37 +02002478 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479 return LY_EVALID;
2480 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002481 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002482 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002483checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002484 /* mandatory substatements */
2485 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002486 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 return LY_EVALID;
2488 }
2489
2490 return ret;
2491}
2492
Michal Vaskoea5abea2018-09-18 13:10:54 +02002493/**
2494 * @brief Parse the refine statement.
2495 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002496 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002497 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002498 * @param[in,out] refines Refines to add to.
2499 *
2500 * @return LY_ERR values.
2501 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002503parse_refine(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002505 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002506 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002507 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002508 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002509 struct lysp_refine *rf;
2510
Radek Krejci2c4e7172018-10-19 15:56:26 +02002511 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002512
2513 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002514 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002515 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002516 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517
Michal Vaskod989ba02020-08-24 10:59:24 +02002518 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002520 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002521 LY_CHECK_RET(parse_config(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002523 case LY_STMT_DEFAULT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002524 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 +02002525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002526 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002527 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 +02002528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002529 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002530 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002531 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002532 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002533 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002534 LY_CHECK_RET(parse_maxelements(ctx, in, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002535 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002536 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002537 LY_CHECK_RET(parse_minelements(ctx, in, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002539 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002540 LY_CHECK_RET(parse_restrs(ctx, in, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002542 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002543 LY_CHECK_RET(parse_mandatory(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002545 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002546 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 +02002547 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002548 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002549 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 +02002550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002551 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002552 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002553 break;
2554 default:
David Sedlákb3077192019-06-19 10:55:37 +02002555 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556 return LY_EVALID;
2557 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002559 return ret;
2560}
2561
Michal Vaskoea5abea2018-09-18 13:10:54 +02002562/**
2563 * @brief Parse the typedef statement.
2564 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002565 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002566 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002567 * @param[in,out] typedefs Typedefs to add to.
2568 *
2569 * @return LY_ERR values.
2570 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002572parse_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 +02002573{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002574 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002576 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002577 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578 struct lysp_tpdf *tpdf;
2579
Radek Krejci2c4e7172018-10-19 15:56:26 +02002580 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581
2582 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002583 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002584 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002585
2586 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002587 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002588 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002589 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002590 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
2591 tpdf->dflt.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002593 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002594 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 +02002595 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002596 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002597 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 +02002598 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002599 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002600 LY_CHECK_RET(parse_status(ctx, in, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002601 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002602 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002603 LY_CHECK_RET(parse_type(ctx, in, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002604 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002605 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002606 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 +02002607 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002608 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002609 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610 break;
2611 default:
David Sedlákb3077192019-06-19 10:55:37 +02002612 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002613 return LY_EVALID;
2614 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002616 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002617checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 /* mandatory substatements */
2619 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002620 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 return LY_EVALID;
2622 }
2623
Radek Krejcibbe09a92018-11-08 09:36:54 +01002624 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002625 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002626 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002627 }
2628
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 return ret;
2630}
2631
Michal Vaskoea5abea2018-09-18 13:10:54 +02002632/**
2633 * @brief Parse the input or output statement.
2634 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002635 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002636 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002637 * @param[in] kw Type of this particular keyword
2638 * @param[in,out] inout_p Input/output pointer to write to.
2639 *
2640 * @return LY_ERR values.
2641 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02002643parse_inout(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt inout_kw, struct lysp_node *parent,
2644 struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002646 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002648 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002649 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002651 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002652 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653 return LY_EVALID;
2654 }
2655
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002656 /* initiate structure */
Michal Vasko22df3f02020-08-24 13:29:22 +02002657 inout_p->nodetype = &((struct lysp_action *)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002658 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002659
2660 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002661 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002663 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002664 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002665 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002666 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002667 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002668 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002669 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002670 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002672 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002673 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002674 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002675 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002676 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002677 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002678 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002679 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002681 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002682 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002683 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002684 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002685 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002686 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002687 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002688 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, in, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002690 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002691 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Michal Vasko63f3d842020-07-08 10:10:14 +02002692 LY_CHECK_RET(parse_restrs(ctx, in, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002693 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002694 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002695 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002696 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002697 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002698 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002699 break;
2700 default:
David Sedlákb3077192019-06-19 10:55:37 +02002701 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702 return LY_EVALID;
2703 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002705 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002706
Radek Krejci7fc68292019-06-12 13:51:09 +02002707checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002708 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002709 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 +02002710
Michal Vaskob83af8a2020-01-06 09:49:22 +01002711 if (!inout_p->data) {
2712 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2713 return LY_EVALID;
2714 }
2715
Michal Vasko7fbc8162018-09-17 10:35:16 +02002716 return ret;
2717}
2718
Michal Vaskoea5abea2018-09-18 13:10:54 +02002719/**
2720 * @brief Parse the action statement.
2721 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002722 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002723 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002724 * @param[in,out] actions Actions to add to.
2725 *
2726 * @return LY_ERR values.
2727 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002728LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002729parse_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 +02002730{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002731 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002733 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002734 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 struct lysp_action *act;
2736
Radek Krejci2c4e7172018-10-19 15:56:26 +02002737 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738
2739 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002740 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002741 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002742 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002743 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002744
Michal Vasko63f3d842020-07-08 10:10:14 +02002745 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002746 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002747 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002748 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 +02002749 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002750 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002751 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002753 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002754 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 +02002755 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002756 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002757 LY_CHECK_RET(parse_status(ctx, in, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 break;
2759
Radek Krejcid6b76452019-09-03 17:03:03 +02002760 case LY_STMT_INPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002761 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002762 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002763 case LY_STMT_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002764 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765 break;
2766
Radek Krejcid6b76452019-09-03 17:03:03 +02002767 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002768 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, in, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002770 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002771 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002773 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002774 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 break;
2776 default:
David Sedlákb3077192019-06-19 10:55:37 +02002777 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 return LY_EVALID;
2779 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002780 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002781 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002782
2783 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2784 if (!act->input.nodetype) {
2785 act->input.nodetype = LYS_INPUT;
2786 act->input.parent = (struct lysp_node *)act;
2787 }
2788 if (!act->output.nodetype) {
2789 act->output.nodetype = LYS_OUTPUT;
2790 act->output.parent = (struct lysp_node *)act;
2791 }
2792
Radek Krejci7fc68292019-06-12 13:51:09 +02002793checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002794 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002795 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002796
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797 return ret;
2798}
2799
Michal Vaskoea5abea2018-09-18 13:10:54 +02002800/**
2801 * @brief Parse the notification statement.
2802 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002803 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002804 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002805 * @param[in,out] notifs Notifications to add to.
2806 *
2807 * @return LY_ERR values.
2808 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002809LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002810parse_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 +02002811{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002812 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002813 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002814 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002815 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002816 struct lysp_notif *notif;
2817
Radek Krejci2c4e7172018-10-19 15:56:26 +02002818 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002819
2820 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002821 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002822 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002823 notif->nodetype = LYS_NOTIF;
2824 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002825
Michal Vasko63f3d842020-07-08 10:10:14 +02002826 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002828 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002829 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 +02002830 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002831 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002832 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002833 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002834 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002835 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 +02002836 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002837 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002838 LY_CHECK_RET(parse_status(ctx, in, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839 break;
2840
Radek Krejcid6b76452019-09-03 17:03:03 +02002841 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002842 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002843 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002844 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002845 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002846 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002847 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002848 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002849 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002850 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002851 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002852 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002853 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002854 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002856 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002857 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002859 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002860 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002862 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002863 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 break;
2865
Radek Krejcid6b76452019-09-03 17:03:03 +02002866 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002867 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Michal Vasko63f3d842020-07-08 10:10:14 +02002868 LY_CHECK_RET(parse_restrs(ctx, in, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002870 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002871 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, in, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002872 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002873 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002874 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002875 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002876 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002877 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002878 break;
2879 default:
David Sedlákb3077192019-06-19 10:55:37 +02002880 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881 return LY_EVALID;
2882 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002884 LY_CHECK_RET(ret);
2885checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002886 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002887 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002888
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889 return ret;
2890}
2891
Michal Vaskoea5abea2018-09-18 13:10:54 +02002892/**
2893 * @brief Parse the grouping statement.
2894 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002895 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002896 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002897 * @param[in,out] groupings Groupings to add to.
2898 *
2899 * @return LY_ERR values.
2900 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002901LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002902parse_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 +02002903{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002904 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002906 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002907 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002908 struct lysp_grp *grp;
2909
Radek Krejci2c4e7172018-10-19 15:56:26 +02002910 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002911
2912 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002913 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002914 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002915 grp->nodetype = LYS_GROUPING;
2916 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917
Michal Vasko63f3d842020-07-08 10:10:14 +02002918 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002920 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002921 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 +02002922 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002923 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002924 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 +02002925 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002926 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002927 LY_CHECK_RET(parse_status(ctx, in, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 break;
2929
Radek Krejcid6b76452019-09-03 17:03:03 +02002930 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002931 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002932 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002933 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002934 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002936 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002937 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002938 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002939 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002940 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002942 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002943 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002945 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002946 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002947 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002948 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002949 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002950 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002951 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002952 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002953 break;
2954
Radek Krejcid6b76452019-09-03 17:03:03 +02002955 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002956 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)grp, in, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002958 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002959 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Michal Vasko22df3f02020-08-24 13:29:22 +02002960 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002962 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002963 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002965 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002966 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Michal Vasko22df3f02020-08-24 13:29:22 +02002967 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002969 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002970 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002971 break;
2972 default:
David Sedlákb3077192019-06-19 10:55:37 +02002973 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974 return LY_EVALID;
2975 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002977 LY_CHECK_RET(ret);
2978checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002979 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002980 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 +02002981
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982 return ret;
2983}
2984
Michal Vaskoea5abea2018-09-18 13:10:54 +02002985/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002986 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002987 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002988 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002989 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002990 * @param[in,out] augments Augments to add to.
2991 *
2992 * @return LY_ERR values.
2993 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002994LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002995parse_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 +02002996{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002997 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002999 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003000 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 struct lysp_augment *aug;
3002
Radek Krejci2c4e7172018-10-19 15:56:26 +02003003 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003004
3005 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003006 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003007 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003008 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003009 aug->nodetype = LYS_AUGMENT;
3010 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003011
Michal Vasko63f3d842020-07-08 10:10:14 +02003012 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003014 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003015 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 +02003016 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003017 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003018 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003020 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003021 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 +02003022 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003023 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003024 LY_CHECK_RET(parse_status(ctx, in, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003025 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003026 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003027 LY_CHECK_RET(parse_when(ctx, in, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028 break;
3029
Radek Krejcid6b76452019-09-03 17:03:03 +02003030 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003031 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003032 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003033 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003034 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003036 case LY_STMT_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003037 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003038 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003039 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003040 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003041 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003042 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003043 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003044 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003045 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003046 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003048 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003049 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003051 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003052 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003054 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003055 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056 break;
3057
Radek Krejcid6b76452019-09-03 17:03:03 +02003058 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003059 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Michal Vasko22df3f02020-08-24 13:29:22 +02003060 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003062 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003063 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Michal Vasko22df3f02020-08-24 13:29:22 +02003064 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003066 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003067 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003068 break;
3069 default:
David Sedlákb3077192019-06-19 10:55:37 +02003070 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071 return LY_EVALID;
3072 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003074 LY_CHECK_RET(ret);
3075checks:
3076 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003077 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 +02003078
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 return ret;
3080}
3081
Michal Vaskoea5abea2018-09-18 13:10:54 +02003082/**
3083 * @brief Parse the uses statement.
3084 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003085 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003086 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003087 * @param[in,out] siblings Siblings to add to.
3088 *
3089 * @return LY_ERR values.
3090 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003091LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003092parse_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 +02003093{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003094 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003096 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003097 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 struct lysp_node_uses *uses;
3099
David Sedlák60adc092019-08-06 15:57:02 +02003100 /* create uses structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003101 LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003103 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003106 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003107 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108
3109 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003110 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003111 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003112 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003113 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 +02003114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003115 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003116 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003118 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003119 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 +02003120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003121 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003122 LY_CHECK_RET(parse_status(ctx, in, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003124 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003125 LY_CHECK_RET(parse_when(ctx, in, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 break;
3127
Radek Krejcid6b76452019-09-03 17:03:03 +02003128 case LY_STMT_REFINE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003129 LY_CHECK_RET(parse_refine(ctx, in, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003131 case LY_STMT_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02003132 LY_CHECK_RET(parse_augment(ctx, in, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003134 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003135 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003136 break;
3137 default:
David Sedlákb3077192019-06-19 10:55:37 +02003138 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 return LY_EVALID;
3140 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003142checks:
3143 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003144 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003145
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146 return ret;
3147}
3148
Michal Vaskoea5abea2018-09-18 13:10:54 +02003149/**
3150 * @brief Parse the case statement.
3151 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003152 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003153 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003154 * @param[in,out] siblings Siblings to add to.
3155 *
3156 * @return LY_ERR values.
3157 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003158LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003159parse_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 +02003160{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003161 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003163 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003164 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 struct lysp_node_case *cas;
3166
David Sedlák60adc092019-08-06 15:57:02 +02003167 /* create new case structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003168 LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003169 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003170 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003173 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003174 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003175
3176 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02003177 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003178 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003179 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003180 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 +02003181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003182 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003183 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003185 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003186 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 +02003187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003188 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003189 LY_CHECK_RET(parse_status(ctx, in, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003191 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003192 LY_CHECK_RET(parse_when(ctx, in, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003193 break;
3194
Radek Krejcid6b76452019-09-03 17:03:03 +02003195 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003196 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003197 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003198 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003199 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003200 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003201 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003202 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003204 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003205 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003206 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003207 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003208 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003209 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003210 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003211 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003213 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003214 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003216 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003217 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003219 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003220 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221 break;
3222 default:
David Sedlákb3077192019-06-19 10:55:37 +02003223 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003224 return LY_EVALID;
3225 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003226 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 return ret;
3228}
3229
Michal Vaskoea5abea2018-09-18 13:10:54 +02003230/**
3231 * @brief Parse the choice statement.
3232 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003233 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003234 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003235 * @param[in,out] siblings Siblings to add to.
3236 *
3237 * @return LY_ERR values.
3238 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003239LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003240parse_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 +02003241{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003242 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003244 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003245 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003246 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247
David Sedlák60adc092019-08-06 15:57:02 +02003248 /* create new choice structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003249 LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003250 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003251 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003254 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003255 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003256
3257 /* parse substatements */
Michal Vasko7f45cf22020-10-01 12:49:44 +02003258 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003259 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003260 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003261 LY_CHECK_RET(parse_config(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003263 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003264 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 +02003265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003266 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003267 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003269 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003270 LY_CHECK_RET(parse_mandatory(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003272 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003273 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 +02003274 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003275 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003276 LY_CHECK_RET(parse_status(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003278 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003279 LY_CHECK_RET(parse_when(ctx, in, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003281 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003282 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
3283 &choice->exts));
3284 choice->dflt.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285 break;
3286
Radek Krejcid6b76452019-09-03 17:03:03 +02003287 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003288 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003289 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003290 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003291 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003293 case LY_STMT_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003294 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003296 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003297 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Michal Vasko22df3f02020-08-24 13:29:22 +02003298 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003299 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003300 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003301 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003302 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003303 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003304 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003306 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003307 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003309 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003310 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003312 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003313 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 break;
3315 default:
David Sedlákb3077192019-06-19 10:55:37 +02003316 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317 return LY_EVALID;
3318 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 return ret;
3321}
3322
Michal Vaskoea5abea2018-09-18 13:10:54 +02003323/**
3324 * @brief Parse the container statement.
3325 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003326 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003327 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003328 * @param[in,out] siblings Siblings to add to.
3329 *
3330 * @return LY_ERR values.
3331 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003332LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003333parse_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 +02003334{
3335 LY_ERR ret = 0;
3336 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003337 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003338 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339 struct lysp_node_container *cont;
3340
David Sedlák60adc092019-08-06 15:57:02 +02003341 /* create new container structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003342 LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003344 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003347 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003348 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349
3350 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003351 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003353 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003354 LY_CHECK_RET(parse_config(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003356 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003357 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 +02003358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003359 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003360 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003362 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003363 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 +02003364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003365 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003366 LY_CHECK_RET(parse_status(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003368 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003369 LY_CHECK_RET(parse_when(ctx, in, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003371 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003372 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 +02003373 break;
3374
Radek Krejcid6b76452019-09-03 17:03:03 +02003375 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003376 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003377 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003378 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003379 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003381 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003382 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003384 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003385 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003387 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003388 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003390 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003391 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003393 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003394 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003396 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003397 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 break;
3399
Radek Krejcid6b76452019-09-03 17:03:03 +02003400 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003401 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, in, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003403 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003404 LY_CHECK_RET(parse_restrs(ctx, in, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003406 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003407 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Michal Vasko22df3f02020-08-24 13:29:22 +02003408 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003410 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02003411 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003414 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Michal Vasko22df3f02020-08-24 13:29:22 +02003415 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003417 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003418 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003419 break;
3420 default:
David Sedlákb3077192019-06-19 10:55:37 +02003421 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003422 return LY_EVALID;
3423 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003425checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003426 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003427 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 +02003428 return ret;
3429}
3430
Michal Vaskoea5abea2018-09-18 13:10:54 +02003431/**
3432 * @brief Parse the list statement.
3433 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003434 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003435 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003436 * @param[in,out] siblings Siblings to add to.
3437 *
3438 * @return LY_ERR values.
3439 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003440LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003441parse_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 +02003442{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003443 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003445 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003446 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447 struct lysp_node_list *list;
3448
David Sedlák60adc092019-08-06 15:57:02 +02003449 /* create new list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003450 LY_LIST_NEW_RET(ctx->ctx, siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003451 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003452 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003453
Michal Vasko7fbc8162018-09-17 10:35:16 +02003454 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003455 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003456 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003457
3458 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003459 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003460 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003461 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003462 LY_CHECK_RET(parse_config(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003464 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003465 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 +02003466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003467 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003468 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003469 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003470 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003471 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 +02003472 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003473 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003474 LY_CHECK_RET(parse_status(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003476 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003477 LY_CHECK_RET(parse_when(ctx, in, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003479 case LY_STMT_KEY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003480 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 +02003481 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003482 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003483 LY_CHECK_RET(parse_maxelements(ctx, in, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003485 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003486 LY_CHECK_RET(parse_minelements(ctx, in, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003488 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003489 LY_CHECK_RET(parse_orderedby(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003491 case LY_STMT_UNIQUE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003492 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 break;
3494
Radek Krejcid6b76452019-09-03 17:03:03 +02003495 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003496 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003497 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003498 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003499 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003501 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003502 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003504 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003505 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003507 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003508 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003510 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003511 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003513 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003514 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003515 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003516 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003517 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 break;
3519
Radek Krejcid6b76452019-09-03 17:03:03 +02003520 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003521 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, in, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003523 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003524 LY_CHECK_RET(parse_restrs(ctx, in, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003527 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Michal Vasko22df3f02020-08-24 13:29:22 +02003528 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003530 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02003531 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003533 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003534 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Michal Vasko22df3f02020-08-24 13:29:22 +02003535 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003536 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003537 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003538 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539 break;
3540 default:
David Sedlákb3077192019-06-19 10:55:37 +02003541 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 return LY_EVALID;
3543 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003545 LY_CHECK_RET(ret);
3546checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003547 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003548 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 +02003549
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 return ret;
3551}
3552
Michal Vaskoea5abea2018-09-18 13:10:54 +02003553/**
3554 * @brief Parse the yin-element statement.
3555 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003556 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003557 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003558 * @param[in,out] flags Flags to write to.
3559 * @param[in,out] exts Extension instances to add to.
3560 *
3561 * @return LY_ERR values.
3562 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003564parse_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 +02003565{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003566 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003568 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003569 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570
3571 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003572 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573 return LY_EVALID;
3574 }
3575
3576 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003577 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578
3579 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3580 *flags |= LYS_YINELEM_TRUE;
3581 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3582 *flags |= LYS_YINELEM_FALSE;
3583 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003584 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003585 free(buf);
3586 return LY_EVALID;
3587 }
3588 free(buf);
3589
Michal Vaskod989ba02020-08-24 10:59:24 +02003590 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003592 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003593 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003594 LY_CHECK_RET(ret);
3595 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596 default:
David Sedlákb3077192019-06-19 10:55:37 +02003597 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 return LY_EVALID;
3599 }
3600 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601 return ret;
3602}
3603
Michal Vaskoea5abea2018-09-18 13:10:54 +02003604/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003605 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003606 *
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] argument Value to write to.
3610 * @param[in,out] flags Flags to write to.
3611 * @param[in,out] exts Extension instances to add to.
3612 *
3613 * @return LY_ERR values.
3614 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003616parse_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 +02003617{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003618 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003619 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003620 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003621 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622
3623 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003624 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 return LY_EVALID;
3626 }
3627
3628 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003629 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003630 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631
Michal Vaskod989ba02020-08-24 10:59:24 +02003632 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003634 case LY_STMT_YIN_ELEMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003635 LY_CHECK_RET(parse_yinelement(ctx, in, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003637 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003638 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639 break;
3640 default:
David Sedlákb3077192019-06-19 10:55:37 +02003641 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003642 return LY_EVALID;
3643 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003645 return ret;
3646}
3647
Michal Vaskoea5abea2018-09-18 13:10:54 +02003648/**
3649 * @brief Parse the extension statement.
3650 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003651 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003652 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003653 * @param[in,out] extensions Extensions to add to.
3654 *
3655 * @return LY_ERR values.
3656 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003658parse_extension(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003659{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003660 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003661 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003662 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003663 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664 struct lysp_ext *ex;
3665
Radek Krejci2c4e7172018-10-19 15:56:26 +02003666 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667
3668 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003669 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003670 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671
Michal Vaskod989ba02020-08-24 10:59:24 +02003672 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003674 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003675 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 +02003676 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003677 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003678 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 +02003679 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003680 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003681 LY_CHECK_RET(parse_status(ctx, in, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003683 case LY_STMT_ARGUMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003684 LY_CHECK_RET(parse_argument(ctx, in, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003686 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003687 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688 break;
3689 default:
David Sedlákb3077192019-06-19 10:55:37 +02003690 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003691 return LY_EVALID;
3692 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003693 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 return ret;
3695}
3696
Michal Vaskoea5abea2018-09-18 13:10:54 +02003697/**
3698 * @brief Parse the deviate statement.
3699 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003700 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003701 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003702 * @param[in,out] deviates Deviates to add to.
3703 *
3704 * @return LY_ERR values.
3705 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003706LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003707parse_deviate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003708{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003709 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003710 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003711 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003712 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003713 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714 struct lysp_deviate_add *d_add = NULL;
3715 struct lysp_deviate_rpl *d_rpl = NULL;
3716 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003717 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3718 struct lysp_restr **d_musts = NULL;
3719 uint16_t *d_flags = 0;
3720 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003721
3722 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003723 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724
3725 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3726 dev_mod = LYS_DEV_NOT_SUPPORTED;
3727 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3728 dev_mod = LYS_DEV_ADD;
3729 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3730 dev_mod = LYS_DEV_REPLACE;
3731 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3732 dev_mod = LYS_DEV_DELETE;
3733 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003734 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 free(buf);
3736 return LY_EVALID;
3737 }
3738 free(buf);
3739
3740 /* create structure */
3741 switch (dev_mod) {
3742 case LYS_DEV_NOT_SUPPORTED:
3743 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003744 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 break;
3746 case LYS_DEV_ADD:
3747 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003748 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003749 d = (struct lysp_deviate *)d_add;
3750 d_units = &d_add->units;
3751 d_uniques = &d_add->uniques;
3752 d_dflts = &d_add->dflts;
3753 d_musts = &d_add->musts;
3754 d_flags = &d_add->flags;
3755 d_min = &d_add->min;
3756 d_max = &d_add->max;
3757 break;
3758 case LYS_DEV_REPLACE:
3759 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003760 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 d = (struct lysp_deviate *)d_rpl;
3762 d_units = &d_rpl->units;
3763 d_flags = &d_rpl->flags;
3764 d_min = &d_rpl->min;
3765 d_max = &d_rpl->max;
3766 break;
3767 case LYS_DEV_DELETE:
3768 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003769 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003770 d = (struct lysp_deviate *)d_del;
3771 d_units = &d_del->units;
3772 d_uniques = &d_del->uniques;
3773 d_dflts = &d_del->dflts;
3774 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 break;
3776 default:
3777 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003778 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 }
3780 d->mod = dev_mod;
3781
3782 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003783 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003784
Michal Vaskod989ba02020-08-24 10:59:24 +02003785 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003787 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 switch (dev_mod) {
3789 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003790 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003791 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003792 return LY_EVALID;
3793 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003794 LY_CHECK_RET(parse_config(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 break;
3796 }
3797 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003798 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799 switch (dev_mod) {
3800 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003801 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003802 return LY_EVALID;
3803 case LYS_DEV_REPLACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003804 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 +02003805 break;
3806 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003807 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 +02003808 break;
3809 }
3810 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003811 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 switch (dev_mod) {
3813 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003814 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003815 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003816 return LY_EVALID;
3817 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003818 LY_CHECK_RET(parse_mandatory(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 break;
3820 }
3821 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003822 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 switch (dev_mod) {
3824 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003825 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003826 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003827 return LY_EVALID;
3828 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003829 LY_CHECK_RET(parse_maxelements(ctx, in, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 break;
3831 }
3832 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003833 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 switch (dev_mod) {
3835 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003836 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003837 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838 return LY_EVALID;
3839 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003840 LY_CHECK_RET(parse_minelements(ctx, in, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 break;
3842 }
3843 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003844 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845 switch (dev_mod) {
3846 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003847 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003848 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003849 return LY_EVALID;
3850 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003851 LY_CHECK_RET(parse_restrs(ctx, in, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 break;
3853 }
3854 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003855 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003856 switch (dev_mod) {
3857 case LYS_DEV_NOT_SUPPORTED:
3858 case LYS_DEV_ADD:
3859 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003860 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 return LY_EVALID;
3862 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003863 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003864 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003865 return LY_EVALID;
3866 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003867 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003868 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02003869 LY_CHECK_RET(parse_type(ctx, in, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 break;
3871 }
3872 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003873 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874 switch (dev_mod) {
3875 case LYS_DEV_NOT_SUPPORTED:
3876 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003877 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003878 return LY_EVALID;
3879 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003880 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 +02003881 break;
3882 }
3883 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003884 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885 switch (dev_mod) {
3886 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003887 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888 return LY_EVALID;
3889 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003890 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 +02003891 break;
3892 }
3893 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003894 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003895 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003896 break;
3897 default:
David Sedlákb3077192019-06-19 10:55:37 +02003898 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003899 return LY_EVALID;
3900 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 return ret;
3903}
3904
Michal Vaskoea5abea2018-09-18 13:10:54 +02003905/**
3906 * @brief Parse the deviation statement.
3907 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003908 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003909 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003910 * @param[in,out] deviations Deviations to add to.
3911 *
3912 * @return LY_ERR values.
3913 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003914LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003915parse_deviation(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003916{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003917 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003918 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003919 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003920 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921 struct lysp_deviation *dev;
3922
Radek Krejci2c4e7172018-10-19 15:56:26 +02003923 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003924
3925 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003926 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003927 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003928 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929
Michal Vasko63f3d842020-07-08 10:10:14 +02003930 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003932 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003933 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 +02003934 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003935 case LY_STMT_DEVIATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003936 LY_CHECK_RET(parse_deviate(ctx, in, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003938 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003939 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 +02003940 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003941 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003942 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003943 break;
3944 default:
David Sedlákb3077192019-06-19 10:55:37 +02003945 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946 return LY_EVALID;
3947 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003948 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003949 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003950checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003951 /* mandatory substatements */
3952 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003953 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 return LY_EVALID;
3955 }
3956
3957 return ret;
3958}
3959
Michal Vaskoea5abea2018-09-18 13:10:54 +02003960/**
3961 * @brief Parse the feature statement.
3962 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003963 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003964 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003965 * @param[in,out] features Features to add to.
3966 *
3967 * @return LY_ERR values.
3968 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003969LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003970parse_feature(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003971{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003972 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003973 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003974 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003975 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 struct lysp_feature *feat;
3977
Radek Krejci2c4e7172018-10-19 15:56:26 +02003978 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003979
3980 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003981 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003982 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003983
Michal Vaskod989ba02020-08-24 10:59:24 +02003984 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003986 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003987 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 +02003988 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003989 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003990 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003992 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003993 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 +02003994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003995 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003996 LY_CHECK_RET(parse_status(ctx, in, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003998 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003999 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 break;
4001 default:
David Sedlákb3077192019-06-19 10:55:37 +02004002 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004003 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004004 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004005 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004006 return ret;
4007}
4008
Michal Vaskoea5abea2018-09-18 13:10:54 +02004009/**
4010 * @brief Parse the identity statement.
4011 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004012 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004013 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004014 * @param[in,out] identities Identities to add to.
4015 *
4016 * @return LY_ERR values.
4017 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004018LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004019parse_identity(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004020{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004021 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004022 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004023 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004024 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004025 struct lysp_ident *ident;
4026
Radek Krejci2c4e7172018-10-19 15:56:26 +02004027 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004028
4029 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02004030 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004031 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004032
Michal Vaskod989ba02020-08-24 10:59:24 +02004033 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004034 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004035 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004036 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 +02004037 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004038 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004039 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Michal Vasko7f45cf22020-10-01 12:49:44 +02004040 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004041 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004042 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004043 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 +02004044 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004045 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02004046 LY_CHECK_RET(parse_status(ctx, in, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004048 case LY_STMT_BASE:
Michal Vasko69730152020-10-09 16:30:07 +02004049 if (ident->bases && (ctx->mod_version < 2)) {
David Sedlákb3077192019-06-19 10:55:37 +02004050 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 +01004051 return LY_EVALID;
4052 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004053 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 +02004054 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004055 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004056 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 break;
4058 default:
David Sedlákb3077192019-06-19 10:55:37 +02004059 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004060 return LY_EVALID;
4061 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004062 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004063 return ret;
4064}
4065
Michal Vaskoea5abea2018-09-18 13:10:54 +02004066/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004067 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004068 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004069 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004070 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004071 * @param[in,out] mod Module to write to.
4072 *
4073 * @return LY_ERR values.
4074 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004075LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004076parse_module(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004077{
4078 LY_ERR ret = 0;
4079 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004080 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004081 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004082 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004083 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004084
Michal Vaskoc3781c32020-10-06 14:04:08 +02004085 mod->is_submod = 0;
4086
4087 /* module name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004088 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004089 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004090
Michal Vasko63f3d842020-07-08 10:10:14 +02004091 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004092
Radek Krejcie3846472018-10-15 15:24:51 +02004093#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004094 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 +02004095
Michal Vasko7fbc8162018-09-17 10:35:16 +02004096 switch (kw) {
4097 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004098 case LY_STMT_NAMESPACE:
4099 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004100 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004102 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004103 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004104 break;
4105 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004106 case LY_STMT_INCLUDE:
4107 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004108 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004109 break;
4110 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004111 case LY_STMT_ORGANIZATION:
4112 case LY_STMT_CONTACT:
4113 case LY_STMT_DESCRIPTION:
4114 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004115 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004116 break;
4117
4118 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004119 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004120 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004121 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004122 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004123 case LY_STMT_ANYDATA:
4124 case LY_STMT_ANYXML:
4125 case LY_STMT_AUGMENT:
4126 case LY_STMT_CHOICE:
4127 case LY_STMT_CONTAINER:
4128 case LY_STMT_DEVIATION:
4129 case LY_STMT_EXTENSION:
4130 case LY_STMT_FEATURE:
4131 case LY_STMT_GROUPING:
4132 case LY_STMT_IDENTITY:
4133 case LY_STMT_LEAF:
4134 case LY_STMT_LEAF_LIST:
4135 case LY_STMT_LIST:
4136 case LY_STMT_NOTIFICATION:
4137 case LY_STMT_RPC:
4138 case LY_STMT_TYPEDEF:
4139 case LY_STMT_USES:
4140 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004141 mod_stmt = Y_MOD_BODY;
4142 break;
4143 default:
4144 /* error handled in the next switch */
4145 break;
4146 }
Radek Krejcie3846472018-10-15 15:24:51 +02004147#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004148
Radek Krejcie3846472018-10-15 15:24:51 +02004149 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004150 switch (kw) {
4151 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004152 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004153 LY_CHECK_RET(parse_yangversion(ctx, in, &mod->mod->version, &mod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004154 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004156 case LY_STMT_NAMESPACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004157 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 +02004158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004159 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02004160 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 +02004161 break;
4162
4163 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004164 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004165 LY_CHECK_RET(parse_include(ctx, mod->mod->name, in, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004167 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004168 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, in, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169 break;
4170
4171 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004172 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004173 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 +02004174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004175 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004176 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 +02004177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004178 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004179 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 +02004180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004181 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004182 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 +02004183 break;
4184
4185 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004186 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004187 LY_CHECK_RET(parse_revision(ctx, in, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004188 break;
4189
4190 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004191 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004192 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004193 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004194 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004195 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004197 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004198 LY_CHECK_RET(parse_choice(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004200 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004201 LY_CHECK_RET(parse_container(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004203 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004204 LY_CHECK_RET(parse_leaf(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004206 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004207 LY_CHECK_RET(parse_leaflist(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004209 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004210 LY_CHECK_RET(parse_list(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004212 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004213 LY_CHECK_RET(parse_uses(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004214 break;
4215
Radek Krejcid6b76452019-09-03 17:03:03 +02004216 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004217 LY_CHECK_RET(parse_augment(ctx, in, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004219 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004220 LY_CHECK_RET(parse_deviation(ctx, in, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004222 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004223 LY_CHECK_RET(parse_extension(ctx, in, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004225 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004226 LY_CHECK_RET(parse_feature(ctx, in, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004228 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004229 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004231 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004232 LY_CHECK_RET(parse_identity(ctx, in, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004234 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004235 LY_CHECK_RET(parse_notif(ctx, in, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004237 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004238 LY_CHECK_RET(parse_action(ctx, in, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004240 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004241 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004243 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004244 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004245 break;
4246
4247 default:
David Sedlákb3077192019-06-19 10:55:37 +02004248 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004249 return LY_EVALID;
4250 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004251 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004252 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004253
Radek Krejci6d6556c2018-11-08 09:37:45 +01004254checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004255 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004256 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 +01004257
Michal Vasko7fbc8162018-09-17 10:35:16 +02004258 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004259 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004260 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004261 return LY_EVALID;
4262 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004263 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004264 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004265 }
4266
Radek Krejcie9e987e2018-10-31 12:50:27 +01004267 /* submodules share the namespace with the module names, so there must not be
4268 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004269 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4270 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004271 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 +01004272 return LY_EVALID;
4273 }
4274
4275 return ret;
4276}
4277
4278/**
4279 * @brief Parse submodule substatements.
4280 *
4281 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004282 * @param[in,out] in Input structure.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004283 * @param[out] submod Parsed submodule structure.
4284 *
4285 * @return LY_ERR values.
4286 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004287LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004288parse_submodule(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004289{
4290 LY_ERR ret = 0;
4291 char *buf, *word;
4292 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004293 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004294 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4295 struct lysp_submodule *dup;
4296
Michal Vaskoc3781c32020-10-06 14:04:08 +02004297 submod->is_submod = 1;
4298
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004299 /* submodule name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004300 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004301 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004302
Michal Vasko63f3d842020-07-08 10:10:14 +02004303 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004304
4305#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004306 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 +01004307
4308 switch (kw) {
4309 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004310 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004311 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4312 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004313 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004314 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4315 break;
4316 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004317 case LY_STMT_INCLUDE:
4318 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004319 CHECK_ORDER(Y_MOD_LINKAGE);
4320 break;
4321 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004322 case LY_STMT_ORGANIZATION:
4323 case LY_STMT_CONTACT:
4324 case LY_STMT_DESCRIPTION:
4325 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004326 CHECK_ORDER(Y_MOD_META);
4327 break;
4328
4329 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004330 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004331 CHECK_ORDER(Y_MOD_REVISION);
4332 break;
4333 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004334 case LY_STMT_ANYDATA:
4335 case LY_STMT_ANYXML:
4336 case LY_STMT_AUGMENT:
4337 case LY_STMT_CHOICE:
4338 case LY_STMT_CONTAINER:
4339 case LY_STMT_DEVIATION:
4340 case LY_STMT_EXTENSION:
4341 case LY_STMT_FEATURE:
4342 case LY_STMT_GROUPING:
4343 case LY_STMT_IDENTITY:
4344 case LY_STMT_LEAF:
4345 case LY_STMT_LEAF_LIST:
4346 case LY_STMT_LIST:
4347 case LY_STMT_NOTIFICATION:
4348 case LY_STMT_RPC:
4349 case LY_STMT_TYPEDEF:
4350 case LY_STMT_USES:
4351 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004352 mod_stmt = Y_MOD_BODY;
4353 break;
4354 default:
4355 /* error handled in the next switch */
4356 break;
4357 }
4358#undef CHECK_ORDER
4359
4360 prev_kw = kw;
4361 switch (kw) {
4362 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004364 LY_CHECK_RET(parse_yangversion(ctx, in, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004365 ctx->mod_version = submod->version;
4366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004367 case LY_STMT_BELONGS_TO:
Michal Vaskoc3781c32020-10-06 14:04:08 +02004368 LY_CHECK_RET(parse_belongsto(ctx, in, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004369 break;
4370
4371 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004373 LY_CHECK_RET(parse_include(ctx, submod->name, in, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004376 LY_CHECK_RET(parse_import(ctx, submod->prefix, in, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004377 break;
4378
4379 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004380 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004381 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 +01004382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004383 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004384 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 +01004385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004386 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004387 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 +01004388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004389 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004390 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 +01004391 break;
4392
4393 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004395 LY_CHECK_RET(parse_revision(ctx, in, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004396 break;
4397
4398 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004399 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004400 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004401 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004402 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004403 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004405 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004406 LY_CHECK_RET(parse_choice(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004407 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004408 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004409 LY_CHECK_RET(parse_container(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004411 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004412 LY_CHECK_RET(parse_leaf(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004413 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004414 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004415 LY_CHECK_RET(parse_leaflist(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004416 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004417 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004418 LY_CHECK_RET(parse_list(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004419 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004420 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004421 LY_CHECK_RET(parse_uses(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004422 break;
4423
Radek Krejcid6b76452019-09-03 17:03:03 +02004424 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004425 LY_CHECK_RET(parse_augment(ctx, in, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004426 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004427 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004428 LY_CHECK_RET(parse_deviation(ctx, in, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004429 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004430 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004431 LY_CHECK_RET(parse_extension(ctx, in, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004433 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004434 LY_CHECK_RET(parse_feature(ctx, in, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004436 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004437 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004438 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004439 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004440 LY_CHECK_RET(parse_identity(ctx, in, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004441 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004442 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004443 LY_CHECK_RET(parse_notif(ctx, in, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004444 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004445 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004446 LY_CHECK_RET(parse_action(ctx, in, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004447 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004448 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004449 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004450 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004451 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004452 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004453 break;
4454
4455 default:
David Sedlákb3077192019-06-19 10:55:37 +02004456 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004457 return LY_EVALID;
4458 }
4459 }
4460 LY_CHECK_RET(ret);
4461
4462checks:
4463 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004464 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments,
Michal Vasko69730152020-10-09 16:30:07 +02004465 submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004466
4467 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004468 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004469 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004470 return LY_EVALID;
4471 }
4472
4473 /* submodules share the namespace with the module names, so there must not be
4474 * a submodule of the same name in the context, no need for revision matching */
4475 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004476 /* main modules may have different revisions */
4477 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02004478 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004479 return LY_EVALID;
4480 }
4481
Michal Vasko7fbc8162018-09-17 10:35:16 +02004482 return ret;
4483}
4484
Radek Krejcid4557c62018-09-17 11:42:09 +02004485LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004486yang_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 +02004487 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004488{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004489 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004490 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004491 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004492 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004493 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004494
David Sedlák1b623122019-08-05 15:27:49 +02004495 /* create context */
4496 *context = calloc(1, sizeof **context);
4497 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004498 (*context)->format = LYS_IN_YANG;
Michal Vasko7c8439f2020-08-05 13:25:19 +02004499 (*context)->main_mod = main_ctx->main_mod;
David Sedlák1b623122019-08-05 15:27:49 +02004500 (*context)->ctx = ly_ctx;
Radek Krejci99435242019-09-05 16:19:15 +02004501 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004502 (*context)->line = 1;
4503
4504 /* map the typedefs and groupings list from main context to the submodule's context */
4505 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4506 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4507
Michal Vasko7fbc8162018-09-17 10:35:16 +02004508 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004509 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004510 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004511
Radek Krejcid6b76452019-09-03 17:03:03 +02004512 if (kw == LY_STMT_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004513 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004514 ret = LY_EINVAL;
4515 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004516 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004517 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004518 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004519 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004520 }
4521
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004522 mod_p = calloc(1, sizeof *mod_p);
Radek Krejcif027df72020-09-15 13:00:28 +02004523 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx); ret = LY_EMEM, cleanup);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004524 mod_p->mod = main_ctx->main_mod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004525 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004526
4527 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004528 ret = parse_submodule(*context, in, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004529 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004530
4531 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004532 while (isspace(in->current[0])) {
4533 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004534 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004535 if (in->current[0]) {
4536 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004537 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004538 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004539 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004540
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004541 mod_p->parsing = 0;
4542 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004543
Radek Krejcibbe09a92018-11-08 09:36:54 +01004544cleanup:
4545 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004546 lysp_submodule_free((*context)->ctx, mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004547 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004548 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004549 }
4550
4551 return ret;
4552}
4553
4554LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004555yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004556{
4557 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004558 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004559 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004560 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004561 struct lysp_module *mod_p = NULL;
4562
David Sedlák1b623122019-08-05 15:27:49 +02004563 /* create context */
4564 *context = calloc(1, sizeof **context);
4565 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004566 (*context)->format = LYS_IN_YANG;
Michal Vasko7c8439f2020-08-05 13:25:19 +02004567 (*context)->main_mod = mod;
David Sedlák1b623122019-08-05 15:27:49 +02004568 (*context)->ctx = mod->ctx;
Radek Krejci335332a2019-09-05 13:03:35 +02004569 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004570 (*context)->line = 1;
4571
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004572 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004573 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004574 LY_CHECK_GOTO(ret, cleanup);
4575
Radek Krejcid6b76452019-09-03 17:03:03 +02004576 if (kw == LY_STMT_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004577 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 +01004578 ret = LY_EINVAL;
4579 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004580 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004581 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004582 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004583 goto cleanup;
4584 }
4585
4586 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004587 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004588 mod_p->mod = mod;
4589 mod_p->parsing = 1;
4590
4591 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004592 ret = parse_module(*context, in, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004593 LY_CHECK_GOTO(ret, cleanup);
4594
4595 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004596 while (isspace(in->current[0])) {
4597 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004598 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004599 if (in->current[0]) {
4600 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004601 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004602 goto cleanup;
4603 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004604
4605 mod_p->parsing = 0;
4606 mod->parsed = mod_p;
4607
4608cleanup:
4609 if (ret) {
4610 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004611 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004612 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004613 }
4614
Michal Vasko7fbc8162018-09-17 10:35:16 +02004615 return ret;
4616}