blob: 65b7d476bea2ee0cec712ea19c69ddc35df8e144 [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 Vaskod989ba02020-08-24 10:59:24 +02002054 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002056 case LY_STMT_BASE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002057 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 +01002058 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002059 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002060 case LY_STMT_BIT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002061 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002062 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002063 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002064 case LY_STMT_ENUM:
Michal Vasko63f3d842020-07-08 10:10:14 +02002065 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002066 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002067 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002068 case LY_STMT_FRACTION_DIGITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002069 LY_CHECK_RET(parse_type_fracdigits(ctx, in, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002070 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002072 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002073 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002074 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002075 return LY_EVALID;
2076 }
2077 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002078 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002079
Michal Vasko63f3d842020-07-08 10:10:14 +02002080 LY_CHECK_RET(parse_restr(ctx, in, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002081 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002083 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002084 if (type->path) {
2085 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2086 return LY_EVALID;
2087 }
2088
Michal Vasko63f3d842020-07-08 10:10:14 +02002089 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 +02002090 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 +02002091 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
2092 lydict_remove(ctx->ctx, str_path);
2093 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002094 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002096 case LY_STMT_PATTERN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002097 LY_CHECK_RET(parse_type_pattern(ctx, in, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002098 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002100 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002101 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002102 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 return LY_EVALID;
2104 }
2105 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002106 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107
Michal Vasko63f3d842020-07-08 10:10:14 +02002108 LY_CHECK_RET(parse_restr(ctx, in, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002109 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002110 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002111 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002112 LY_CHECK_RET(parse_type_reqinstance(ctx, in, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002113 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002115 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002116 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02002117 LY_CHECK_RET(parse_type(ctx, in, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002118 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002119 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002120 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002121 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122 break;
2123 default:
David Sedlákb3077192019-06-19 10:55:37 +02002124 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125 return LY_EVALID;
2126 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002128 return ret;
2129}
2130
Michal Vaskoea5abea2018-09-18 13:10:54 +02002131/**
2132 * @brief Parse the leaf statement.
2133 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002134 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002135 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002136 * @param[in,out] siblings Siblings to add to.
2137 *
2138 * @return LY_ERR values.
2139 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002140LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002141parse_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 +02002142{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002143 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002144 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002145 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002146 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147 struct lysp_node_leaf *leaf;
2148
David Sedlák60adc092019-08-06 15:57:02 +02002149 /* create new leaf structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002150 LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002152 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002155 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002156 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002157
2158 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002159 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002160 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002161 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002162 LY_CHECK_RET(parse_config(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002164 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002165 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
2166 leaf->dflt.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002168 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002169 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 +02002170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002171 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002172 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, 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_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002175 LY_CHECK_RET(parse_mandatory(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002177 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002178 LY_CHECK_RET(parse_restrs(ctx, in, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002180 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002181 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 +02002182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002183 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002184 LY_CHECK_RET(parse_status(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002186 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002187 LY_CHECK_RET(parse_type(ctx, in, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002189 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002190 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 +02002191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002192 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002193 LY_CHECK_RET(parse_when(ctx, in, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002195 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002196 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 break;
2198 default:
David Sedlákb3077192019-06-19 10:55:37 +02002199 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200 return LY_EVALID;
2201 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002203 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002204checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002205 /* mandatory substatements */
2206 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002207 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002208 return LY_EVALID;
2209 }
2210
2211 return ret;
2212}
2213
Michal Vaskoea5abea2018-09-18 13:10:54 +02002214/**
2215 * @brief Parse the max-elements statement.
2216 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002217 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002218 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002219 * @param[in,out] max Value to write to.
2220 * @param[in,out] flags Flags to write to.
2221 * @param[in,out] exts Extension instances to add to.
2222 *
2223 * @return LY_ERR values.
2224 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002225LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002226parse_maxelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *max, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002227 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002229 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002230 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002231 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002232 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002233 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234
2235 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002236 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 return LY_EVALID;
2238 }
2239 *flags |= LYS_SET_MAX;
2240
2241 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002242 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002243
2244 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002245 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002246 free(buf);
2247 return LY_EVALID;
2248 }
2249
Radek Krejci7f9b6512019-09-18 13:11:09 +02002250 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 errno = 0;
2252 num = strtoul(word, &ptr, 10);
2253 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002254 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002255 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256 free(buf);
2257 return LY_EVALID;
2258 }
2259 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002260 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002261 free(buf);
2262 return LY_EVALID;
2263 }
2264
2265 *max = num;
2266 }
2267 free(buf);
2268
Michal Vaskod989ba02020-08-24 10:59:24 +02002269 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002270 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002271 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002272 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002273 break;
2274 default:
David Sedlákb3077192019-06-19 10:55:37 +02002275 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276 return LY_EVALID;
2277 }
2278 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 return ret;
2280}
2281
Michal Vaskoea5abea2018-09-18 13:10:54 +02002282/**
2283 * @brief Parse the min-elements statement.
2284 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002285 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002286 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002287 * @param[in,out] min Value to write to.
2288 * @param[in,out] flags Flags to write to.
2289 * @param[in,out] exts Extension instances to add to.
2290 *
2291 * @return LY_ERR values.
2292 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002293LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002294parse_minelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *min, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002295 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002296{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002297 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002299 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002300 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002301 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302
2303 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002304 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002305 return LY_EVALID;
2306 }
2307 *flags |= LYS_SET_MIN;
2308
2309 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002310 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311
2312 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002313 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314 free(buf);
2315 return LY_EVALID;
2316 }
2317
2318 errno = 0;
2319 num = strtoul(word, &ptr, 10);
2320 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002321 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002322 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002323 free(buf);
2324 return LY_EVALID;
2325 }
2326 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002327 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 free(buf);
2329 return LY_EVALID;
2330 }
2331 *min = num;
2332 free(buf);
2333
Michal Vaskod989ba02020-08-24 10:59:24 +02002334 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002335 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002336 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002337 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002338 break;
2339 default:
David Sedlákb3077192019-06-19 10:55:37 +02002340 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002341 return LY_EVALID;
2342 }
2343 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 return ret;
2345}
2346
Michal Vaskoea5abea2018-09-18 13:10:54 +02002347/**
2348 * @brief Parse the ordered-by statement.
2349 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002350 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002351 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002352 * @param[in,out] flags Flags to write to.
2353 * @param[in,out] exts Extension instances to add to.
2354 *
2355 * @return LY_ERR values.
2356 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002358parse_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 +02002359{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002360 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002362 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002363 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364
2365 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002366 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367 return LY_EVALID;
2368 }
2369
2370 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002371 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372
2373 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2374 *flags |= LYS_ORDBY_SYSTEM;
2375 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2376 *flags |= LYS_ORDBY_USER;
2377 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002378 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002379 free(buf);
2380 return LY_EVALID;
2381 }
2382 free(buf);
2383
Michal Vaskod989ba02020-08-24 10:59:24 +02002384 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002386 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002387 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388 break;
2389 default:
David Sedlákb3077192019-06-19 10:55:37 +02002390 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 return LY_EVALID;
2392 }
2393 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 return ret;
2395}
2396
Michal Vaskoea5abea2018-09-18 13:10:54 +02002397/**
2398 * @brief Parse the leaf-list statement.
2399 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002400 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002401 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002402 * @param[in,out] siblings Siblings to add to.
2403 *
2404 * @return LY_ERR values.
2405 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002406LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002407parse_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 +02002408{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002409 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002410 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002411 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002412 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413 struct lysp_node_leaflist *llist;
2414
David Sedlák60adc092019-08-06 15:57:02 +02002415 /* create new leaf-list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002416 LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002417 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002418 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002421 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002422 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423
2424 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002425 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002427 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002428 LY_CHECK_RET(parse_config(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002429 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002430 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002431 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002432 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002434 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002435 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 +02002436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002437 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002438 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, 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_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002441 LY_CHECK_RET(parse_maxelements(ctx, in, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002443 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002444 LY_CHECK_RET(parse_minelements(ctx, in, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002446 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002447 LY_CHECK_RET(parse_restrs(ctx, in, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002449 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002450 LY_CHECK_RET(parse_orderedby(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002452 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002453 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 +02002454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002455 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002456 LY_CHECK_RET(parse_status(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002458 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002459 LY_CHECK_RET(parse_type(ctx, in, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002461 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002462 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 +02002463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002464 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002465 LY_CHECK_RET(parse_when(ctx, in, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002467 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002468 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469 break;
2470 default:
David Sedlákb3077192019-06-19 10:55:37 +02002471 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002472 return LY_EVALID;
2473 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002474 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002475 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002476checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 /* mandatory substatements */
2478 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002479 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 return LY_EVALID;
2481 }
2482
2483 return ret;
2484}
2485
Michal Vaskoea5abea2018-09-18 13:10:54 +02002486/**
2487 * @brief Parse the refine statement.
2488 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002489 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002490 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002491 * @param[in,out] refines Refines to add to.
2492 *
2493 * @return LY_ERR values.
2494 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002495static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002496parse_refine(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002498 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002500 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002501 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 struct lysp_refine *rf;
2503
Radek Krejci2c4e7172018-10-19 15:56:26 +02002504 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505
2506 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002507 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002508 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002509 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510
Michal Vaskod989ba02020-08-24 10:59:24 +02002511 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002512 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002513 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002514 LY_CHECK_RET(parse_config(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002515 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002516 case LY_STMT_DEFAULT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002517 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 +02002518 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002519 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002520 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 +02002521 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002522 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002523 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002524 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002526 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002527 LY_CHECK_RET(parse_maxelements(ctx, in, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002529 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002530 LY_CHECK_RET(parse_minelements(ctx, in, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002532 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002533 LY_CHECK_RET(parse_restrs(ctx, in, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002535 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002536 LY_CHECK_RET(parse_mandatory(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002538 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002539 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 +02002540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002541 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002542 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 +02002543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002544 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002545 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546 break;
2547 default:
David Sedlákb3077192019-06-19 10:55:37 +02002548 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002549 return LY_EVALID;
2550 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002551 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 return ret;
2553}
2554
Michal Vaskoea5abea2018-09-18 13:10:54 +02002555/**
2556 * @brief Parse the typedef statement.
2557 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002558 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002559 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002560 * @param[in,out] typedefs Typedefs to add to.
2561 *
2562 * @return LY_ERR values.
2563 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002564static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002565parse_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 +02002566{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002567 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002568 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002569 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002570 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571 struct lysp_tpdf *tpdf;
2572
Radek Krejci2c4e7172018-10-19 15:56:26 +02002573 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574
2575 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002576 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002577 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578
2579 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002580 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002582 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002583 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
2584 tpdf->dflt.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002585 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002586 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002587 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 +02002588 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002589 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002590 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 +02002591 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002592 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002593 LY_CHECK_RET(parse_status(ctx, in, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002595 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002596 LY_CHECK_RET(parse_type(ctx, in, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002598 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002599 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 +02002600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002601 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002602 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 break;
2604 default:
David Sedlákb3077192019-06-19 10:55:37 +02002605 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 return LY_EVALID;
2607 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002609 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002610checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611 /* mandatory substatements */
2612 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002613 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614 return LY_EVALID;
2615 }
2616
Radek Krejcibbe09a92018-11-08 09:36:54 +01002617 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002618 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002619 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002620 }
2621
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622 return ret;
2623}
2624
Michal Vaskoea5abea2018-09-18 13:10:54 +02002625/**
2626 * @brief Parse the input or output statement.
2627 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002628 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002629 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002630 * @param[in] kw Type of this particular keyword
2631 * @param[in,out] inout_p Input/output pointer to write to.
2632 *
2633 * @return LY_ERR values.
2634 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02002636parse_inout(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt inout_kw, struct lysp_node *parent,
2637 struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002639 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002641 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002642 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002643
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002644 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002645 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646 return LY_EVALID;
2647 }
2648
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002649 /* initiate structure */
Michal Vasko22df3f02020-08-24 13:29:22 +02002650 inout_p->nodetype = &((struct lysp_action *)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002651 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652
2653 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002654 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002656 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002657 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002658 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002659 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002660 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002662 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002663 LY_CHECK_RET(parse_choice(ctx, in, (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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002666 LY_CHECK_RET(parse_container(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_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002669 LY_CHECK_RET(parse_leaf(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002672 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002675 LY_CHECK_RET(parse_list(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_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002678 LY_CHECK_RET(parse_uses(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_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002681 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, in, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002683 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002684 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Michal Vasko63f3d842020-07-08 10:10:14 +02002685 LY_CHECK_RET(parse_restrs(ctx, in, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002686 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002687 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002688 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002690 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002691 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 break;
2693 default:
David Sedlákb3077192019-06-19 10:55:37 +02002694 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695 return LY_EVALID;
2696 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002698 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002699
Radek Krejci7fc68292019-06-12 13:51:09 +02002700checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002701 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002702 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 +02002703
Michal Vaskob83af8a2020-01-06 09:49:22 +01002704 if (!inout_p->data) {
2705 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2706 return LY_EVALID;
2707 }
2708
Michal Vasko7fbc8162018-09-17 10:35:16 +02002709 return ret;
2710}
2711
Michal Vaskoea5abea2018-09-18 13:10:54 +02002712/**
2713 * @brief Parse the action statement.
2714 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002715 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002716 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002717 * @param[in,out] actions Actions to add to.
2718 *
2719 * @return LY_ERR values.
2720 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002721LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002722parse_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 +02002723{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002724 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002725 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002726 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002727 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728 struct lysp_action *act;
2729
Radek Krejci2c4e7172018-10-19 15:56:26 +02002730 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731
2732 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002733 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002734 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002735 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002736 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737
Michal Vasko63f3d842020-07-08 10:10:14 +02002738 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002740 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002741 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 +02002742 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002743 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002744 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002747 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 +02002748 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002749 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002750 LY_CHECK_RET(parse_status(ctx, in, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751 break;
2752
Radek Krejcid6b76452019-09-03 17:03:03 +02002753 case LY_STMT_INPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002754 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002756 case LY_STMT_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002757 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 break;
2759
Radek Krejcid6b76452019-09-03 17:03:03 +02002760 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002761 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, in, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002762 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002763 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002764 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002766 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002767 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002768 break;
2769 default:
David Sedlákb3077192019-06-19 10:55:37 +02002770 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002771 return LY_EVALID;
2772 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002774 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002775
2776 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2777 if (!act->input.nodetype) {
2778 act->input.nodetype = LYS_INPUT;
2779 act->input.parent = (struct lysp_node *)act;
2780 }
2781 if (!act->output.nodetype) {
2782 act->output.nodetype = LYS_OUTPUT;
2783 act->output.parent = (struct lysp_node *)act;
2784 }
2785
Radek Krejci7fc68292019-06-12 13:51:09 +02002786checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002787 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002788 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002789
Michal Vasko7fbc8162018-09-17 10:35:16 +02002790 return ret;
2791}
2792
Michal Vaskoea5abea2018-09-18 13:10:54 +02002793/**
2794 * @brief Parse the notification statement.
2795 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002796 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002797 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002798 * @param[in,out] notifs Notifications to add to.
2799 *
2800 * @return LY_ERR values.
2801 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002802LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002803parse_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 +02002804{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002805 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002807 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002808 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002809 struct lysp_notif *notif;
2810
Radek Krejci2c4e7172018-10-19 15:56:26 +02002811 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002812
2813 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002814 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002815 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002816 notif->nodetype = LYS_NOTIF;
2817 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818
Michal Vasko63f3d842020-07-08 10:10:14 +02002819 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002821 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002822 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 +02002823 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002824 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002825 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002828 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 +02002829 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002830 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002831 LY_CHECK_RET(parse_status(ctx, in, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002832 break;
2833
Radek Krejcid6b76452019-09-03 17:03:03 +02002834 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002835 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002836 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002837 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002838 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002840 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002841 LY_CHECK_RET(parse_case(ctx, in, (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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002844 LY_CHECK_RET(parse_container(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_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002847 LY_CHECK_RET(parse_leaf(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002850 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002853 LY_CHECK_RET(parse_list(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_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002856 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 break;
2858
Radek Krejcid6b76452019-09-03 17:03:03 +02002859 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002860 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Michal Vasko63f3d842020-07-08 10:10:14 +02002861 LY_CHECK_RET(parse_restrs(ctx, in, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002863 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002864 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, in, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002866 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002867 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002868 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002869 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002870 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002871 break;
2872 default:
David Sedlákb3077192019-06-19 10:55:37 +02002873 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002874 return LY_EVALID;
2875 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002877 LY_CHECK_RET(ret);
2878checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002879 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002880 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002881
Michal Vasko7fbc8162018-09-17 10:35:16 +02002882 return ret;
2883}
2884
Michal Vaskoea5abea2018-09-18 13:10:54 +02002885/**
2886 * @brief Parse the grouping statement.
2887 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002888 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002889 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002890 * @param[in,out] groupings Groupings to add to.
2891 *
2892 * @return LY_ERR values.
2893 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002894LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002895parse_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 +02002896{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002897 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002899 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002900 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 struct lysp_grp *grp;
2902
Radek Krejci2c4e7172018-10-19 15:56:26 +02002903 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904
2905 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002906 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002907 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002908 grp->nodetype = LYS_GROUPING;
2909 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910
Michal Vasko63f3d842020-07-08 10:10:14 +02002911 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002912 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002913 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002914 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 +02002915 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002916 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002917 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 +02002918 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002919 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002920 LY_CHECK_RET(parse_status(ctx, in, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 break;
2922
Radek Krejcid6b76452019-09-03 17:03:03 +02002923 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002924 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002925 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002926 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002927 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002929 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002930 LY_CHECK_RET(parse_choice(ctx, in, (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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002933 LY_CHECK_RET(parse_container(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_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002936 LY_CHECK_RET(parse_leaf(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002939 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002942 LY_CHECK_RET(parse_list(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_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002945 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 break;
2947
Radek Krejcid6b76452019-09-03 17:03:03 +02002948 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002949 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)grp, in, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002950 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002951 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002952 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Michal Vasko22df3f02020-08-24 13:29:22 +02002953 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002954 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002955 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002956 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002958 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002959 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Michal Vasko22df3f02020-08-24 13:29:22 +02002960 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002962 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002963 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 break;
2965 default:
David Sedlákb3077192019-06-19 10:55:37 +02002966 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967 return LY_EVALID;
2968 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002969 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002970 LY_CHECK_RET(ret);
2971checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002972 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002973 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 +02002974
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975 return ret;
2976}
2977
Michal Vaskoea5abea2018-09-18 13:10:54 +02002978/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002979 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002980 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002981 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002982 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002983 * @param[in,out] augments Augments to add to.
2984 *
2985 * @return LY_ERR values.
2986 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002987LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002988parse_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 +02002989{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002990 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002992 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002993 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 struct lysp_augment *aug;
2995
Radek Krejci2c4e7172018-10-19 15:56:26 +02002996 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997
2998 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002999 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003000 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003001 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003002 aug->nodetype = LYS_AUGMENT;
3003 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003004
Michal Vasko63f3d842020-07-08 10:10:14 +02003005 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003007 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003008 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 +02003009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003010 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003011 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003014 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 +02003015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003016 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003017 LY_CHECK_RET(parse_status(ctx, in, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003019 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003020 LY_CHECK_RET(parse_when(ctx, in, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021 break;
3022
Radek Krejcid6b76452019-09-03 17:03:03 +02003023 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003024 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003025 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003026 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003027 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003029 case LY_STMT_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003030 LY_CHECK_RET(parse_case(ctx, in, (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_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003033 LY_CHECK_RET(parse_choice(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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003036 LY_CHECK_RET(parse_container(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_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003039 LY_CHECK_RET(parse_leaf(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003042 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003045 LY_CHECK_RET(parse_list(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_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003048 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 break;
3050
Radek Krejcid6b76452019-09-03 17:03:03 +02003051 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003052 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Michal Vasko22df3f02020-08-24 13:29:22 +02003053 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003054 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003055 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003056 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Michal Vasko22df3f02020-08-24 13:29:22 +02003057 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003059 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003060 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061 break;
3062 default:
David Sedlákb3077192019-06-19 10:55:37 +02003063 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064 return LY_EVALID;
3065 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003067 LY_CHECK_RET(ret);
3068checks:
3069 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003070 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 +02003071
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 return ret;
3073}
3074
Michal Vaskoea5abea2018-09-18 13:10:54 +02003075/**
3076 * @brief Parse the uses statement.
3077 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003078 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003079 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003080 * @param[in,out] siblings Siblings to add to.
3081 *
3082 * @return LY_ERR values.
3083 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003084LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003085parse_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 +02003086{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003087 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003088 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003089 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003090 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003091 struct lysp_node_uses *uses;
3092
David Sedlák60adc092019-08-06 15:57:02 +02003093 /* create uses structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003094 LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003096 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003099 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003100 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101
3102 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003103 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003105 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003106 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 +02003107 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003108 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003109 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003112 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 +02003113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003114 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003115 LY_CHECK_RET(parse_status(ctx, in, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003116 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003117 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003118 LY_CHECK_RET(parse_when(ctx, in, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 break;
3120
Radek Krejcid6b76452019-09-03 17:03:03 +02003121 case LY_STMT_REFINE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003122 LY_CHECK_RET(parse_refine(ctx, in, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003124 case LY_STMT_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02003125 LY_CHECK_RET(parse_augment(ctx, in, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003127 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003128 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 break;
3130 default:
David Sedlákb3077192019-06-19 10:55:37 +02003131 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132 return LY_EVALID;
3133 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003134 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003135checks:
3136 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003137 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003138
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 return ret;
3140}
3141
Michal Vaskoea5abea2018-09-18 13:10:54 +02003142/**
3143 * @brief Parse the case statement.
3144 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003145 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003146 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003147 * @param[in,out] siblings Siblings to add to.
3148 *
3149 * @return LY_ERR values.
3150 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003151LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003152parse_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 +02003153{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003154 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003155 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003156 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003157 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158 struct lysp_node_case *cas;
3159
David Sedlák60adc092019-08-06 15:57:02 +02003160 /* create new case structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003161 LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003163 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003166 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003167 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003168
3169 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02003170 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003172 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003173 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 +02003174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003175 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003176 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003179 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 +02003180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003181 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003182 LY_CHECK_RET(parse_status(ctx, in, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003184 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003185 LY_CHECK_RET(parse_when(ctx, in, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003186 break;
3187
Radek Krejcid6b76452019-09-03 17:03:03 +02003188 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003189 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003190 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003191 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003192 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003194 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003195 LY_CHECK_RET(parse_choice(ctx, in, (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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003198 LY_CHECK_RET(parse_container(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_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003201 LY_CHECK_RET(parse_leaf(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003204 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003207 LY_CHECK_RET(parse_list(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_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003210 LY_CHECK_RET(parse_uses(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_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003213 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
3215 default:
David Sedlákb3077192019-06-19 10:55:37 +02003216 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 return LY_EVALID;
3218 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 return ret;
3221}
3222
Michal Vaskoea5abea2018-09-18 13:10:54 +02003223/**
3224 * @brief Parse the choice statement.
3225 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003226 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003227 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003228 * @param[in,out] siblings Siblings to add to.
3229 *
3230 * @return LY_ERR values.
3231 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003232LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003233parse_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 +02003234{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003235 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003237 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003238 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003239 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240
David Sedlák60adc092019-08-06 15:57:02 +02003241 /* create new choice structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003242 LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003243 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003244 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003247 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003248 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249
3250 /* parse substatements */
Michal Vasko7f45cf22020-10-01 12:49:44 +02003251 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003253 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003254 LY_CHECK_RET(parse_config(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003256 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003257 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 +02003258 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003259 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003260 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, 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_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003263 LY_CHECK_RET(parse_mandatory(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003265 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003266 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 +02003267 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003268 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003269 LY_CHECK_RET(parse_status(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003271 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003272 LY_CHECK_RET(parse_when(ctx, in, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003274 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003275 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
3276 &choice->exts));
3277 choice->dflt.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278 break;
3279
Radek Krejcid6b76452019-09-03 17:03:03 +02003280 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003281 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003282 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003283 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003284 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003286 case LY_STMT_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003287 LY_CHECK_RET(parse_case(ctx, in, (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_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003290 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Michal Vasko22df3f02020-08-24 13:29:22 +02003291 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003293 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003294 LY_CHECK_RET(parse_container(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_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003297 LY_CHECK_RET(parse_leaf(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003300 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003303 LY_CHECK_RET(parse_list(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_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003306 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 break;
3308 default:
David Sedlákb3077192019-06-19 10:55:37 +02003309 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310 return LY_EVALID;
3311 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313 return ret;
3314}
3315
Michal Vaskoea5abea2018-09-18 13:10:54 +02003316/**
3317 * @brief Parse the container statement.
3318 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003319 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003320 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003321 * @param[in,out] siblings Siblings to add to.
3322 *
3323 * @return LY_ERR values.
3324 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003325LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003326parse_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 +02003327{
3328 LY_ERR ret = 0;
3329 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003330 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003331 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332 struct lysp_node_container *cont;
3333
David Sedlák60adc092019-08-06 15:57:02 +02003334 /* create new container structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003335 LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003336 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003337 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003340 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003341 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342
3343 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003344 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003346 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003347 LY_CHECK_RET(parse_config(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003349 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003350 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 +02003351 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003352 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003353 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003356 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 +02003357 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003358 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003359 LY_CHECK_RET(parse_status(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003360 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003361 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003362 LY_CHECK_RET(parse_when(ctx, in, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003364 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003365 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 +02003366 break;
3367
Radek Krejcid6b76452019-09-03 17:03:03 +02003368 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003369 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003370 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003371 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003372 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003374 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003375 LY_CHECK_RET(parse_choice(ctx, in, (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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003378 LY_CHECK_RET(parse_container(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_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003381 LY_CHECK_RET(parse_leaf(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003384 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003387 LY_CHECK_RET(parse_list(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_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003390 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003391 break;
3392
Radek Krejcid6b76452019-09-03 17:03:03 +02003393 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003394 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, in, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003396 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003397 LY_CHECK_RET(parse_restrs(ctx, in, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003399 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003400 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Michal Vasko22df3f02020-08-24 13:29:22 +02003401 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003403 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02003404 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003406 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003407 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Michal Vasko22df3f02020-08-24 13:29:22 +02003408 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003410 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003411 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 break;
3413 default:
David Sedlákb3077192019-06-19 10:55:37 +02003414 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 return LY_EVALID;
3416 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003418checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003419 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003420 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 +02003421 return ret;
3422}
3423
Michal Vaskoea5abea2018-09-18 13:10:54 +02003424/**
3425 * @brief Parse the list statement.
3426 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003427 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003428 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003429 * @param[in,out] siblings Siblings to add to.
3430 *
3431 * @return LY_ERR values.
3432 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003433LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003434parse_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 +02003435{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003436 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003437 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003438 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003439 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003440 struct lysp_node_list *list;
3441
David Sedlák60adc092019-08-06 15:57:02 +02003442 /* create new list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003443 LY_LIST_NEW_RET(ctx->ctx, siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003445 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003448 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003449 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003450
3451 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003452 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003453 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003454 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003455 LY_CHECK_RET(parse_config(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003457 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003458 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 +02003459 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003461 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003464 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 +02003465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003466 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003467 LY_CHECK_RET(parse_status(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003468 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003469 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003470 LY_CHECK_RET(parse_when(ctx, in, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003472 case LY_STMT_KEY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003473 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 +02003474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003475 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003476 LY_CHECK_RET(parse_maxelements(ctx, in, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003477 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003478 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003479 LY_CHECK_RET(parse_minelements(ctx, in, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003481 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003482 LY_CHECK_RET(parse_orderedby(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003484 case LY_STMT_UNIQUE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003485 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486 break;
3487
Radek Krejcid6b76452019-09-03 17:03:03 +02003488 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003489 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003490 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003491 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003492 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003494 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003495 LY_CHECK_RET(parse_choice(ctx, in, (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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003498 LY_CHECK_RET(parse_container(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_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003501 LY_CHECK_RET(parse_leaf(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003504 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003507 LY_CHECK_RET(parse_list(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_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003510 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 break;
3512
Radek Krejcid6b76452019-09-03 17:03:03 +02003513 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003514 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, in, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003515 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003516 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003517 LY_CHECK_RET(parse_restrs(ctx, in, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003519 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003520 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Michal Vasko22df3f02020-08-24 13:29:22 +02003521 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003523 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02003524 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003527 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Michal Vasko22df3f02020-08-24 13:29:22 +02003528 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003530 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003531 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532 break;
3533 default:
David Sedlákb3077192019-06-19 10:55:37 +02003534 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535 return LY_EVALID;
3536 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003538 LY_CHECK_RET(ret);
3539checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003540 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003541 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 +02003542
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 return ret;
3544}
3545
Michal Vaskoea5abea2018-09-18 13:10:54 +02003546/**
3547 * @brief Parse the yin-element statement.
3548 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003549 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003550 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003551 * @param[in,out] flags Flags to write to.
3552 * @param[in,out] exts Extension instances to add to.
3553 *
3554 * @return LY_ERR values.
3555 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003557parse_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 +02003558{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003559 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003561 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003562 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563
3564 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003565 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 return LY_EVALID;
3567 }
3568
3569 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003570 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571
3572 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3573 *flags |= LYS_YINELEM_TRUE;
3574 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3575 *flags |= LYS_YINELEM_FALSE;
3576 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003577 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 free(buf);
3579 return LY_EVALID;
3580 }
3581 free(buf);
3582
Michal Vaskod989ba02020-08-24 10:59:24 +02003583 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003585 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003586 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003587 LY_CHECK_RET(ret);
3588 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 default:
David Sedlákb3077192019-06-19 10:55:37 +02003590 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591 return LY_EVALID;
3592 }
3593 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 return ret;
3595}
3596
Michal Vaskoea5abea2018-09-18 13:10:54 +02003597/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003598 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003599 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003600 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003601 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003602 * @param[in,out] argument Value to write to.
3603 * @param[in,out] flags Flags to write to.
3604 * @param[in,out] exts Extension instances to add to.
3605 *
3606 * @return LY_ERR values.
3607 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003608static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003609parse_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 +02003610{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003611 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003612 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003613 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003614 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615
3616 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003617 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618 return LY_EVALID;
3619 }
3620
3621 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003622 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003623 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003624
Michal Vaskod989ba02020-08-24 10:59:24 +02003625 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003627 case LY_STMT_YIN_ELEMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003628 LY_CHECK_RET(parse_yinelement(ctx, in, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003630 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003631 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 break;
3633 default:
David Sedlákb3077192019-06-19 10:55:37 +02003634 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 return LY_EVALID;
3636 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 return ret;
3639}
3640
Michal Vaskoea5abea2018-09-18 13:10:54 +02003641/**
3642 * @brief Parse the extension statement.
3643 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003644 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003645 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003646 * @param[in,out] extensions Extensions to add to.
3647 *
3648 * @return LY_ERR values.
3649 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003651parse_extension(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003653 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003654 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003655 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003656 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 struct lysp_ext *ex;
3658
Radek Krejci2c4e7172018-10-19 15:56:26 +02003659 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660
3661 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003662 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003663 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664
Michal Vaskod989ba02020-08-24 10:59:24 +02003665 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003666 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003667 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003668 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 +02003669 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003670 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003671 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 +02003672 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003673 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003674 LY_CHECK_RET(parse_status(ctx, in, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003676 case LY_STMT_ARGUMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003677 LY_CHECK_RET(parse_argument(ctx, in, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003679 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003680 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003681 break;
3682 default:
David Sedlákb3077192019-06-19 10:55:37 +02003683 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003684 return LY_EVALID;
3685 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003686 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687 return ret;
3688}
3689
Michal Vaskoea5abea2018-09-18 13:10:54 +02003690/**
3691 * @brief Parse the deviate statement.
3692 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003693 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003694 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003695 * @param[in,out] deviates Deviates to add to.
3696 *
3697 * @return LY_ERR values.
3698 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003699LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003700parse_deviate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003702 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003703 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003704 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003705 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003706 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003707 struct lysp_deviate_add *d_add = NULL;
3708 struct lysp_deviate_rpl *d_rpl = NULL;
3709 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003710 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3711 struct lysp_restr **d_musts = NULL;
3712 uint16_t *d_flags = 0;
3713 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714
3715 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003716 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717
3718 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3719 dev_mod = LYS_DEV_NOT_SUPPORTED;
3720 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3721 dev_mod = LYS_DEV_ADD;
3722 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3723 dev_mod = LYS_DEV_REPLACE;
3724 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3725 dev_mod = LYS_DEV_DELETE;
3726 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003727 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728 free(buf);
3729 return LY_EVALID;
3730 }
3731 free(buf);
3732
3733 /* create structure */
3734 switch (dev_mod) {
3735 case LYS_DEV_NOT_SUPPORTED:
3736 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003737 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003738 break;
3739 case LYS_DEV_ADD:
3740 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003741 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742 d = (struct lysp_deviate *)d_add;
3743 d_units = &d_add->units;
3744 d_uniques = &d_add->uniques;
3745 d_dflts = &d_add->dflts;
3746 d_musts = &d_add->musts;
3747 d_flags = &d_add->flags;
3748 d_min = &d_add->min;
3749 d_max = &d_add->max;
3750 break;
3751 case LYS_DEV_REPLACE:
3752 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003753 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003754 d = (struct lysp_deviate *)d_rpl;
3755 d_units = &d_rpl->units;
3756 d_flags = &d_rpl->flags;
3757 d_min = &d_rpl->min;
3758 d_max = &d_rpl->max;
3759 break;
3760 case LYS_DEV_DELETE:
3761 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003762 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003763 d = (struct lysp_deviate *)d_del;
3764 d_units = &d_del->units;
3765 d_uniques = &d_del->uniques;
3766 d_dflts = &d_del->dflts;
3767 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 break;
3769 default:
3770 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003771 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 }
3773 d->mod = dev_mod;
3774
3775 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003776 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003777
Michal Vaskod989ba02020-08-24 10:59:24 +02003778 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003780 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 switch (dev_mod) {
3782 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003783 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003784 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785 return LY_EVALID;
3786 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003787 LY_CHECK_RET(parse_config(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 break;
3789 }
3790 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003791 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003792 switch (dev_mod) {
3793 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003794 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 return LY_EVALID;
3796 case LYS_DEV_REPLACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003797 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 +02003798 break;
3799 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003800 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 +02003801 break;
3802 }
3803 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003804 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805 switch (dev_mod) {
3806 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003807 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003808 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003809 return LY_EVALID;
3810 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003811 LY_CHECK_RET(parse_mandatory(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 break;
3813 }
3814 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003815 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003816 switch (dev_mod) {
3817 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003818 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003819 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003820 return LY_EVALID;
3821 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003822 LY_CHECK_RET(parse_maxelements(ctx, in, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 break;
3824 }
3825 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003826 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003827 switch (dev_mod) {
3828 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003829 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003830 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003831 return LY_EVALID;
3832 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003833 LY_CHECK_RET(parse_minelements(ctx, in, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 break;
3835 }
3836 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003837 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838 switch (dev_mod) {
3839 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003840 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003841 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003842 return LY_EVALID;
3843 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003844 LY_CHECK_RET(parse_restrs(ctx, in, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845 break;
3846 }
3847 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003848 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003849 switch (dev_mod) {
3850 case LYS_DEV_NOT_SUPPORTED:
3851 case LYS_DEV_ADD:
3852 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003853 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003854 return LY_EVALID;
3855 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003856 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003857 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003858 return LY_EVALID;
3859 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003860 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003861 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02003862 LY_CHECK_RET(parse_type(ctx, in, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863 break;
3864 }
3865 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003866 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003867 switch (dev_mod) {
3868 case LYS_DEV_NOT_SUPPORTED:
3869 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003870 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003871 return LY_EVALID;
3872 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003873 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 +02003874 break;
3875 }
3876 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003877 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003878 switch (dev_mod) {
3879 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003880 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 return LY_EVALID;
3882 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003883 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 +02003884 break;
3885 }
3886 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003887 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003888 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003889 break;
3890 default:
David Sedlákb3077192019-06-19 10:55:37 +02003891 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 return LY_EVALID;
3893 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 return ret;
3896}
3897
Michal Vaskoea5abea2018-09-18 13:10:54 +02003898/**
3899 * @brief Parse the deviation statement.
3900 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003901 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003902 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003903 * @param[in,out] deviations Deviations to add to.
3904 *
3905 * @return LY_ERR values.
3906 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003907LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003908parse_deviation(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003909{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003910 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003911 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003912 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003913 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914 struct lysp_deviation *dev;
3915
Radek Krejci2c4e7172018-10-19 15:56:26 +02003916 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917
3918 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003919 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003920 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003921 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003922
Michal Vasko63f3d842020-07-08 10:10:14 +02003923 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003924 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003925 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003926 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 +02003927 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003928 case LY_STMT_DEVIATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003929 LY_CHECK_RET(parse_deviate(ctx, in, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003931 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003932 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 +02003933 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003934 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003935 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936 break;
3937 default:
David Sedlákb3077192019-06-19 10:55:37 +02003938 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939 return LY_EVALID;
3940 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003941 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003942 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003943checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 /* mandatory substatements */
3945 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003946 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 return LY_EVALID;
3948 }
3949
3950 return ret;
3951}
3952
Michal Vaskoea5abea2018-09-18 13:10:54 +02003953/**
3954 * @brief Parse the feature statement.
3955 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003956 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003957 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003958 * @param[in,out] features Features to add to.
3959 *
3960 * @return LY_ERR values.
3961 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003962LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003963parse_feature(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003965 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003966 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003967 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003968 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969 struct lysp_feature *feat;
3970
Radek Krejci2c4e7172018-10-19 15:56:26 +02003971 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003972
3973 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003974 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003975 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003976
Michal Vaskod989ba02020-08-24 10:59:24 +02003977 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003979 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003980 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 +02003981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003982 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003983 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003986 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 +02003987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003988 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003989 LY_CHECK_RET(parse_status(ctx, in, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003991 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003992 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 break;
3994 default:
David Sedlákb3077192019-06-19 10:55:37 +02003995 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003996 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003998 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003999 return ret;
4000}
4001
Michal Vaskoea5abea2018-09-18 13:10:54 +02004002/**
4003 * @brief Parse the identity statement.
4004 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004005 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004006 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004007 * @param[in,out] identities Identities to add to.
4008 *
4009 * @return LY_ERR values.
4010 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004011LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004012parse_identity(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004014 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004015 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004016 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004017 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004018 struct lysp_ident *ident;
4019
Radek Krejci2c4e7172018-10-19 15:56:26 +02004020 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004021
4022 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02004023 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004024 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004025
Michal Vaskod989ba02020-08-24 10:59:24 +02004026 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004027 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004028 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004029 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 +02004030 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004031 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004032 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Michal Vasko7f45cf22020-10-01 12:49:44 +02004033 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004034 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004035 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004036 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 +02004037 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004038 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02004039 LY_CHECK_RET(parse_status(ctx, in, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004041 case LY_STMT_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004042 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02004043 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 +01004044 return LY_EVALID;
4045 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004046 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 +02004047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004048 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004049 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004050 break;
4051 default:
David Sedlákb3077192019-06-19 10:55:37 +02004052 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 return LY_EVALID;
4054 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004055 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 return ret;
4057}
4058
Michal Vaskoea5abea2018-09-18 13:10:54 +02004059/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004060 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004061 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004062 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004063 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004064 * @param[in,out] mod Module to write to.
4065 *
4066 * @return LY_ERR values.
4067 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004068LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004069parse_module(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004070{
4071 LY_ERR ret = 0;
4072 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004073 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004074 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004075 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004076 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004077
4078 /* (sub)module name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004079 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004080 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004081
Michal Vasko63f3d842020-07-08 10:10:14 +02004082 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004083
Radek Krejcie3846472018-10-15 15:24:51 +02004084#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004085 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 +02004086
Michal Vasko7fbc8162018-09-17 10:35:16 +02004087 switch (kw) {
4088 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004089 case LY_STMT_NAMESPACE:
4090 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004091 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004093 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004094 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095 break;
4096 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004097 case LY_STMT_INCLUDE:
4098 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004099 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004100 break;
4101 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004102 case LY_STMT_ORGANIZATION:
4103 case LY_STMT_CONTACT:
4104 case LY_STMT_DESCRIPTION:
4105 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004106 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107 break;
4108
4109 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004110 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004111 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004112 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004113 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004114 case LY_STMT_ANYDATA:
4115 case LY_STMT_ANYXML:
4116 case LY_STMT_AUGMENT:
4117 case LY_STMT_CHOICE:
4118 case LY_STMT_CONTAINER:
4119 case LY_STMT_DEVIATION:
4120 case LY_STMT_EXTENSION:
4121 case LY_STMT_FEATURE:
4122 case LY_STMT_GROUPING:
4123 case LY_STMT_IDENTITY:
4124 case LY_STMT_LEAF:
4125 case LY_STMT_LEAF_LIST:
4126 case LY_STMT_LIST:
4127 case LY_STMT_NOTIFICATION:
4128 case LY_STMT_RPC:
4129 case LY_STMT_TYPEDEF:
4130 case LY_STMT_USES:
4131 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004132 mod_stmt = Y_MOD_BODY;
4133 break;
4134 default:
4135 /* error handled in the next switch */
4136 break;
4137 }
Radek Krejcie3846472018-10-15 15:24:51 +02004138#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139
Radek Krejcie3846472018-10-15 15:24:51 +02004140 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004141 switch (kw) {
4142 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004143 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004144 LY_CHECK_RET(parse_yangversion(ctx, in, &mod->mod->version, &mod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004145 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004147 case LY_STMT_NAMESPACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004148 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 +02004149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004150 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02004151 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 +02004152 break;
4153
4154 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004155 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004156 LY_CHECK_RET(parse_include(ctx, mod->mod->name, in, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004158 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004159 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, in, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004160 break;
4161
4162 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004163 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004164 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 +02004165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004167 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 +02004168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004169 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004170 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 +02004171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004172 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004173 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 +02004174 break;
4175
4176 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004177 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004178 LY_CHECK_RET(parse_revision(ctx, in, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004179 break;
4180
4181 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004182 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004183 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004184 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004186 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004189 LY_CHECK_RET(parse_choice(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004191 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004192 LY_CHECK_RET(parse_container(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_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004195 LY_CHECK_RET(parse_leaf(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_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004198 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004201 LY_CHECK_RET(parse_list(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_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004204 LY_CHECK_RET(parse_uses(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 break;
4206
Radek Krejcid6b76452019-09-03 17:03:03 +02004207 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004208 LY_CHECK_RET(parse_augment(ctx, in, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004209 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004210 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004211 LY_CHECK_RET(parse_deviation(ctx, in, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004212 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004213 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004214 LY_CHECK_RET(parse_extension(ctx, in, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004216 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004217 LY_CHECK_RET(parse_feature(ctx, in, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004218 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004219 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004220 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004222 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004223 LY_CHECK_RET(parse_identity(ctx, in, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004225 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004226 LY_CHECK_RET(parse_notif(ctx, in, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004228 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004229 LY_CHECK_RET(parse_action(ctx, in, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004231 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004232 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004234 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004235 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004236 break;
4237
4238 default:
David Sedlákb3077192019-06-19 10:55:37 +02004239 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004240 return LY_EVALID;
4241 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004242 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004243 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004244
Radek Krejci6d6556c2018-11-08 09:37:45 +01004245checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004246 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004247 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 +01004248
Michal Vasko7fbc8162018-09-17 10:35:16 +02004249 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004250 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004251 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004252 return LY_EVALID;
4253 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004254 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004255 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004256 }
4257
Radek Krejcie9e987e2018-10-31 12:50:27 +01004258 /* submodules share the namespace with the module names, so there must not be
4259 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004260 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4261 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004262 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 +01004263 return LY_EVALID;
4264 }
4265
4266 return ret;
4267}
4268
4269/**
4270 * @brief Parse submodule substatements.
4271 *
4272 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004273 * @param[in,out] in Input structure.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004274 * @param[out] submod Parsed submodule structure.
4275 *
4276 * @return LY_ERR values.
4277 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004278LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004279parse_submodule(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004280{
4281 LY_ERR ret = 0;
4282 char *buf, *word;
4283 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004284 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004285 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4286 struct lysp_submodule *dup;
4287
4288 /* submodule name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004289 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004290 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004291
Michal Vasko63f3d842020-07-08 10:10:14 +02004292 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004293
4294#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004295 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 +01004296
4297 switch (kw) {
4298 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004299 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004300 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4301 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004302 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004303 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4304 break;
4305 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004306 case LY_STMT_INCLUDE:
4307 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004308 CHECK_ORDER(Y_MOD_LINKAGE);
4309 break;
4310 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004311 case LY_STMT_ORGANIZATION:
4312 case LY_STMT_CONTACT:
4313 case LY_STMT_DESCRIPTION:
4314 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004315 CHECK_ORDER(Y_MOD_META);
4316 break;
4317
4318 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004319 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004320 CHECK_ORDER(Y_MOD_REVISION);
4321 break;
4322 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004323 case LY_STMT_ANYDATA:
4324 case LY_STMT_ANYXML:
4325 case LY_STMT_AUGMENT:
4326 case LY_STMT_CHOICE:
4327 case LY_STMT_CONTAINER:
4328 case LY_STMT_DEVIATION:
4329 case LY_STMT_EXTENSION:
4330 case LY_STMT_FEATURE:
4331 case LY_STMT_GROUPING:
4332 case LY_STMT_IDENTITY:
4333 case LY_STMT_LEAF:
4334 case LY_STMT_LEAF_LIST:
4335 case LY_STMT_LIST:
4336 case LY_STMT_NOTIFICATION:
4337 case LY_STMT_RPC:
4338 case LY_STMT_TYPEDEF:
4339 case LY_STMT_USES:
4340 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004341 mod_stmt = Y_MOD_BODY;
4342 break;
4343 default:
4344 /* error handled in the next switch */
4345 break;
4346 }
4347#undef CHECK_ORDER
4348
4349 prev_kw = kw;
4350 switch (kw) {
4351 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004352 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004353 LY_CHECK_RET(parse_yangversion(ctx, in, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004354 ctx->mod_version = submod->version;
4355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004356 case LY_STMT_BELONGS_TO:
Michal Vasko63f3d842020-07-08 10:10:14 +02004357 LY_CHECK_RET(parse_belongsto(ctx, in, &submod->belongsto, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004358 break;
4359
4360 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004361 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004362 LY_CHECK_RET(parse_include(ctx, submod->name, in, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004364 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004365 LY_CHECK_RET(parse_import(ctx, submod->prefix, in, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004366 break;
4367
4368 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004370 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 +01004371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004373 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 +01004374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004376 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 +01004377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004379 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 +01004380 break;
4381
4382 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004383 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004384 LY_CHECK_RET(parse_revision(ctx, in, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004385 break;
4386
4387 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004389 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004390 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004392 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004395 LY_CHECK_RET(parse_choice(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004397 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004398 LY_CHECK_RET(parse_container(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_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004401 LY_CHECK_RET(parse_leaf(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_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004404 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004407 LY_CHECK_RET(parse_list(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_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004410 LY_CHECK_RET(parse_uses(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004411 break;
4412
Radek Krejcid6b76452019-09-03 17:03:03 +02004413 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004414 LY_CHECK_RET(parse_augment(ctx, in, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004416 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004417 LY_CHECK_RET(parse_deviation(ctx, in, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004419 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004420 LY_CHECK_RET(parse_extension(ctx, in, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004422 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004423 LY_CHECK_RET(parse_feature(ctx, in, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004425 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004426 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004428 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004429 LY_CHECK_RET(parse_identity(ctx, in, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004431 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004432 LY_CHECK_RET(parse_notif(ctx, in, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004434 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004435 LY_CHECK_RET(parse_action(ctx, in, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004437 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004438 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004440 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004441 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004442 break;
4443
4444 default:
David Sedlákb3077192019-06-19 10:55:37 +02004445 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004446 return LY_EVALID;
4447 }
4448 }
4449 LY_CHECK_RET(ret);
4450
4451checks:
4452 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004453 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments,
4454 submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004455
4456 /* mandatory substatements */
4457 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004458 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004459 return LY_EVALID;
4460 }
4461
4462 /* submodules share the namespace with the module names, so there must not be
4463 * a submodule of the same name in the context, no need for revision matching */
4464 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4465 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004466 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004467 return LY_EVALID;
4468 }
4469
Michal Vasko7fbc8162018-09-17 10:35:16 +02004470 return ret;
4471}
4472
Radek Krejcid4557c62018-09-17 11:42:09 +02004473LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004474yang_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 +02004475 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004476{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004477 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004478 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004479 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004480 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004481 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004482
David Sedlák1b623122019-08-05 15:27:49 +02004483 /* create context */
4484 *context = calloc(1, sizeof **context);
4485 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004486 (*context)->format = LYS_IN_YANG;
Michal Vasko7c8439f2020-08-05 13:25:19 +02004487 (*context)->main_mod = main_ctx->main_mod;
David Sedlák1b623122019-08-05 15:27:49 +02004488 (*context)->ctx = ly_ctx;
Radek Krejci99435242019-09-05 16:19:15 +02004489 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004490 (*context)->line = 1;
4491
4492 /* map the typedefs and groupings list from main context to the submodule's context */
4493 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4494 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4495
Michal Vasko7fbc8162018-09-17 10:35:16 +02004496 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004497 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004498 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004499
Radek Krejcid6b76452019-09-03 17:03:03 +02004500 if (kw == LY_STMT_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004501 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004502 ret = LY_EINVAL;
4503 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004504 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004505 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004506 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004507 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004508 }
4509
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004510 mod_p = calloc(1, sizeof *mod_p);
Radek Krejcif027df72020-09-15 13:00:28 +02004511 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx); ret = LY_EMEM, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004512 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004513
4514 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004515 ret = parse_submodule(*context, in, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004516 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004517
4518 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004519 while (isspace(in->current[0])) {
4520 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004521 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004522 if (in->current[0]) {
4523 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004524 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004525 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004526 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004527
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004528 mod_p->parsing = 0;
4529 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004530
Radek Krejcibbe09a92018-11-08 09:36:54 +01004531cleanup:
4532 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004533 lysp_submodule_free((*context)->ctx, mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004534 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004535 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004536 }
4537
4538 return ret;
4539}
4540
4541LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004542yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004543{
4544 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004545 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004546 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004547 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004548 struct lysp_module *mod_p = NULL;
4549
David Sedlák1b623122019-08-05 15:27:49 +02004550 /* create context */
4551 *context = calloc(1, sizeof **context);
4552 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004553 (*context)->format = LYS_IN_YANG;
Michal Vasko7c8439f2020-08-05 13:25:19 +02004554 (*context)->main_mod = mod;
David Sedlák1b623122019-08-05 15:27:49 +02004555 (*context)->ctx = mod->ctx;
Radek Krejci335332a2019-09-05 13:03:35 +02004556 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004557 (*context)->line = 1;
4558
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004559 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004560 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004561 LY_CHECK_GOTO(ret, cleanup);
4562
Radek Krejcid6b76452019-09-03 17:03:03 +02004563 if (kw == LY_STMT_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004564 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 +01004565 ret = LY_EINVAL;
4566 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004567 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004568 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004569 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004570 goto cleanup;
4571 }
4572
4573 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004574 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004575 mod_p->mod = mod;
4576 mod_p->parsing = 1;
4577
4578 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004579 ret = parse_module(*context, in, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004580 LY_CHECK_GOTO(ret, cleanup);
4581
4582 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004583 while (isspace(in->current[0])) {
4584 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004585 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004586 if (in->current[0]) {
4587 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004588 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004589 goto cleanup;
4590 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004591
4592 mod_p->parsing = 0;
4593 mod->parsed = mod_p;
4594
4595cleanup:
4596 if (ret) {
4597 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004598 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004599 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004600 }
4601
Michal Vasko7fbc8162018-09-17 10:35:16 +02004602 return ret;
4603}