blob: 44c232cffb84cdea841f96f9cbf343b916dfdfb4 [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko63f3d842020-07-08 10:10:14 +020014#include "parser_internal.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
17#include <ctype.h>
18#include <errno.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020028#include "path.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "set.h"
31#include "tree.h"
32#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020033#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020034
Radek Krejciceaf2122019-01-02 15:03:26 +010035/**
36 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
37 * @param[in] CTX yang parser context to access libyang context.
38 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
39 * @param[out] TARGET variable where to store the pointer to the inserted value.
40 * @param[in] WORD string to store.
41 * @param[in] LEN length of the string in WORD to store.
42 */
Radek 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),
152 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, in->current[-len]), LY_EVALID);
153 in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200154 if (c == '\n') {
155 ctx->indent = 0;
156 } else {
157 /* note - even the multibyte character is count as 1 */
158 ++ctx->indent;
159 }
160
Radek Krejci44ceedc2018-10-02 15:54:31 +0200161 /* check character validity */
162 switch (arg) {
163 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100164 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200165 break;
166 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100167 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200168 break;
169 case Y_STR_ARG:
170 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100171 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200172 break;
173 }
174
Michal Vasko7fbc8162018-09-17 10:35:16 +0200175 if (word_b && *word_b) {
176 /* add another character into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200177 if (buf_add_char(ctx->ctx, in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200178 return LY_EMEM;
179 }
180
181 /* in case of realloc */
182 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200183 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200184 /* first time we need a buffer, copy everything read up to now */
185 if (*word_len) {
186 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200187 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200188 *buf_len = *word_len;
189 memcpy(*word_b, *word_p, *word_len);
190 }
191
192 /* add this new character into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200193 if (buf_add_char(ctx->ctx, in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200194 return LY_EMEM;
195 }
196
197 /* in case of realloc */
198 *word_p = *word_b;
199 } else {
200 /* just remember the first character pointer */
201 if (!*word_p) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200202 *word_p = (char *)in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200203 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200204 /* ... and update the word's length */
205 (*word_len) += len;
Michal Vasko63f3d842020-07-08 10:10:14 +0200206 ly_in_skip(in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200207 }
208
209 return LY_SUCCESS;
210}
211
Michal Vaskoea5abea2018-09-18 13:10:54 +0200212/**
213 * @brief Skip YANG comment in data.
214 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200215 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200216 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200217 * @param[in] comment Type of the comment to process:
218 * 1 for a one-line comment,
219 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200220 * @return LY_ERR values.
221 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200222LY_ERR
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'.",
434 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:
Radek Krejci4e199f52019-05-28 09:09:28 +0200492 if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) {
493 /* 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 Vasko7fbc8162018-09-17 10:35:16 +0200520 /* word buffer - dynamically allocated */
521 *word_b = NULL;
522
523 /* word pointer - just a pointer to data */
524 *word_p = NULL;
525
526 *word_len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200527 while (in->current[0]) {
528 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200529 case '\'':
530 case '\"':
531 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200532 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Michal Vasko63f3d842020-07-08 10:10:14 +0200533 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200534 "unquoted string character, optsep, semicolon or opening brace");
535 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200536 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200537 if (flags) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200538 (*flags) |= in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200539 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200540 LY_CHECK_RET(read_qstring(ctx, in, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200541 goto str_end;
542 case '/':
Michal Vasko63f3d842020-07-08 10:10:14 +0200543 if (in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200544 /* one-line comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200545 MOVE_INPUT(ctx, in, 2);
546 LY_CHECK_RET(skip_comment(ctx, in, 1));
547 } else if (in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200548 /* block comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200549 MOVE_INPUT(ctx, in, 2);
550 LY_CHECK_RET(skip_comment(ctx, in, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200551 } else {
552 /* not a comment after all */
Michal Vasko63f3d842020-07-08 10:10:14 +0200553 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 +0200554 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200555 break;
556 case ' ':
557 if (*word_len) {
558 /* word is finished */
559 goto str_end;
560 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200561 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200562 break;
563 case '\t':
564 if (*word_len) {
565 /* word is finished */
566 goto str_end;
567 }
568 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200569 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200570
Michal Vasko63f3d842020-07-08 10:10:14 +0200571 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200572 break;
573 case '\n':
574 if (*word_len) {
575 /* word is finished */
576 goto str_end;
577 }
578 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200579 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200580
581 /* track line numbers */
582 ++ctx->line;
583
Michal Vasko63f3d842020-07-08 10:10:14 +0200584 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200585 break;
586 case ';':
587 case '{':
588 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
589 /* word is finished */
590 goto str_end;
591 }
592
Michal Vasko63f3d842020-07-08 10:10:14 +0200593 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200594 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200595 case '}':
596 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200597 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200598 "unquoted string character, optsep, semicolon or opening brace");
599 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200601 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 +0200602 break;
603 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200604 }
605
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200606 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200607 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200608 return LY_EVALID;
609
Michal Vasko7fbc8162018-09-17 10:35:16 +0200610str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200611 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200612 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200613 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
614 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
615 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200616 *word_p = *word_b;
617 }
618
619 return LY_SUCCESS;
620}
621
Michal Vaskoea5abea2018-09-18 13:10:54 +0200622/**
623 * @brief Get another YANG keyword from the raw data.
624 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200625 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200626 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200627 * @param[out] kw YANG keyword read.
628 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
629 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
630 *
631 * @return LY_ERR values.
632 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200633LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200634get_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 +0200635{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200636 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200637 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200638 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200639
640 if (word_p) {
641 *word_p = NULL;
642 *word_len = 0;
643 }
644
645 /* first skip "optsep", comments */
Michal Vasko63f3d842020-07-08 10:10:14 +0200646 while (in->current[0]) {
647 switch (in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200648 case '/':
Michal Vasko63f3d842020-07-08 10:10:14 +0200649 if (in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200650 /* one-line comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200651 MOVE_INPUT(ctx, in, 2);
652 LY_CHECK_RET(skip_comment(ctx, in, 1));
653 } else if (in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200654 /* block comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200655 MOVE_INPUT(ctx, in, 2);
656 LY_CHECK_RET(skip_comment(ctx, in, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200657 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200658 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200659 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200660 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200661 }
Radek Krejci13028282019-06-11 14:56:48 +0200662 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200663 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200664 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200665 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200666 ctx->indent = 0;
667 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200668 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200669 /* skip whitespaces (optsep) */
670 ++ctx->indent;
671 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200673 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200674 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200675 break;
676 default:
677 /* either a keyword start or an invalid character */
678 goto keyword_start;
679 }
680
Michal Vasko63f3d842020-07-08 10:10:14 +0200681 ly_in_skip(in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200682 }
683
684keyword_start:
Michal Vasko63f3d842020-07-08 10:10:14 +0200685 word_start = in->current;
686 *kw = lysp_match_kw(ctx, in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200687
Radek Krejcid6b76452019-09-03 17:03:03 +0200688 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 +0200689 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200690 }
691
Radek Krejcid6b76452019-09-03 17:03:03 +0200692 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200693 /* make sure we have the whole keyword */
Michal Vasko63f3d842020-07-08 10:10:14 +0200694 switch (in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200695 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200696 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200697 case ' ':
698 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200700 case ':':
701 /* keyword is not actually a keyword, but prefix of an extension.
702 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
703 * and we will be checking the keyword (extension instance) itself */
704 prefix = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200705 MOVE_INPUT(ctx, in, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200706 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200707 case '{':
708 /* allowed only for input and output statements which can be without arguments */
Radek Krejcid6b76452019-09-03 17:03:03 +0200709 if (*kw == LY_STMT_INPUT || *kw == LY_STMT_OUTPUT) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200710 break;
711 }
Radek Krejci0f969882020-08-21 16:56:47 +0200712 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200713 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200714 MOVE_INPUT(ctx, in, 1);
715 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(in->current - word_start), word_start,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200716 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200717 return LY_EVALID;
718 }
719 } else {
720 /* still can be an extension */
721 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200722extension:
Michal Vasko63f3d842020-07-08 10:10:14 +0200723 while (in->current[0] && (in->current[0] != ' ') && (in->current[0] != '\t') && (in->current[0] != '\n')
724 && (in->current[0] != '{') && (in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200725 uint32_t c = 0;
726
Michal Vasko63f3d842020-07-08 10:10:14 +0200727 LY_CHECK_ERR_RET(ly_getutf8(&in->current, &c, &len),
728 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200729 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200730 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200731 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
732 in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200733 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200734 if (!in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200735 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 return LY_EVALID;
737 }
738
739 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200740 if (prefix != 2) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200741 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200742 return LY_EVALID;
743 }
744
Radek Krejcid6b76452019-09-03 17:03:03 +0200745 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200746 }
Radek Krejci626df482018-10-11 15:06:31 +0200747success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 if (word_p) {
749 *word_p = (char *)word_start;
Michal Vasko63f3d842020-07-08 10:10:14 +0200750 *word_len = in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200751 }
752
753 return LY_SUCCESS;
754}
755
Michal Vaskoea5abea2018-09-18 13:10:54 +0200756/**
757 * @brief Parse extension instance substatements.
758 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200759 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200760 * @param[in,out] in Input structure.
Radek Krejci335332a2019-09-05 13:03:35 +0200761 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200762 * @param[in] word Extension instance substatement name (keyword).
763 * @param[in] word_len Extension instance substatement name length.
764 * @param[in,out] child Children of this extension instance to add to.
765 *
766 * @return LY_ERR values.
767 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200768static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200769parse_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 +0200770 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200771{
772 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100773 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200774 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200775 struct lysp_stmt *stmt, *par_child;
776
777 stmt = calloc(1, sizeof *stmt);
778 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
779
Radek Krejcibb9b1982019-04-08 14:24:59 +0200780 /* insert into parent statements */
781 if (!*child) {
782 *child = stmt;
783 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200784 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200785 par_child->next = stmt;
786 }
787
Radek Krejci011e4aa2020-09-04 15:22:31 +0200788 LY_CHECK_RET(lydict_insert(ctx->ctx, word, word_len, &stmt->stmt));
Radek Krejci335332a2019-09-05 13:03:35 +0200789 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200790
791 /* get optional argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200792 LY_CHECK_RET(get_argument(ctx, in, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200793
Radek Krejci0ae092d2018-09-20 16:43:19 +0200794 if (word) {
795 if (buf) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200796 LY_CHECK_RET(lydict_insert_zc(ctx->ctx, word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200797 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200798 LY_CHECK_RET(lydict_insert(ctx->ctx, word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200799 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200800 }
801
Michal Vasko63f3d842020-07-08 10:10:14 +0200802 YANG_READ_SUBSTMT_FOR(ctx, in, child_kw, word, word_len, ret, ) {
803 LY_CHECK_RET(parse_ext_substmt(ctx, in, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200804 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200805 return ret;
806}
807
Michal Vaskoea5abea2018-09-18 13:10:54 +0200808/**
809 * @brief Parse extension instance.
810 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200811 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200812 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200813 * @param[in] ext_name Extension instance substatement name (keyword).
814 * @param[in] ext_name_len Extension instance substatement name length.
815 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
816 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
817 * @param[in,out] exts Extension instances to add to.
818 *
819 * @return LY_ERR values.
820 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200822parse_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 +0200823 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200824{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100825 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200827 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200828 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200829 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200830
Radek Krejci2c4e7172018-10-19 15:56:26 +0200831 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200832
833 /* store name and insubstmt info */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200834 LY_CHECK_RET(lydict_insert(ctx->ctx, ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200835 e->insubstmt = insubstmt;
836 e->insubstmt_index = insubstmt_index;
837
838 /* get optional argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200839 LY_CHECK_RET(get_argument(ctx, in, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200840
Radek Krejci0ae092d2018-09-20 16:43:19 +0200841 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200842 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843 }
844
Michal Vaskod989ba02020-08-24 10:59:24 +0200845 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200846 LY_CHECK_RET(parse_ext_substmt(ctx, in, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200847 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200848 return ret;
849}
850
Michal Vaskoea5abea2018-09-18 13:10:54 +0200851/**
852 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
853 * description, etc...
854 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200855 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200856 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200857 * @param[in] substmt Type of this substatement.
858 * @param[in] substmt_index Index of this substatement.
859 * @param[in,out] value Place to store the parsed value.
860 * @param[in] arg Type of the YANG keyword argument (of the value).
861 * @param[in,out] exts Extension instances to add to.
862 *
863 * @return LY_ERR values.
864 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200865static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200866parse_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 +0200867 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200868{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100869 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200871 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200872 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200873
874 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200875 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876 return LY_EVALID;
877 }
878
879 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200880 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881
882 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200883 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200884
Michal Vaskod989ba02020-08-24 10:59:24 +0200885 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200887 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200888 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200889 break;
890 default:
David Sedlákb3077192019-06-19 10:55:37 +0200891 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892 return LY_EVALID;
893 }
894 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895 return ret;
896}
897
Michal Vaskoea5abea2018-09-18 13:10:54 +0200898/**
899 * @brief Parse the yang-version statement.
900 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200901 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200902 * @param[in,out] in Input structure.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100903 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200904 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200905 *
906 * @return LY_ERR values.
907 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200908static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200909parse_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 +0200910{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100911 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200913 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200914 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200915
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100916 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200917 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918 return LY_EVALID;
919 }
920
921 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200922 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200923
Radek Krejci96e48da2020-09-04 13:18:06 +0200924 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100925 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200926 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100927 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200929 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200930 free(buf);
931 return LY_EVALID;
932 }
933 free(buf);
934
Michal Vaskod989ba02020-08-24 10:59:24 +0200935 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200937 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200938 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939 break;
940 default:
David Sedlákb3077192019-06-19 10:55:37 +0200941 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200942 return LY_EVALID;
943 }
944 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200945 return ret;
946}
947
Michal Vaskoea5abea2018-09-18 13:10:54 +0200948/**
949 * @brief Parse the belongs-to statement.
950 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200951 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200952 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200953 * @param[in,out] belongsto Place to store the parsed value.
954 * @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 Vasko63f3d842020-07-08 10:10:14 +0200960parse_belongsto(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **belongsto, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +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
967 if (*belongsto) {
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
972 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200973 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200974
Radek Krejci011e4aa2020-09-04 15:22:31 +0200975 INSERT_WORD_RET(ctx, buf, *belongsto, word, word_len);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200976
Michal Vasko63f3d842020-07-08 10:10:14 +0200977 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200978 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200979 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +0200980 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200982 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200983 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 break;
985 default:
David Sedlákb3077192019-06-19 10:55:37 +0200986 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987 return LY_EVALID;
988 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200990 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100991checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992 /* mandatory substatements */
993 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200994 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995 return LY_EVALID;
996 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200997 return ret;
998}
999
Michal Vaskoea5abea2018-09-18 13:10:54 +02001000/**
1001 * @brief Parse the revision-date statement.
1002 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001003 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001004 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001005 * @param[in,out] rev Array to store the parsed value in.
1006 * @param[in,out] exts Extension instances to add to.
1007 *
1008 * @return LY_ERR values.
1009 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001010static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001011parse_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 +02001012{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001013 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001015 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001016 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001017
1018 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001019 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001020 return LY_EVALID;
1021 }
1022
1023 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001024 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025
1026 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001027 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001028 free(buf);
1029 return LY_EVALID;
1030 }
1031
1032 /* store value and spend buf if allocated */
1033 strncpy(rev, word, word_len);
1034 free(buf);
1035
Michal Vaskod989ba02020-08-24 10:59:24 +02001036 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001038 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001039 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001040 break;
1041 default:
David Sedlákb3077192019-06-19 10:55:37 +02001042 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001043 return LY_EVALID;
1044 }
1045 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001046 return ret;
1047}
1048
Michal Vaskoea5abea2018-09-18 13:10:54 +02001049/**
1050 * @brief Parse the include statement.
1051 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001052 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001053 * @param[in] module_name Name of the module to check name collisions.
Michal Vasko63f3d842020-07-08 10:10:14 +02001054 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001055 * @param[in,out] includes Parsed includes to add to.
1056 *
1057 * @return LY_ERR values.
1058 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001059static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001060parse_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 +02001061{
Radek Krejcid33273d2018-10-25 14:55:52 +02001062 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001063 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001064 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001065 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001066 struct lysp_include *inc;
1067
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001068 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069
1070 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001071 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001072
Radek Krejci011e4aa2020-09-04 15:22:31 +02001073 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001074
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001075 /* submodules share the namespace with the module names, so there must not be
1076 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001077 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001078 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001079 return LY_EVALID;
1080 }
1081
Michal Vaskod989ba02020-08-24 10:59:24 +02001082 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001084 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001085 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko63f3d842020-07-08 10:10:14 +02001086 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 +02001087 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001088 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001089 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko63f3d842020-07-08 10:10:14 +02001090 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 +02001091 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001092 case LY_STMT_REVISION_DATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001093 LY_CHECK_RET(parse_revisiondate(ctx, in, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001095 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001096 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097 break;
1098 default:
David Sedlákb3077192019-06-19 10:55:37 +02001099 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001100 return LY_EVALID;
1101 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001103 return ret;
1104}
1105
Michal Vaskoea5abea2018-09-18 13:10:54 +02001106/**
1107 * @brief Parse the import statement.
1108 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001109 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001110 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vasko63f3d842020-07-08 10:10:14 +02001111 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001112 * @param[in,out] imports Parsed imports to add to.
1113 *
1114 * @return LY_ERR values.
1115 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001117parse_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 +02001118{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001119 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001120 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001121 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001122 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001123 struct lysp_import *imp;
1124
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001125 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126
1127 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001128 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001129 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130
Michal Vasko63f3d842020-07-08 10:10:14 +02001131 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001133 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02001134 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 +01001135 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001137 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001138 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko63f3d842020-07-08 10:10:14 +02001139 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 +02001140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001141 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001142 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko63f3d842020-07-08 10:10:14 +02001143 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 +02001144 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001145 case LY_STMT_REVISION_DATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001146 LY_CHECK_RET(parse_revisiondate(ctx, in, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001148 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001149 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150 break;
1151 default:
David Sedlákb3077192019-06-19 10:55:37 +02001152 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153 return LY_EVALID;
1154 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001156 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001157checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001159 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160
1161 return ret;
1162}
1163
Michal Vaskoea5abea2018-09-18 13:10:54 +02001164/**
1165 * @brief Parse the revision statement.
1166 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001167 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001168 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001169 * @param[in,out] revs Parsed revisions to add to.
1170 *
1171 * @return LY_ERR values.
1172 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001173static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001174parse_revision(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001176 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001177 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001178 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001179 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180 struct lysp_revision *rev;
1181
Radek Krejci2c4e7172018-10-19 15:56:26 +02001182 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183
1184 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001185 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001186
1187 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001188 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001189 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190 return LY_EVALID;
1191 }
1192
Radek Krejcib7db73a2018-10-24 14:18:40 +02001193 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 free(buf);
1195
Michal Vaskod989ba02020-08-24 10:59:24 +02001196 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001198 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001199 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 +02001200 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001201 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001202 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 +02001203 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001204 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001205 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206 break;
1207 default:
David Sedlákb3077192019-06-19 10:55:37 +02001208 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001209 return LY_EVALID;
1210 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212 return ret;
1213}
1214
Michal Vaskoea5abea2018-09-18 13:10:54 +02001215/**
1216 * @brief Parse a generic text field that can have more instances such as base.
1217 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001218 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001219 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001220 * @param[in] substmt Type of this substatement.
1221 * @param[in,out] texts Parsed values to add to.
1222 * @param[in] arg Type of the expected argument.
1223 * @param[in,out] exts Extension instances to add to.
1224 *
1225 * @return LY_ERR values.
1226 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001228parse_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 +02001229 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001231 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001232 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001233 const char **item;
1234 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001235 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236
1237 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001238 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239
1240 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001241 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242
Radek Krejci011e4aa2020-09-04 15:22:31 +02001243 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Michal Vaskod989ba02020-08-24 10:59:24 +02001244 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001246 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001247 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248 break;
1249 default:
David Sedlákb3077192019-06-19 10:55:37 +02001250 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251 return LY_EVALID;
1252 }
1253 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254 return ret;
1255}
1256
Michal Vaskoea5abea2018-09-18 13:10:54 +02001257/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001258 * @brief Parse a generic text field that can have more instances such as base.
1259 *
1260 * @param[in] ctx yang parser context for logging.
1261 * @param[in,out] in Input structure.
1262 * @param[in] substmt Type of this substatement.
1263 * @param[in,out] qnames Parsed qnames to add to.
1264 * @param[in] arg Type of the expected argument.
1265 * @param[in,out] exts Extension instances to add to.
1266 *
1267 * @return LY_ERR values.
1268 */
1269static LY_ERR
1270parse_qnames(struct lys_yang_parser_ctx *ctx, struct ly_in *in, LYEXT_SUBSTMT substmt, struct lysp_qname **qnames,
1271 enum yang_arg arg, struct lysp_ext_instance **exts)
1272{
1273 LY_ERR ret = LY_SUCCESS;
1274 char *buf, *word;
1275 struct lysp_qname *item;
1276 size_t word_len;
1277 enum ly_stmt kw;
1278
1279 /* allocate new pointer */
1280 LY_ARRAY_NEW_RET(ctx->ctx, *qnames, item, LY_EMEM);
1281
1282 /* get value */
1283 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
1284
1285 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
1286 item->mod = ctx->main_mod;
1287 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
1288 switch (kw) {
1289 case LY_STMT_EXTENSION_INSTANCE:
1290 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
1291 break;
1292 default:
1293 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
1294 return LY_EVALID;
1295 }
1296 }
1297 return ret;
1298}
1299
1300/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001301 * @brief Parse the config statement.
1302 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001303 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001304 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001305 * @param[in,out] flags Flags to add to.
1306 * @param[in,out] exts Extension instances to add to.
1307 *
1308 * @return LY_ERR values.
1309 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001311parse_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 +02001312{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001313 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001315 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001316 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001317
1318 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001319 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001320 return LY_EVALID;
1321 }
1322
1323 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001324 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325
1326 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1327 *flags |= LYS_CONFIG_W;
1328 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1329 *flags |= LYS_CONFIG_R;
1330 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001331 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332 free(buf);
1333 return LY_EVALID;
1334 }
1335 free(buf);
1336
Michal Vaskod989ba02020-08-24 10:59:24 +02001337 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001338 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001339 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001340 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001341 break;
1342 default:
David Sedlákb3077192019-06-19 10:55:37 +02001343 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001344 return LY_EVALID;
1345 }
1346 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001347 return ret;
1348}
1349
Michal Vaskoea5abea2018-09-18 13:10:54 +02001350/**
1351 * @brief Parse the mandatory statement.
1352 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001353 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001354 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001355 * @param[in,out] flags Flags to add to.
1356 * @param[in,out] exts Extension instances to add to.
1357 *
1358 * @return LY_ERR values.
1359 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001360static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001361parse_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 +02001362{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001363 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001365 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001366 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001367
1368 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001369 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001370 return LY_EVALID;
1371 }
1372
1373 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001374 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001375
1376 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1377 *flags |= LYS_MAND_TRUE;
1378 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1379 *flags |= LYS_MAND_FALSE;
1380 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001381 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001382 free(buf);
1383 return LY_EVALID;
1384 }
1385 free(buf);
1386
Michal Vaskod989ba02020-08-24 10:59:24 +02001387 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001389 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001390 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001391 break;
1392 default:
David Sedlákb3077192019-06-19 10:55:37 +02001393 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001394 return LY_EVALID;
1395 }
1396 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001397 return ret;
1398}
1399
Michal Vaskoea5abea2018-09-18 13:10:54 +02001400/**
1401 * @brief Parse a restriction such as range or length.
1402 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001403 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001404 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001405 * @param[in] restr_kw Type of this particular restriction.
1406 * @param[in,out] exts Extension instances to add to.
1407 *
1408 * @return LY_ERR values.
1409 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001410static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001411parse_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 +02001412{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001413 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001414 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001415 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001416 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417
1418 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001419 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420
Michal Vaskob36053d2020-03-26 15:49:30 +01001421 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001422 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
1423 restr->arg.mod = ctx->main_mod;
Michal Vaskod989ba02020-08-24 10:59:24 +02001424 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001425 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001426 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001427 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 +02001428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001429 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001430 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 +02001431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001432 case LY_STMT_ERROR_APP_TAG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001433 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 +02001434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001435 case LY_STMT_ERROR_MESSAGE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001436 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 +02001437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001438 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001439 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440 break;
1441 default:
David Sedlákb3077192019-06-19 10:55:37 +02001442 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443 return LY_EVALID;
1444 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001445 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001446 return ret;
1447}
1448
Michal Vaskoea5abea2018-09-18 13:10:54 +02001449/**
1450 * @brief Parse a restriction that can have more instances such as must.
1451 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001452 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001453 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001454 * @param[in] restr_kw Type of this particular restriction.
1455 * @param[in,out] restrs Restrictions to add to.
1456 *
1457 * @return LY_ERR values.
1458 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001459static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001460parse_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 +02001461{
1462 struct lysp_restr *restr;
1463
Radek Krejci2c4e7172018-10-19 15:56:26 +02001464 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02001465 return parse_restr(ctx, in, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466}
1467
Michal Vaskoea5abea2018-09-18 13:10:54 +02001468/**
1469 * @brief Parse the status statement.
1470 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001471 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001472 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001473 * @param[in,out] flags Flags to add to.
1474 * @param[in,out] exts Extension instances to add to.
1475 *
1476 * @return LY_ERR values.
1477 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001479parse_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 +02001480{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001481 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001482 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001483 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001484 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001485
1486 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001487 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488 return LY_EVALID;
1489 }
1490
1491 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001492 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493
1494 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1495 *flags |= LYS_STATUS_CURR;
1496 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1497 *flags |= LYS_STATUS_DEPRC;
1498 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1499 *flags |= LYS_STATUS_OBSLT;
1500 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001501 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001502 free(buf);
1503 return LY_EVALID;
1504 }
1505 free(buf);
1506
Michal Vaskod989ba02020-08-24 10:59:24 +02001507 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001509 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001510 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001511 break;
1512 default:
David Sedlákb3077192019-06-19 10:55:37 +02001513 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 return LY_EVALID;
1515 }
1516 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 return ret;
1518}
1519
Michal Vaskoea5abea2018-09-18 13:10:54 +02001520/**
1521 * @brief Parse the when statement.
1522 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001523 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001524 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001525 * @param[in,out] when_p When pointer to parse to.
1526 *
1527 * @return LY_ERR values.
1528 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001529LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001530parse_when(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001532 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001533 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001534 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001535 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536 struct lysp_when *when;
1537
1538 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001539 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001540 return LY_EVALID;
1541 }
1542
1543 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001544 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001545 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546
1547 /* get value */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001548 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001549 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001550 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551
Radek Krejcif09e4e82019-06-14 15:08:11 +02001552
Michal Vaskod989ba02020-08-24 10:59:24 +02001553 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001555 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001556 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 +02001557 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001558 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001559 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 +02001560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001561 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001562 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001563 break;
1564 default:
David Sedlákb3077192019-06-19 10:55:37 +02001565 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001566 return LY_EVALID;
1567 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569 return ret;
1570}
1571
Michal Vaskoea5abea2018-09-18 13:10:54 +02001572/**
1573 * @brief Parse the anydata or anyxml statement.
1574 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001575 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001576 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001577 * @param[in] kw Type of this particular keyword.
1578 * @param[in,out] siblings Siblings to add to.
1579 *
1580 * @return LY_ERR values.
1581 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001582LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001583parse_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 +02001584{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001585 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001587 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588 struct lysp_node_anydata *any;
1589
David Sedlák60adc092019-08-06 15:57:02 +02001590 /* create new structure and insert into siblings */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02001591 LY_LIST_NEW_RET(ctx->ctx, siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001592
Radek Krejcid6b76452019-09-03 17:03:03 +02001593 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001594 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001595
Michal Vasko7fbc8162018-09-17 10:35:16 +02001596 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02001597 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001598 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001599
1600 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02001601 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001602 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001603 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001604 LY_CHECK_RET(parse_config(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001606 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001607 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 +02001608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001609 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001610 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001611 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001612 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02001613 LY_CHECK_RET(parse_mandatory(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001615 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02001616 LY_CHECK_RET(parse_restrs(ctx, in, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001618 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001619 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 +02001620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001621 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02001622 LY_CHECK_RET(parse_status(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001624 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001625 LY_CHECK_RET(parse_when(ctx, in, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001627 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001628 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001629 break;
1630 default:
David Sedlákb3077192019-06-19 10:55:37 +02001631 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejci0f969882020-08-21 16:56:47 +02001632 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 return LY_EVALID;
1634 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001635 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 return ret;
1637}
1638
Michal Vaskoea5abea2018-09-18 13:10:54 +02001639/**
1640 * @brief Parse the value or position statement. Substatement of type enum statement.
1641 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001642 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001643 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001644 * @param[in] val_kw Type of this particular keyword.
1645 * @param[in,out] value Value to write to.
1646 * @param[in,out] flags Flags to write to.
1647 * @param[in,out] exts Extension instances to add to.
1648 *
1649 * @return LY_ERR values.
1650 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001651LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001652parse_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 +02001653 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001654{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001655 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001656 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001657 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001658 long int num = 0;
1659 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001660 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661
1662 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001663 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 return LY_EVALID;
1665 }
1666 *flags |= LYS_SET_VALUE;
1667
1668 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001669 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001670
Radek Krejcid6b76452019-09-03 17:03:03 +02001671 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 +02001672 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001673 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001674 }
1675
1676 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001677 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001678 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001679 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001680 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001681 goto error;
1682 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001683 } else {
1684 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001685 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001686 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001687 goto error;
1688 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001689 }
1690 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001691 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001692 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001693 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001694 }
1695 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001696 LOGVAL_PARSER(ctx, LY_VCODE_OOB, 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 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001699 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001700 *value = num;
1701 } else {
1702 *value = unum;
1703 }
1704 free(buf);
1705
Michal Vaskod989ba02020-08-24 10:59:24 +02001706 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001708 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001709 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 +02001710 break;
1711 default:
David Sedlákb3077192019-06-19 10:55:37 +02001712 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 return LY_EVALID;
1714 }
1715 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001716 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001717
1718error:
1719 free(buf);
1720 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001721}
1722
Michal Vaskoea5abea2018-09-18 13:10:54 +02001723/**
1724 * @brief Parse the enum or bit statement. Substatement of type statement.
1725 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001726 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001727 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001728 * @param[in] enum_kw Type of this particular keyword.
1729 * @param[in,out] enums Enums or bits to add to.
1730 *
1731 * @return LY_ERR values.
1732 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001734parse_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 +02001735{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001736 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001738 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001739 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740 struct lysp_type_enum *enm;
1741
Radek Krejci2c4e7172018-10-19 15:56:26 +02001742 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001743
1744 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001745 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 +02001746 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001747 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001748 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001749 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001750
Radek Krejci011e4aa2020-09-04 15:22:31 +02001751 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001752 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1753
Michal Vaskod989ba02020-08-24 10:59:24 +02001754 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001755 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001756 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001757 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 +02001758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001759 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001760 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001761 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001764 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 +02001765 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001766 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02001767 LY_CHECK_RET(parse_status(ctx, in, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001769 case LY_STMT_VALUE:
1770 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
David Sedlák9fb515f2019-07-11 10:33:58 +02001771 ly_stmt2str(enum_kw)), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +02001772 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 +02001773 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001774 case LY_STMT_POSITION:
1775 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
David Sedlák9fb515f2019-07-11 10:33:58 +02001776 ly_stmt2str(enum_kw)), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +02001777 LY_CHECK_RET(parse_type_enum_value_pos(ctx, in, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001778 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001779 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001780 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001781 break;
1782 default:
David Sedlákb3077192019-06-19 10:55:37 +02001783 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001784 return LY_EVALID;
1785 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001786 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001787 return ret;
1788}
1789
Michal Vaskoea5abea2018-09-18 13:10:54 +02001790/**
1791 * @brief Parse the fraction-digits statement. Substatement of type statement.
1792 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001793 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001794 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001795 * @param[in,out] fracdig Value to write to.
1796 * @param[in,out] exts Extension instances to add to.
1797 *
1798 * @return LY_ERR values.
1799 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001800static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001801parse_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 +02001802{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001803 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001804 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001805 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001806 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001807 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001808
1809 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001810 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811 return LY_EVALID;
1812 }
1813
1814 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001815 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816
1817 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001818 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 free(buf);
1820 return LY_EVALID;
1821 }
1822
1823 errno = 0;
1824 num = strtoul(word, &ptr, 10);
1825 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001826 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001827 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 free(buf);
1829 return LY_EVALID;
1830 }
1831 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001832 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001833 free(buf);
1834 return LY_EVALID;
1835 }
1836 *fracdig = num;
1837 free(buf);
1838
Michal Vaskod989ba02020-08-24 10:59:24 +02001839 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001841 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001842 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 break;
1844 default:
David Sedlákb3077192019-06-19 10:55:37 +02001845 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846 return LY_EVALID;
1847 }
1848 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001849 return ret;
1850}
1851
Michal Vaskoea5abea2018-09-18 13:10:54 +02001852/**
1853 * @brief Parse the require-instance statement. Substatement of type statement.
1854 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001855 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001856 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001857 * @param[in,out] reqinst Value to write to.
1858 * @param[in,out] flags Flags to write to.
1859 * @param[in,out] exts Extension instances to add to.
1860 *
1861 * @return LY_ERR values.
1862 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001863static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001864parse_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 +02001865 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001866{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001867 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001869 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001870 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871
1872 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001873 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874 return LY_EVALID;
1875 }
1876 *flags |= LYS_SET_REQINST;
1877
1878 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001879 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001880
1881 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1882 *reqinst = 1;
1883 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001884 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 free(buf);
1886 return LY_EVALID;
1887 }
1888 free(buf);
1889
Michal Vaskod989ba02020-08-24 10:59:24 +02001890 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001892 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001893 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001894 break;
1895 default:
David Sedlákb3077192019-06-19 10:55:37 +02001896 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897 return LY_EVALID;
1898 }
1899 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001900 return ret;
1901}
1902
Michal Vaskoea5abea2018-09-18 13:10:54 +02001903/**
1904 * @brief Parse the modifier statement. Substatement of type pattern statement.
1905 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001906 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001907 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001908 * @param[in,out] pat Value to write to.
1909 * @param[in,out] exts Extension instances to add to.
1910 *
1911 * @return LY_ERR values.
1912 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001913static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001914parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **pat,
Radek Krejci0f969882020-08-21 16:56:47 +02001915 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001916{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001917 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001918 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001919 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001920 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921
1922 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001923 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924 return LY_EVALID;
1925 }
1926
1927 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001928 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929
1930 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001931 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932 free(buf);
1933 return LY_EVALID;
1934 }
1935 free(buf);
1936
1937 /* replace the value in the dictionary */
1938 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001939 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001940 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001941 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001942
1943 assert(buf[0] == 0x06);
1944 buf[0] = 0x15;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001945 LY_CHECK_RET(lydict_insert_zc(ctx->ctx, buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946
Michal Vaskod989ba02020-08-24 10:59:24 +02001947 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001948 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001949 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001950 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951 break;
1952 default:
David Sedlákb3077192019-06-19 10:55:37 +02001953 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001954 return LY_EVALID;
1955 }
1956 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001957 return ret;
1958}
1959
Michal Vaskoea5abea2018-09-18 13:10:54 +02001960/**
1961 * @brief Parse the pattern statement. Substatement of type statement.
1962 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001963 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001964 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001965 * @param[in,out] patterns Restrictions to add to.
1966 *
1967 * @return LY_ERR values.
1968 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001970parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001971{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001972 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001974 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001975 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976 struct lysp_restr *restr;
1977
Radek Krejci2c4e7172018-10-19 15:56:26 +02001978 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979
1980 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001981 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001982
1983 /* add special meaning first byte */
1984 if (buf) {
1985 buf = realloc(buf, word_len + 2);
1986 word = buf;
1987 } else {
1988 buf = malloc(word_len + 2);
1989 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001990 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001991 memmove(buf + 1, word, word_len);
1992 buf[0] = 0x06; /* pattern's default regular-match flag */
1993 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko7f45cf22020-10-01 12:49:44 +02001994 LY_CHECK_RET(lydict_insert_zc(ctx->ctx, buf, &restr->arg.str));
1995 restr->arg.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001996
Michal Vaskod989ba02020-08-24 10:59:24 +02001997 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001998 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001999 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002000 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 +02002001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002002 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002003 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 +02002004 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002005 case LY_STMT_ERROR_APP_TAG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002006 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 +02002007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002008 case LY_STMT_ERROR_MESSAGE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002009 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 +02002010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002011 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002012 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002013 LY_CHECK_RET(parse_type_pattern_modifier(ctx, in, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002015 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002016 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017 break;
2018 default:
David Sedlákb3077192019-06-19 10:55:37 +02002019 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020 return LY_EVALID;
2021 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023 return ret;
2024}
2025
Michal Vaskoea5abea2018-09-18 13:10:54 +02002026/**
2027 * @brief Parse the type statement.
2028 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002029 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002030 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002031 * @param[in,out] type Type to wrote to.
2032 *
2033 * @return LY_ERR values.
2034 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002035static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002036parse_type(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002038 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002040 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002041 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002042 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043 struct lysp_type *nest_type;
2044
2045 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002046 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047 return LY_EVALID;
2048 }
2049
2050 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002051 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002052 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002054 /* set module */
2055 type->mod = ctx->main_mod;
2056
Michal Vaskod989ba02020-08-24 10:59:24 +02002057 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002059 case LY_STMT_BASE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002060 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 +01002061 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002063 case LY_STMT_BIT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002064 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002065 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002067 case LY_STMT_ENUM:
Michal Vasko63f3d842020-07-08 10:10:14 +02002068 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002069 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002071 case LY_STMT_FRACTION_DIGITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002072 LY_CHECK_RET(parse_type_fracdigits(ctx, in, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002073 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002075 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002077 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078 return LY_EVALID;
2079 }
2080 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002081 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082
Michal Vasko63f3d842020-07-08 10:10:14 +02002083 LY_CHECK_RET(parse_restr(ctx, in, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002084 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002085 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002086 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002087 if (type->path) {
2088 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2089 return LY_EVALID;
2090 }
2091
Michal Vasko63f3d842020-07-08 10:10:14 +02002092 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 +02002093 ret = ly_path_parse(ctx->ctx, NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
Michal Vasko004d3152020-06-11 19:59:22 +02002094 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
2095 lydict_remove(ctx->ctx, str_path);
2096 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002097 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002099 case LY_STMT_PATTERN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002100 LY_CHECK_RET(parse_type_pattern(ctx, in, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002101 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002103 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002104 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002105 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106 return LY_EVALID;
2107 }
2108 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002109 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002110
Michal Vasko63f3d842020-07-08 10:10:14 +02002111 LY_CHECK_RET(parse_restr(ctx, in, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002112 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002114 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002115 LY_CHECK_RET(parse_type_reqinstance(ctx, in, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002116 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002118 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002119 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02002120 LY_CHECK_RET(parse_type(ctx, in, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002121 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002123 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002124 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125 break;
2126 default:
David Sedlákb3077192019-06-19 10:55:37 +02002127 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002128 return LY_EVALID;
2129 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002131 return ret;
2132}
2133
Michal Vaskoea5abea2018-09-18 13:10:54 +02002134/**
2135 * @brief Parse the leaf statement.
2136 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002137 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002138 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002139 * @param[in,out] siblings Siblings to add to.
2140 *
2141 * @return LY_ERR values.
2142 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002143LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002144parse_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 +02002145{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002146 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002148 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002149 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002150 struct lysp_node_leaf *leaf;
2151
David Sedlák60adc092019-08-06 15:57:02 +02002152 /* create new leaf structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002153 LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002155 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156
Michal Vasko7fbc8162018-09-17 10:35:16 +02002157 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002158 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002159 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002160
2161 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002162 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002164 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002165 LY_CHECK_RET(parse_config(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002167 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002168 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
2169 leaf->dflt.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002171 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002172 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 +02002173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002174 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002175 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002177 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002178 LY_CHECK_RET(parse_mandatory(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002180 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002181 LY_CHECK_RET(parse_restrs(ctx, in, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002183 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002184 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 +02002185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002186 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002187 LY_CHECK_RET(parse_status(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002189 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002190 LY_CHECK_RET(parse_type(ctx, in, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002192 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002193 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 +02002194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002195 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002196 LY_CHECK_RET(parse_when(ctx, in, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002198 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002199 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200 break;
2201 default:
David Sedlákb3077192019-06-19 10:55:37 +02002202 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002203 return LY_EVALID;
2204 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002205 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002206 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002207checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002208 /* mandatory substatements */
2209 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002210 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211 return LY_EVALID;
2212 }
2213
2214 return ret;
2215}
2216
Michal Vaskoea5abea2018-09-18 13:10:54 +02002217/**
2218 * @brief Parse the max-elements statement.
2219 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002220 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002221 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002222 * @param[in,out] max Value to write to.
2223 * @param[in,out] flags Flags to write to.
2224 * @param[in,out] exts Extension instances to add to.
2225 *
2226 * @return LY_ERR values.
2227 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002228LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002229parse_maxelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *max, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002230 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002231{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002232 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002234 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002236 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237
2238 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002239 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240 return LY_EVALID;
2241 }
2242 *flags |= LYS_SET_MAX;
2243
2244 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002245 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002246
2247 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002248 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002249 free(buf);
2250 return LY_EVALID;
2251 }
2252
Radek Krejci7f9b6512019-09-18 13:11:09 +02002253 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254 errno = 0;
2255 num = strtoul(word, &ptr, 10);
2256 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002257 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002258 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002259 free(buf);
2260 return LY_EVALID;
2261 }
2262 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002263 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002264 free(buf);
2265 return LY_EVALID;
2266 }
2267
2268 *max = num;
2269 }
2270 free(buf);
2271
Michal Vaskod989ba02020-08-24 10:59:24 +02002272 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002273 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002274 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002275 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276 break;
2277 default:
David Sedlákb3077192019-06-19 10:55:37 +02002278 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 return LY_EVALID;
2280 }
2281 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002282 return ret;
2283}
2284
Michal Vaskoea5abea2018-09-18 13:10:54 +02002285/**
2286 * @brief Parse the min-elements statement.
2287 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002288 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002289 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002290 * @param[in,out] min Value to write to.
2291 * @param[in,out] flags Flags to write to.
2292 * @param[in,out] exts Extension instances to add to.
2293 *
2294 * @return LY_ERR values.
2295 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002296LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002297parse_minelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *min, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002298 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002300 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002302 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002303 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002304 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002305
2306 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002307 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308 return LY_EVALID;
2309 }
2310 *flags |= LYS_SET_MIN;
2311
2312 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002313 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314
2315 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002316 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317 free(buf);
2318 return LY_EVALID;
2319 }
2320
2321 errno = 0;
2322 num = strtoul(word, &ptr, 10);
2323 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002324 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002325 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326 free(buf);
2327 return LY_EVALID;
2328 }
2329 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002330 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 free(buf);
2332 return LY_EVALID;
2333 }
2334 *min = num;
2335 free(buf);
2336
Michal Vaskod989ba02020-08-24 10:59:24 +02002337 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002338 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002339 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002340 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002341 break;
2342 default:
David Sedlákb3077192019-06-19 10:55:37 +02002343 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 return LY_EVALID;
2345 }
2346 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347 return ret;
2348}
2349
Michal Vaskoea5abea2018-09-18 13:10:54 +02002350/**
2351 * @brief Parse the ordered-by statement.
2352 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002353 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002354 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002355 * @param[in,out] flags Flags to write to.
2356 * @param[in,out] exts Extension instances to add to.
2357 *
2358 * @return LY_ERR values.
2359 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002361parse_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 +02002362{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002363 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002365 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002366 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367
2368 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002369 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370 return LY_EVALID;
2371 }
2372
2373 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002374 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375
2376 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2377 *flags |= LYS_ORDBY_SYSTEM;
2378 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2379 *flags |= LYS_ORDBY_USER;
2380 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002381 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382 free(buf);
2383 return LY_EVALID;
2384 }
2385 free(buf);
2386
Michal Vaskod989ba02020-08-24 10:59:24 +02002387 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002389 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002390 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 break;
2392 default:
David Sedlákb3077192019-06-19 10:55:37 +02002393 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 return LY_EVALID;
2395 }
2396 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 return ret;
2398}
2399
Michal Vaskoea5abea2018-09-18 13:10:54 +02002400/**
2401 * @brief Parse the leaf-list statement.
2402 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002403 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002404 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002405 * @param[in,out] siblings Siblings to add to.
2406 *
2407 * @return LY_ERR values.
2408 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002409LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002410parse_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 +02002411{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002412 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002414 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002415 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416 struct lysp_node_leaflist *llist;
2417
David Sedlák60adc092019-08-06 15:57:02 +02002418 /* create new leaf-list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002419 LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002421 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002424 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002425 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426
2427 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002428 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002429 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002430 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002431 LY_CHECK_RET(parse_config(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002433 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002434 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002435 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002437 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002438 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 +02002439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002440 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002441 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002443 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002444 LY_CHECK_RET(parse_maxelements(ctx, in, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002446 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002447 LY_CHECK_RET(parse_minelements(ctx, in, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002449 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002450 LY_CHECK_RET(parse_restrs(ctx, in, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002452 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002453 LY_CHECK_RET(parse_orderedby(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002455 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002456 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 +02002457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002458 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002459 LY_CHECK_RET(parse_status(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002461 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002462 LY_CHECK_RET(parse_type(ctx, in, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002464 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002465 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 +02002466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002467 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002468 LY_CHECK_RET(parse_when(ctx, in, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002470 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002471 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002472 break;
2473 default:
David Sedlákb3077192019-06-19 10:55:37 +02002474 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 return LY_EVALID;
2476 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002478 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002479checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 /* mandatory substatements */
2481 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002482 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483 return LY_EVALID;
2484 }
2485
2486 return ret;
2487}
2488
Michal Vaskoea5abea2018-09-18 13:10:54 +02002489/**
2490 * @brief Parse the refine statement.
2491 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002492 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002493 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002494 * @param[in,out] refines Refines to add to.
2495 *
2496 * @return LY_ERR values.
2497 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002498static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002499parse_refine(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002501 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002503 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002504 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 struct lysp_refine *rf;
2506
Radek Krejci2c4e7172018-10-19 15:56:26 +02002507 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508
2509 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002510 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002511 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002512 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513
Michal Vaskod989ba02020-08-24 10:59:24 +02002514 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002515 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002516 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002517 LY_CHECK_RET(parse_config(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002519 case LY_STMT_DEFAULT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002520 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 +02002521 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002522 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002523 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 +02002524 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002525 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002526 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002527 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, 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_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002530 LY_CHECK_RET(parse_maxelements(ctx, in, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002532 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002533 LY_CHECK_RET(parse_minelements(ctx, in, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002535 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002536 LY_CHECK_RET(parse_restrs(ctx, in, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002538 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002539 LY_CHECK_RET(parse_mandatory(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002541 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002542 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 +02002543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002544 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002545 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 +02002546 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002547 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002548 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002549 break;
2550 default:
David Sedlákb3077192019-06-19 10:55:37 +02002551 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 return LY_EVALID;
2553 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 return ret;
2556}
2557
Michal Vaskoea5abea2018-09-18 13:10:54 +02002558/**
2559 * @brief Parse the typedef statement.
2560 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002561 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002562 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002563 * @param[in,out] typedefs Typedefs to add to.
2564 *
2565 * @return LY_ERR values.
2566 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002568parse_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 +02002569{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002570 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002572 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002573 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574 struct lysp_tpdf *tpdf;
2575
Radek Krejci2c4e7172018-10-19 15:56:26 +02002576 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577
2578 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002579 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002580 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581
2582 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002583 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002585 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002586 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
2587 tpdf->dflt.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002588 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002589 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002590 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 +02002591 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002592 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002593 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 +02002594 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002595 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002596 LY_CHECK_RET(parse_status(ctx, in, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002598 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002599 LY_CHECK_RET(parse_type(ctx, in, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002601 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002602 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 +02002603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002604 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002605 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 break;
2607 default:
David Sedlákb3077192019-06-19 10:55:37 +02002608 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 return LY_EVALID;
2610 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002612 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002613checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614 /* mandatory substatements */
2615 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002616 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002617 return LY_EVALID;
2618 }
2619
Radek Krejcibbe09a92018-11-08 09:36:54 +01002620 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002621 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002622 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002623 }
2624
Michal Vasko7fbc8162018-09-17 10:35:16 +02002625 return ret;
2626}
2627
Michal Vaskoea5abea2018-09-18 13:10:54 +02002628/**
2629 * @brief Parse the input or output statement.
2630 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002631 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002632 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002633 * @param[in] kw Type of this particular keyword
2634 * @param[in,out] inout_p Input/output pointer to write to.
2635 *
2636 * @return LY_ERR values.
2637 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02002639parse_inout(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt inout_kw, struct lysp_node *parent,
2640 struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002642 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002643 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002644 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002645 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002647 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002648 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649 return LY_EVALID;
2650 }
2651
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002652 /* initiate structure */
Michal Vasko22df3f02020-08-24 13:29:22 +02002653 inout_p->nodetype = &((struct lysp_action *)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002654 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655
2656 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002657 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002659 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002660 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002661 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002662 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002663 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002664 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002665 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002666 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002668 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002669 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002670 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002671 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002672 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002674 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002675 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002676 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002677 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002678 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002679 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002680 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002681 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002683 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002684 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, in, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002686 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002687 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Michal Vasko63f3d842020-07-08 10:10:14 +02002688 LY_CHECK_RET(parse_restrs(ctx, in, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002690 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002691 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002693 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002694 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695 break;
2696 default:
David Sedlákb3077192019-06-19 10:55:37 +02002697 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002698 return LY_EVALID;
2699 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002701 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002702
Radek Krejci7fc68292019-06-12 13:51:09 +02002703checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002704 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002705 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 +02002706
Michal Vaskob83af8a2020-01-06 09:49:22 +01002707 if (!inout_p->data) {
2708 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2709 return LY_EVALID;
2710 }
2711
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712 return ret;
2713}
2714
Michal Vaskoea5abea2018-09-18 13:10:54 +02002715/**
2716 * @brief Parse the action statement.
2717 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002718 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002719 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002720 * @param[in,out] actions Actions to add to.
2721 *
2722 * @return LY_ERR values.
2723 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002724LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002725parse_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 +02002726{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002727 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002729 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002730 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731 struct lysp_action *act;
2732
Radek Krejci2c4e7172018-10-19 15:56:26 +02002733 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734
2735 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002736 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002737 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002738 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002739 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002740
Michal Vasko63f3d842020-07-08 10:10:14 +02002741 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002742 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002743 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002744 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 +02002745 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002746 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002747 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002748 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002749 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002750 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 +02002751 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002752 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002753 LY_CHECK_RET(parse_status(ctx, in, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002754 break;
2755
Radek Krejcid6b76452019-09-03 17:03:03 +02002756 case LY_STMT_INPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002757 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002759 case LY_STMT_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002760 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 break;
2762
Radek Krejcid6b76452019-09-03 17:03:03 +02002763 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002764 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, in, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002766 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002767 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002768 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002769 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002770 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002771 break;
2772 default:
David Sedlákb3077192019-06-19 10:55:37 +02002773 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002774 return LY_EVALID;
2775 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002777 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002778
2779 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2780 if (!act->input.nodetype) {
2781 act->input.nodetype = LYS_INPUT;
2782 act->input.parent = (struct lysp_node *)act;
2783 }
2784 if (!act->output.nodetype) {
2785 act->output.nodetype = LYS_OUTPUT;
2786 act->output.parent = (struct lysp_node *)act;
2787 }
2788
Radek Krejci7fc68292019-06-12 13:51:09 +02002789checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002790 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002791 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002792
Michal Vasko7fbc8162018-09-17 10:35:16 +02002793 return ret;
2794}
2795
Michal Vaskoea5abea2018-09-18 13:10:54 +02002796/**
2797 * @brief Parse the notification statement.
2798 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002799 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002800 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002801 * @param[in,out] notifs Notifications to add to.
2802 *
2803 * @return LY_ERR values.
2804 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002805LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002806parse_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 +02002807{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002808 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002809 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002810 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002811 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002812 struct lysp_notif *notif;
2813
Radek Krejci2c4e7172018-10-19 15:56:26 +02002814 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002815
2816 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002817 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002818 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002819 notif->nodetype = LYS_NOTIF;
2820 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002821
Michal Vasko63f3d842020-07-08 10:10:14 +02002822 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002824 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002825 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 +02002826 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002827 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002828 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002829 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002830 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002831 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 +02002832 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002833 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002834 LY_CHECK_RET(parse_status(ctx, in, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002835 break;
2836
Radek Krejcid6b76452019-09-03 17:03:03 +02002837 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002838 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002839 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002840 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002841 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002843 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002844 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002846 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002847 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002849 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002850 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002852 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002853 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002855 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002856 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002858 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002859 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002860 break;
2861
Radek Krejcid6b76452019-09-03 17:03:03 +02002862 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002863 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Michal Vasko63f3d842020-07-08 10:10:14 +02002864 LY_CHECK_RET(parse_restrs(ctx, in, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002866 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002867 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, in, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002868 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002869 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002870 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002871 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002872 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002873 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002874 break;
2875 default:
David Sedlákb3077192019-06-19 10:55:37 +02002876 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002877 return LY_EVALID;
2878 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002879 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002880 LY_CHECK_RET(ret);
2881checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002882 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002883 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002884
Michal Vasko7fbc8162018-09-17 10:35:16 +02002885 return ret;
2886}
2887
Michal Vaskoea5abea2018-09-18 13:10:54 +02002888/**
2889 * @brief Parse the grouping statement.
2890 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002891 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002892 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002893 * @param[in,out] groupings Groupings to add to.
2894 *
2895 * @return LY_ERR values.
2896 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002897LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002898parse_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 +02002899{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002900 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002902 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002903 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 struct lysp_grp *grp;
2905
Radek Krejci2c4e7172018-10-19 15:56:26 +02002906 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907
2908 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002909 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002910 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002911 grp->nodetype = LYS_GROUPING;
2912 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913
Michal Vasko63f3d842020-07-08 10:10:14 +02002914 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002916 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002917 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 +02002918 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002919 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002920 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 +02002921 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002922 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002923 LY_CHECK_RET(parse_status(ctx, in, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002924 break;
2925
Radek Krejcid6b76452019-09-03 17:03:03 +02002926 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002927 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002928 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002929 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002930 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002932 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002933 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002934 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002935 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002936 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002938 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002939 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002940 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002941 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002942 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002944 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002945 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002947 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002948 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949 break;
2950
Radek Krejcid6b76452019-09-03 17:03:03 +02002951 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002952 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)grp, in, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002953 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002954 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002955 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Michal Vasko22df3f02020-08-24 13:29:22 +02002956 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002958 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002959 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002961 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002962 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Michal Vasko22df3f02020-08-24 13:29:22 +02002963 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002965 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002966 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967 break;
2968 default:
David Sedlákb3077192019-06-19 10:55:37 +02002969 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970 return LY_EVALID;
2971 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002972 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002973 LY_CHECK_RET(ret);
2974checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002975 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002976 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 +02002977
Michal Vasko7fbc8162018-09-17 10:35:16 +02002978 return ret;
2979}
2980
Michal Vaskoea5abea2018-09-18 13:10:54 +02002981/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002982 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002983 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002984 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002985 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002986 * @param[in,out] augments Augments to add to.
2987 *
2988 * @return LY_ERR values.
2989 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002990LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002991parse_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 +02002992{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002993 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002995 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002996 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997 struct lysp_augment *aug;
2998
Radek Krejci2c4e7172018-10-19 15:56:26 +02002999 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000
3001 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003002 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003003 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003004 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003005 aug->nodetype = LYS_AUGMENT;
3006 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003007
Michal Vasko63f3d842020-07-08 10:10:14 +02003008 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003010 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003011 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 +02003012 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003013 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003014 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003016 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003017 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 +02003018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003019 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003020 LY_CHECK_RET(parse_status(ctx, in, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003022 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003023 LY_CHECK_RET(parse_when(ctx, in, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 break;
3025
Radek Krejcid6b76452019-09-03 17:03:03 +02003026 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003027 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003028 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003029 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003030 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003031 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003032 case LY_STMT_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003033 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003034 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003035 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003036 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003037 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003038 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003039 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003041 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003042 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003044 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003045 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003046 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003047 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003048 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003050 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003051 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 break;
3053
Radek Krejcid6b76452019-09-03 17:03:03 +02003054 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003055 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Michal Vasko22df3f02020-08-24 13:29:22 +02003056 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003057 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003058 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003059 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Michal Vasko22df3f02020-08-24 13:29:22 +02003060 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003062 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003063 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064 break;
3065 default:
David Sedlákb3077192019-06-19 10:55:37 +02003066 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003067 return LY_EVALID;
3068 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003070 LY_CHECK_RET(ret);
3071checks:
3072 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003073 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 +02003074
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 return ret;
3076}
3077
Michal Vaskoea5abea2018-09-18 13:10:54 +02003078/**
3079 * @brief Parse the uses statement.
3080 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003081 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003082 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003083 * @param[in,out] siblings Siblings to add to.
3084 *
3085 * @return LY_ERR values.
3086 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003087LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003088parse_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 +02003089{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003090 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003091 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003092 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003093 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003094 struct lysp_node_uses *uses;
3095
David Sedlák60adc092019-08-06 15:57:02 +02003096 /* create uses structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003097 LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003099 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003100
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003102 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003103 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104
3105 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003106 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003108 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003109 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 +02003110 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003111 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003112 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003114 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003115 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 +02003116 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003117 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003118 LY_CHECK_RET(parse_status(ctx, in, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003120 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003121 LY_CHECK_RET(parse_when(ctx, in, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 break;
3123
Radek Krejcid6b76452019-09-03 17:03:03 +02003124 case LY_STMT_REFINE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003125 LY_CHECK_RET(parse_refine(ctx, in, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003127 case LY_STMT_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02003128 LY_CHECK_RET(parse_augment(ctx, in, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003130 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003131 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132 break;
3133 default:
David Sedlákb3077192019-06-19 10:55:37 +02003134 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 return LY_EVALID;
3136 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003138checks:
3139 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003140 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003141
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 return ret;
3143}
3144
Michal Vaskoea5abea2018-09-18 13:10:54 +02003145/**
3146 * @brief Parse the case statement.
3147 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003148 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003149 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003150 * @param[in,out] siblings Siblings to add to.
3151 *
3152 * @return LY_ERR values.
3153 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003154LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003155parse_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 +02003156{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003157 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003159 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003160 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161 struct lysp_node_case *cas;
3162
David Sedlák60adc092019-08-06 15:57:02 +02003163 /* create new case structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003164 LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003166 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167
Michal Vasko7fbc8162018-09-17 10:35:16 +02003168 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003169 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003170 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171
3172 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02003173 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003175 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003176 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 +02003177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003178 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003179 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003181 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003182 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 +02003183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003184 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003185 LY_CHECK_RET(parse_status(ctx, in, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003187 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003188 LY_CHECK_RET(parse_when(ctx, in, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003189 break;
3190
Radek Krejcid6b76452019-09-03 17:03:03 +02003191 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003192 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003193 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003194 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003195 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003197 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003198 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003200 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003201 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003203 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003204 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003206 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003207 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003209 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003210 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003212 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003213 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003216 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 break;
3218 default:
David Sedlákb3077192019-06-19 10:55:37 +02003219 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 return LY_EVALID;
3221 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223 return ret;
3224}
3225
Michal Vaskoea5abea2018-09-18 13:10:54 +02003226/**
3227 * @brief Parse the choice statement.
3228 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003229 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003230 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003231 * @param[in,out] siblings Siblings to add to.
3232 *
3233 * @return LY_ERR values.
3234 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003235LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003236parse_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 +02003237{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003238 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003240 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003241 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003242 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243
David Sedlák60adc092019-08-06 15:57:02 +02003244 /* create new choice structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003245 LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003246 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003247 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003250 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003251 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252
3253 /* parse substatements */
Michal Vasko7f45cf22020-10-01 12:49:44 +02003254 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003256 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003257 LY_CHECK_RET(parse_config(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003258 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003259 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003260 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 +02003261 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003262 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003263 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003265 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003266 LY_CHECK_RET(parse_mandatory(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003268 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003269 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 +02003270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003271 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003272 LY_CHECK_RET(parse_status(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003274 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003275 LY_CHECK_RET(parse_when(ctx, in, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003277 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003278 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
3279 &choice->exts));
3280 choice->dflt.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003281 break;
3282
Radek Krejcid6b76452019-09-03 17:03:03 +02003283 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003284 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003285 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003286 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003287 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003289 case LY_STMT_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003290 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003291 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003292 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003293 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Michal Vasko22df3f02020-08-24 13:29:22 +02003294 LY_CHECK_RET(parse_choice(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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003297 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003298 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003299 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003300 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003302 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003303 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003305 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003306 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003308 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003309 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310 break;
3311 default:
David Sedlákb3077192019-06-19 10:55:37 +02003312 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313 return LY_EVALID;
3314 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316 return ret;
3317}
3318
Michal Vaskoea5abea2018-09-18 13:10:54 +02003319/**
3320 * @brief Parse the container statement.
3321 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003322 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003323 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003324 * @param[in,out] siblings Siblings to add to.
3325 *
3326 * @return LY_ERR values.
3327 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003328LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003329parse_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 +02003330{
3331 LY_ERR ret = 0;
3332 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003333 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003334 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 struct lysp_node_container *cont;
3336
David Sedlák60adc092019-08-06 15:57:02 +02003337 /* create new container structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003338 LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003340 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003343 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003344 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345
3346 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003347 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003348 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003349 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003350 LY_CHECK_RET(parse_config(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003352 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003353 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 +02003354 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003355 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003356 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003358 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003359 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 +02003360 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003361 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003362 LY_CHECK_RET(parse_status(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003364 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003365 LY_CHECK_RET(parse_when(ctx, in, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003367 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003368 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 +02003369 break;
3370
Radek Krejcid6b76452019-09-03 17:03:03 +02003371 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003372 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003373 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003374 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003375 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003377 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003378 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003380 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003381 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003383 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003384 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003386 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003387 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003389 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003390 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003392 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003393 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394 break;
3395
Radek Krejcid6b76452019-09-03 17:03:03 +02003396 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003397 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, in, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003399 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003400 LY_CHECK_RET(parse_restrs(ctx, in, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003402 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003403 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Michal Vasko22df3f02020-08-24 13:29:22 +02003404 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003406 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02003407 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003409 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003410 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Michal Vasko22df3f02020-08-24 13:29:22 +02003411 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003414 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 break;
3416 default:
David Sedlákb3077192019-06-19 10:55:37 +02003417 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 return LY_EVALID;
3419 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003421checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003422 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003423 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 +02003424 return ret;
3425}
3426
Michal Vaskoea5abea2018-09-18 13:10:54 +02003427/**
3428 * @brief Parse the list statement.
3429 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003430 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003431 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003432 * @param[in,out] siblings Siblings to add to.
3433 *
3434 * @return LY_ERR values.
3435 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003436LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003437parse_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 +02003438{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003439 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003440 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003441 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003442 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443 struct lysp_node_list *list;
3444
David Sedlák60adc092019-08-06 15:57:02 +02003445 /* create new list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003446 LY_LIST_NEW_RET(ctx->ctx, siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003448 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449
Michal Vasko7fbc8162018-09-17 10:35:16 +02003450 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003451 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003452 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003453
3454 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003455 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003457 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003458 LY_CHECK_RET(parse_config(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003461 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 +02003462 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003463 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003464 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003466 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003467 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 +02003468 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003469 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003470 LY_CHECK_RET(parse_status(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003472 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003473 LY_CHECK_RET(parse_when(ctx, in, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003475 case LY_STMT_KEY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003476 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 +02003477 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003478 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003479 LY_CHECK_RET(parse_maxelements(ctx, in, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003481 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003482 LY_CHECK_RET(parse_minelements(ctx, in, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003484 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003485 LY_CHECK_RET(parse_orderedby(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003487 case LY_STMT_UNIQUE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003488 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489 break;
3490
Radek Krejcid6b76452019-09-03 17:03:03 +02003491 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003492 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003493 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003494 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003495 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003497 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003498 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003500 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003501 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003502 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003503 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003504 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003506 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003507 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003509 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003510 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003512 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003513 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003514 break;
3515
Radek Krejcid6b76452019-09-03 17:03:03 +02003516 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003517 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, in, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003519 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003520 LY_CHECK_RET(parse_restrs(ctx, in, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003522 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003523 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Michal Vasko22df3f02020-08-24 13:29:22 +02003524 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02003527 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003529 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003530 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Michal Vasko22df3f02020-08-24 13:29:22 +02003531 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003533 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003534 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535 break;
3536 default:
David Sedlákb3077192019-06-19 10:55:37 +02003537 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538 return LY_EVALID;
3539 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003541 LY_CHECK_RET(ret);
3542checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003543 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003544 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 +02003545
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 return ret;
3547}
3548
Michal Vaskoea5abea2018-09-18 13:10:54 +02003549/**
3550 * @brief Parse the yin-element statement.
3551 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003552 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003553 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003554 * @param[in,out] flags Flags to write to.
3555 * @param[in,out] exts Extension instances to add to.
3556 *
3557 * @return LY_ERR values.
3558 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003559static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003560parse_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 +02003561{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003562 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003564 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003565 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566
3567 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003568 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569 return LY_EVALID;
3570 }
3571
3572 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003573 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574
3575 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3576 *flags |= LYS_YINELEM_TRUE;
3577 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3578 *flags |= LYS_YINELEM_FALSE;
3579 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003580 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581 free(buf);
3582 return LY_EVALID;
3583 }
3584 free(buf);
3585
Michal Vaskod989ba02020-08-24 10:59:24 +02003586 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003588 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003589 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003590 LY_CHECK_RET(ret);
3591 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 default:
David Sedlákb3077192019-06-19 10:55:37 +02003593 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 return LY_EVALID;
3595 }
3596 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 return ret;
3598}
3599
Michal Vaskoea5abea2018-09-18 13:10:54 +02003600/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003601 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003602 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003603 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003604 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003605 * @param[in,out] argument Value to write to.
3606 * @param[in,out] flags Flags to write to.
3607 * @param[in,out] exts Extension instances to add to.
3608 *
3609 * @return LY_ERR values.
3610 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003611static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003612parse_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 +02003613{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003614 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003616 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003617 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618
3619 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003620 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621 return LY_EVALID;
3622 }
3623
3624 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003625 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003626 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627
Michal Vaskod989ba02020-08-24 10:59:24 +02003628 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003630 case LY_STMT_YIN_ELEMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003631 LY_CHECK_RET(parse_yinelement(ctx, in, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003633 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003634 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 break;
3636 default:
David Sedlákb3077192019-06-19 10:55:37 +02003637 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 return LY_EVALID;
3639 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 return ret;
3642}
3643
Michal Vaskoea5abea2018-09-18 13:10:54 +02003644/**
3645 * @brief Parse the extension statement.
3646 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003647 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003648 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003649 * @param[in,out] extensions Extensions to add to.
3650 *
3651 * @return LY_ERR values.
3652 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003654parse_extension(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003655{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003656 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003658 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003659 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660 struct lysp_ext *ex;
3661
Radek Krejci2c4e7172018-10-19 15:56:26 +02003662 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003663
3664 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003665 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003666 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667
Michal Vaskod989ba02020-08-24 10:59:24 +02003668 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003670 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003671 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 +02003672 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003673 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003674 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 +02003675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003676 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003677 LY_CHECK_RET(parse_status(ctx, in, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003679 case LY_STMT_ARGUMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003680 LY_CHECK_RET(parse_argument(ctx, in, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003682 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003683 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003684 break;
3685 default:
David Sedlákb3077192019-06-19 10:55:37 +02003686 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687 return LY_EVALID;
3688 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003689 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 return ret;
3691}
3692
Michal Vaskoea5abea2018-09-18 13:10:54 +02003693/**
3694 * @brief Parse the deviate statement.
3695 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003696 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003697 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003698 * @param[in,out] deviates Deviates to add to.
3699 *
3700 * @return LY_ERR values.
3701 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003702LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003703parse_deviate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003705 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003706 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003707 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003708 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003709 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003710 struct lysp_deviate_add *d_add = NULL;
3711 struct lysp_deviate_rpl *d_rpl = NULL;
3712 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003713 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3714 struct lysp_restr **d_musts = NULL;
3715 uint16_t *d_flags = 0;
3716 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717
3718 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003719 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720
3721 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3722 dev_mod = LYS_DEV_NOT_SUPPORTED;
3723 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3724 dev_mod = LYS_DEV_ADD;
3725 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3726 dev_mod = LYS_DEV_REPLACE;
3727 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3728 dev_mod = LYS_DEV_DELETE;
3729 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003730 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003731 free(buf);
3732 return LY_EVALID;
3733 }
3734 free(buf);
3735
3736 /* create structure */
3737 switch (dev_mod) {
3738 case LYS_DEV_NOT_SUPPORTED:
3739 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003740 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003741 break;
3742 case LYS_DEV_ADD:
3743 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003744 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 d = (struct lysp_deviate *)d_add;
3746 d_units = &d_add->units;
3747 d_uniques = &d_add->uniques;
3748 d_dflts = &d_add->dflts;
3749 d_musts = &d_add->musts;
3750 d_flags = &d_add->flags;
3751 d_min = &d_add->min;
3752 d_max = &d_add->max;
3753 break;
3754 case LYS_DEV_REPLACE:
3755 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003756 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 d = (struct lysp_deviate *)d_rpl;
3758 d_units = &d_rpl->units;
3759 d_flags = &d_rpl->flags;
3760 d_min = &d_rpl->min;
3761 d_max = &d_rpl->max;
3762 break;
3763 case LYS_DEV_DELETE:
3764 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003765 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766 d = (struct lysp_deviate *)d_del;
3767 d_units = &d_del->units;
3768 d_uniques = &d_del->uniques;
3769 d_dflts = &d_del->dflts;
3770 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003771 break;
3772 default:
3773 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003774 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 }
3776 d->mod = dev_mod;
3777
3778 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003779 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003780
Michal Vaskod989ba02020-08-24 10:59:24 +02003781 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003782 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003783 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003784 switch (dev_mod) {
3785 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003786 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003787 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 return LY_EVALID;
3789 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003790 LY_CHECK_RET(parse_config(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791 break;
3792 }
3793 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003794 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 switch (dev_mod) {
3796 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003797 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798 return LY_EVALID;
3799 case LYS_DEV_REPLACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003800 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 +02003801 break;
3802 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003803 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 +02003804 break;
3805 }
3806 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003807 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 switch (dev_mod) {
3809 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003810 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003811 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 return LY_EVALID;
3813 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003814 LY_CHECK_RET(parse_mandatory(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003815 break;
3816 }
3817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003818 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 switch (dev_mod) {
3820 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003821 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003822 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 return LY_EVALID;
3824 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003825 LY_CHECK_RET(parse_maxelements(ctx, in, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 break;
3827 }
3828 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003829 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 switch (dev_mod) {
3831 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003832 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003833 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 return LY_EVALID;
3835 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003836 LY_CHECK_RET(parse_minelements(ctx, in, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003837 break;
3838 }
3839 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003840 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 switch (dev_mod) {
3842 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003843 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003844 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845 return LY_EVALID;
3846 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003847 LY_CHECK_RET(parse_restrs(ctx, in, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 break;
3849 }
3850 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003851 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 switch (dev_mod) {
3853 case LYS_DEV_NOT_SUPPORTED:
3854 case LYS_DEV_ADD:
3855 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003856 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003857 return LY_EVALID;
3858 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003859 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003860 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003861 return LY_EVALID;
3862 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003864 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02003865 LY_CHECK_RET(parse_type(ctx, in, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 break;
3867 }
3868 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003869 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 switch (dev_mod) {
3871 case LYS_DEV_NOT_SUPPORTED:
3872 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003873 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874 return LY_EVALID;
3875 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003876 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 +02003877 break;
3878 }
3879 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003880 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 switch (dev_mod) {
3882 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003883 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 return LY_EVALID;
3885 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003886 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 +02003887 break;
3888 }
3889 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003890 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003891 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 break;
3893 default:
David Sedlákb3077192019-06-19 10:55:37 +02003894 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 return LY_EVALID;
3896 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003897 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 return ret;
3899}
3900
Michal Vaskoea5abea2018-09-18 13:10:54 +02003901/**
3902 * @brief Parse the deviation statement.
3903 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003904 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003905 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003906 * @param[in,out] deviations Deviations to add to.
3907 *
3908 * @return LY_ERR values.
3909 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003910LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003911parse_deviation(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003913 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003915 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003916 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917 struct lysp_deviation *dev;
3918
Radek Krejci2c4e7172018-10-19 15:56:26 +02003919 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003920
3921 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003922 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003923 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003924 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925
Michal Vasko63f3d842020-07-08 10:10:14 +02003926 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003927 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003928 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003929 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 +02003930 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003931 case LY_STMT_DEVIATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003932 LY_CHECK_RET(parse_deviate(ctx, in, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003934 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003935 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 +02003936 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003937 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003938 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939 break;
3940 default:
David Sedlákb3077192019-06-19 10:55:37 +02003941 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942 return LY_EVALID;
3943 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003945 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003946checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 /* mandatory substatements */
3948 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003949 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 return LY_EVALID;
3951 }
3952
3953 return ret;
3954}
3955
Michal Vaskoea5abea2018-09-18 13:10:54 +02003956/**
3957 * @brief Parse the feature statement.
3958 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003959 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003960 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003961 * @param[in,out] features Features to add to.
3962 *
3963 * @return LY_ERR values.
3964 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003965LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003966parse_feature(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003968 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003970 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003971 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003972 struct lysp_feature *feat;
3973
Radek Krejci2c4e7172018-10-19 15:56:26 +02003974 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003975
3976 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003977 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003978 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003979
Michal Vaskod989ba02020-08-24 10:59:24 +02003980 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003982 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003983 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 +02003984 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003985 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003986 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003988 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003989 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 +02003990 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003991 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003992 LY_CHECK_RET(parse_status(ctx, in, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003994 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003995 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 break;
3997 default:
David Sedlákb3077192019-06-19 10:55:37 +02003998 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003999 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004002 return ret;
4003}
4004
Michal Vaskoea5abea2018-09-18 13:10:54 +02004005/**
4006 * @brief Parse the identity statement.
4007 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004008 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004009 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004010 * @param[in,out] identities Identities to add to.
4011 *
4012 * @return LY_ERR values.
4013 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004014LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004015parse_identity(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004017 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004018 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004019 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004020 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004021 struct lysp_ident *ident;
4022
Radek Krejci2c4e7172018-10-19 15:56:26 +02004023 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024
4025 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02004026 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004027 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004028
Michal Vaskod989ba02020-08-24 10:59:24 +02004029 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004030 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004031 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004032 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 +02004033 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004034 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004035 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Michal Vasko7f45cf22020-10-01 12:49:44 +02004036 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004039 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 +02004040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004041 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02004042 LY_CHECK_RET(parse_status(ctx, in, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004044 case LY_STMT_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004045 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02004046 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 +01004047 return LY_EVALID;
4048 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004049 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 +02004050 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004051 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004052 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 break;
4054 default:
David Sedlákb3077192019-06-19 10:55:37 +02004055 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 return LY_EVALID;
4057 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004058 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059 return ret;
4060}
4061
Michal Vaskoea5abea2018-09-18 13:10:54 +02004062/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004063 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004064 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004065 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004066 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004067 * @param[in,out] mod Module to write to.
4068 *
4069 * @return LY_ERR values.
4070 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004071LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004072parse_module(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073{
4074 LY_ERR ret = 0;
4075 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004076 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004077 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004079 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004080
4081 /* (sub)module name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004082 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004083 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004084
Michal Vasko63f3d842020-07-08 10:10:14 +02004085 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004086
Radek Krejcie3846472018-10-15 15:24:51 +02004087#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004088 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 +02004089
Michal Vasko7fbc8162018-09-17 10:35:16 +02004090 switch (kw) {
4091 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004092 case LY_STMT_NAMESPACE:
4093 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004094 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004096 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004097 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 break;
4099 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004100 case LY_STMT_INCLUDE:
4101 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004102 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004103 break;
4104 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004105 case LY_STMT_ORGANIZATION:
4106 case LY_STMT_CONTACT:
4107 case LY_STMT_DESCRIPTION:
4108 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004109 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004110 break;
4111
4112 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004113 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004114 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004116 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004117 case LY_STMT_ANYDATA:
4118 case LY_STMT_ANYXML:
4119 case LY_STMT_AUGMENT:
4120 case LY_STMT_CHOICE:
4121 case LY_STMT_CONTAINER:
4122 case LY_STMT_DEVIATION:
4123 case LY_STMT_EXTENSION:
4124 case LY_STMT_FEATURE:
4125 case LY_STMT_GROUPING:
4126 case LY_STMT_IDENTITY:
4127 case LY_STMT_LEAF:
4128 case LY_STMT_LEAF_LIST:
4129 case LY_STMT_LIST:
4130 case LY_STMT_NOTIFICATION:
4131 case LY_STMT_RPC:
4132 case LY_STMT_TYPEDEF:
4133 case LY_STMT_USES:
4134 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004135 mod_stmt = Y_MOD_BODY;
4136 break;
4137 default:
4138 /* error handled in the next switch */
4139 break;
4140 }
Radek Krejcie3846472018-10-15 15:24:51 +02004141#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004142
Radek Krejcie3846472018-10-15 15:24:51 +02004143 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004144 switch (kw) {
4145 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004146 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004147 LY_CHECK_RET(parse_yangversion(ctx, in, &mod->mod->version, &mod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004148 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004150 case LY_STMT_NAMESPACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004151 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 +02004152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004153 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02004154 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 +02004155 break;
4156
4157 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004158 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004159 LY_CHECK_RET(parse_include(ctx, mod->mod->name, in, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004161 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004162 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, in, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 break;
4164
4165 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004167 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 +02004168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004169 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004170 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 +02004171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004172 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004173 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 +02004174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004175 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004176 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 +02004177 break;
4178
4179 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004180 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004181 LY_CHECK_RET(parse_revision(ctx, in, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004182 break;
4183
4184 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004186 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004187 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004189 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004191 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004192 LY_CHECK_RET(parse_choice(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004194 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004195 LY_CHECK_RET(parse_container(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004197 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004198 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004201 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004204 LY_CHECK_RET(parse_list(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_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004207 LY_CHECK_RET(parse_uses(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004208 break;
4209
Radek Krejcid6b76452019-09-03 17:03:03 +02004210 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004211 LY_CHECK_RET(parse_augment(ctx, in, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004213 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004214 LY_CHECK_RET(parse_deviation(ctx, in, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004216 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004217 LY_CHECK_RET(parse_extension(ctx, in, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004219 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004220 LY_CHECK_RET(parse_feature(ctx, in, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004222 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004223 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004225 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004226 LY_CHECK_RET(parse_identity(ctx, in, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004228 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004229 LY_CHECK_RET(parse_notif(ctx, in, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004231 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004232 LY_CHECK_RET(parse_action(ctx, in, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004234 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004235 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004237 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004238 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004239 break;
4240
4241 default:
David Sedlákb3077192019-06-19 10:55:37 +02004242 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004243 return LY_EVALID;
4244 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004245 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004246 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004247
Radek Krejci6d6556c2018-11-08 09:37:45 +01004248checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004249 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004250 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 +01004251
Michal Vasko7fbc8162018-09-17 10:35:16 +02004252 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004253 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004254 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004255 return LY_EVALID;
4256 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004257 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004258 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004259 }
4260
Radek Krejcie9e987e2018-10-31 12:50:27 +01004261 /* submodules share the namespace with the module names, so there must not be
4262 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004263 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4264 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004265 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 +01004266 return LY_EVALID;
4267 }
4268
4269 return ret;
4270}
4271
4272/**
4273 * @brief Parse submodule substatements.
4274 *
4275 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004276 * @param[in,out] in Input structure.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004277 * @param[out] submod Parsed submodule structure.
4278 *
4279 * @return LY_ERR values.
4280 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004281LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004282parse_submodule(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004283{
4284 LY_ERR ret = 0;
4285 char *buf, *word;
4286 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004287 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004288 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4289 struct lysp_submodule *dup;
4290
4291 /* submodule name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004292 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004293 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004294
Michal Vasko63f3d842020-07-08 10:10:14 +02004295 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004296
4297#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004298 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 +01004299
4300 switch (kw) {
4301 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004302 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004303 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4304 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004305 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004306 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4307 break;
4308 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004309 case LY_STMT_INCLUDE:
4310 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004311 CHECK_ORDER(Y_MOD_LINKAGE);
4312 break;
4313 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004314 case LY_STMT_ORGANIZATION:
4315 case LY_STMT_CONTACT:
4316 case LY_STMT_DESCRIPTION:
4317 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004318 CHECK_ORDER(Y_MOD_META);
4319 break;
4320
4321 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004322 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004323 CHECK_ORDER(Y_MOD_REVISION);
4324 break;
4325 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004326 case LY_STMT_ANYDATA:
4327 case LY_STMT_ANYXML:
4328 case LY_STMT_AUGMENT:
4329 case LY_STMT_CHOICE:
4330 case LY_STMT_CONTAINER:
4331 case LY_STMT_DEVIATION:
4332 case LY_STMT_EXTENSION:
4333 case LY_STMT_FEATURE:
4334 case LY_STMT_GROUPING:
4335 case LY_STMT_IDENTITY:
4336 case LY_STMT_LEAF:
4337 case LY_STMT_LEAF_LIST:
4338 case LY_STMT_LIST:
4339 case LY_STMT_NOTIFICATION:
4340 case LY_STMT_RPC:
4341 case LY_STMT_TYPEDEF:
4342 case LY_STMT_USES:
4343 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004344 mod_stmt = Y_MOD_BODY;
4345 break;
4346 default:
4347 /* error handled in the next switch */
4348 break;
4349 }
4350#undef CHECK_ORDER
4351
4352 prev_kw = kw;
4353 switch (kw) {
4354 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004355 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004356 LY_CHECK_RET(parse_yangversion(ctx, in, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004357 ctx->mod_version = submod->version;
4358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004359 case LY_STMT_BELONGS_TO:
Michal Vasko63f3d842020-07-08 10:10:14 +02004360 LY_CHECK_RET(parse_belongsto(ctx, in, &submod->belongsto, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004361 break;
4362
4363 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004364 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004365 LY_CHECK_RET(parse_include(ctx, submod->name, in, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004367 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004368 LY_CHECK_RET(parse_import(ctx, submod->prefix, in, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004369 break;
4370
4371 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004373 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 +01004374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004376 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 +01004377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004379 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 +01004380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004381 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004382 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 +01004383 break;
4384
4385 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004386 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004387 LY_CHECK_RET(parse_revision(ctx, in, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004388 break;
4389
4390 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004392 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004393 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004395 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004397 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004398 LY_CHECK_RET(parse_choice(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004399 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004400 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004401 LY_CHECK_RET(parse_container(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004403 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004404 LY_CHECK_RET(parse_leaf(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004406 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004407 LY_CHECK_RET(parse_leaflist(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004409 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004410 LY_CHECK_RET(parse_list(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004412 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004413 LY_CHECK_RET(parse_uses(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004414 break;
4415
Radek Krejcid6b76452019-09-03 17:03:03 +02004416 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004417 LY_CHECK_RET(parse_augment(ctx, in, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004419 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004420 LY_CHECK_RET(parse_deviation(ctx, in, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004422 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004423 LY_CHECK_RET(parse_extension(ctx, in, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004425 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004426 LY_CHECK_RET(parse_feature(ctx, in, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004428 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004429 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004431 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004432 LY_CHECK_RET(parse_identity(ctx, in, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004434 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004435 LY_CHECK_RET(parse_notif(ctx, in, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004437 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004438 LY_CHECK_RET(parse_action(ctx, in, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004440 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004441 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004443 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004444 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004445 break;
4446
4447 default:
David Sedlákb3077192019-06-19 10:55:37 +02004448 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004449 return LY_EVALID;
4450 }
4451 }
4452 LY_CHECK_RET(ret);
4453
4454checks:
4455 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004456 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments,
4457 submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004458
4459 /* mandatory substatements */
4460 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004461 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004462 return LY_EVALID;
4463 }
4464
4465 /* submodules share the namespace with the module names, so there must not be
4466 * a submodule of the same name in the context, no need for revision matching */
4467 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4468 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004469 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004470 return LY_EVALID;
4471 }
4472
Michal Vasko7fbc8162018-09-17 10:35:16 +02004473 return ret;
4474}
4475
Radek Krejcid4557c62018-09-17 11:42:09 +02004476LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004477yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004478 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004479{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004480 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004481 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004482 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004483 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004484 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004485
David Sedlák1b623122019-08-05 15:27:49 +02004486 /* create context */
4487 *context = calloc(1, sizeof **context);
4488 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004489 (*context)->format = LYS_IN_YANG;
Michal Vasko7c8439f2020-08-05 13:25:19 +02004490 (*context)->main_mod = main_ctx->main_mod;
David Sedlák1b623122019-08-05 15:27:49 +02004491 (*context)->ctx = ly_ctx;
Radek Krejci99435242019-09-05 16:19:15 +02004492 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004493 (*context)->line = 1;
4494
4495 /* map the typedefs and groupings list from main context to the submodule's context */
4496 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4497 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4498
Michal Vasko7fbc8162018-09-17 10:35:16 +02004499 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004500 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004501 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004502
Radek Krejcid6b76452019-09-03 17:03:03 +02004503 if (kw == LY_STMT_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004504 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004505 ret = LY_EINVAL;
4506 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004507 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004508 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004509 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004510 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004511 }
4512
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004513 mod_p = calloc(1, sizeof *mod_p);
Radek Krejcif027df72020-09-15 13:00:28 +02004514 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx); ret = LY_EMEM, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004515 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004516
4517 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004518 ret = parse_submodule(*context, in, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004519 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004520
4521 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004522 while (isspace(in->current[0])) {
4523 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004524 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004525 if (in->current[0]) {
4526 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004527 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004528 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004529 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004530
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004531 mod_p->parsing = 0;
4532 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004533
Radek Krejcibbe09a92018-11-08 09:36:54 +01004534cleanup:
4535 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004536 lysp_submodule_free((*context)->ctx, mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004537 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004538 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004539 }
4540
4541 return ret;
4542}
4543
4544LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004545yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004546{
4547 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004548 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004549 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004550 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004551 struct lysp_module *mod_p = NULL;
4552
David Sedlák1b623122019-08-05 15:27:49 +02004553 /* create context */
4554 *context = calloc(1, sizeof **context);
4555 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004556 (*context)->format = LYS_IN_YANG;
Michal Vasko7c8439f2020-08-05 13:25:19 +02004557 (*context)->main_mod = mod;
David Sedlák1b623122019-08-05 15:27:49 +02004558 (*context)->ctx = mod->ctx;
Radek Krejci335332a2019-09-05 13:03:35 +02004559 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004560 (*context)->line = 1;
4561
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004562 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004563 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004564 LY_CHECK_GOTO(ret, cleanup);
4565
Radek Krejcid6b76452019-09-03 17:03:03 +02004566 if (kw == LY_STMT_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004567 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 +01004568 ret = LY_EINVAL;
4569 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004570 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004571 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004572 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004573 goto cleanup;
4574 }
4575
4576 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004577 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004578 mod_p->mod = mod;
4579 mod_p->parsing = 1;
4580
4581 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004582 ret = parse_module(*context, in, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004583 LY_CHECK_GOTO(ret, cleanup);
4584
4585 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004586 while (isspace(in->current[0])) {
4587 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004588 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004589 if (in->current[0]) {
4590 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004591 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004592 goto cleanup;
4593 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004594
4595 mod_p->parsing = 0;
4596 mod->parsed = mod_p;
4597
4598cleanup:
4599 if (ret) {
4600 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004601 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004602 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004603 }
4604
Michal Vasko7fbc8162018-09-17 10:35:16 +02004605 return ret;
4606}