blob: 18c76bc0dab3875fce2a0842f49cd91e810a57e1 [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] prefix Place to store the parsed belongs-to prefix value.
954 * @param[in,out] exts Extension instances to add to.
955 *
956 * @return LY_ERR values.
957 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200958static LY_ERR
Michal Vaskoc3781c32020-10-06 14:04:08 +0200959parse_belongsto(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100961 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200962 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200963 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200964 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200965
Michal Vaskoc3781c32020-10-06 14:04:08 +0200966 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200967 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968 return LY_EVALID;
969 }
970
Michal Vaskoc3781c32020-10-06 14:04:08 +0200971 /* get value, it must match the main module */
Michal Vasko63f3d842020-07-08 10:10:14 +0200972 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vaskoc3781c32020-10-06 14:04:08 +0200973 if (ly_strncmp(ctx->main_mod->name, word, word_len)) {
974 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Submodule \"belongs-to\" value \"%.*s\" does not match its module name \"%s\".",
975 (int)word_len, word, ctx->main_mod->name);
976 free(buf);
977 return LY_EVALID;
978 }
979 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200980
Michal Vasko63f3d842020-07-08 10:10:14 +0200981 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200982 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200983 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +0200984 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200985 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200986 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200987 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988 break;
989 default:
David Sedlákb3077192019-06-19 10:55:37 +0200990 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200991 return LY_EVALID;
992 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200993 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200994 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100995checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996 /* mandatory substatements */
997 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200998 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200999 return LY_EVALID;
1000 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001 return ret;
1002}
1003
Michal Vaskoea5abea2018-09-18 13:10:54 +02001004/**
1005 * @brief Parse the revision-date statement.
1006 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001007 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001008 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001009 * @param[in,out] rev Array to store the parsed value in.
1010 * @param[in,out] exts Extension instances to add to.
1011 *
1012 * @return LY_ERR values.
1013 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001015parse_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 +02001016{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001017 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001018 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001019 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001020 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001021
1022 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001023 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001024 return LY_EVALID;
1025 }
1026
1027 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001028 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001029
1030 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001031 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001032 free(buf);
1033 return LY_EVALID;
1034 }
1035
1036 /* store value and spend buf if allocated */
1037 strncpy(rev, word, word_len);
1038 free(buf);
1039
Michal Vaskod989ba02020-08-24 10:59:24 +02001040 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001041 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001042 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001043 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001044 break;
1045 default:
David Sedlákb3077192019-06-19 10:55:37 +02001046 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001047 return LY_EVALID;
1048 }
1049 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001050 return ret;
1051}
1052
Michal Vaskoea5abea2018-09-18 13:10:54 +02001053/**
1054 * @brief Parse the include statement.
1055 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001056 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001057 * @param[in] module_name Name of the module to check name collisions.
Michal Vasko63f3d842020-07-08 10:10:14 +02001058 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001059 * @param[in,out] includes Parsed includes to add to.
1060 *
1061 * @return LY_ERR values.
1062 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001063static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001064parse_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 +02001065{
Radek Krejcid33273d2018-10-25 14:55:52 +02001066 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001068 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001069 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001070 struct lysp_include *inc;
1071
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001072 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073
1074 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001075 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001076
Radek Krejci011e4aa2020-09-04 15:22:31 +02001077 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001078
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001079 /* submodules share the namespace with the module names, so there must not be
1080 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001081 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001082 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001083 return LY_EVALID;
1084 }
1085
Michal Vaskod989ba02020-08-24 10:59:24 +02001086 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001087 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001088 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001089 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko63f3d842020-07-08 10:10:14 +02001090 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 +02001091 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001092 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001093 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko63f3d842020-07-08 10:10:14 +02001094 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 +02001095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001096 case LY_STMT_REVISION_DATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001097 LY_CHECK_RET(parse_revisiondate(ctx, in, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001099 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001100 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001101 break;
1102 default:
David Sedlákb3077192019-06-19 10:55:37 +02001103 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104 return LY_EVALID;
1105 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001106 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 return ret;
1108}
1109
Michal Vaskoea5abea2018-09-18 13:10:54 +02001110/**
1111 * @brief Parse the import statement.
1112 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001113 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001114 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vasko63f3d842020-07-08 10:10:14 +02001115 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001116 * @param[in,out] imports Parsed imports to add to.
1117 *
1118 * @return LY_ERR values.
1119 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001120static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001121parse_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 +02001122{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001123 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001125 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001126 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127 struct lysp_import *imp;
1128
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001129 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130
1131 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001132 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001133 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001134
Michal Vasko63f3d842020-07-08 10:10:14 +02001135 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001137 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02001138 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 +01001139 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 +02001140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001141 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001142 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko63f3d842020-07-08 10:10:14 +02001143 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 +02001144 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001145 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001146 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko63f3d842020-07-08 10:10:14 +02001147 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 +02001148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001149 case LY_STMT_REVISION_DATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001150 LY_CHECK_RET(parse_revisiondate(ctx, in, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001152 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001153 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001154 break;
1155 default:
David Sedlákb3077192019-06-19 10:55:37 +02001156 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157 return LY_EVALID;
1158 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001159 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001160 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001161checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001162 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001163 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164
1165 return ret;
1166}
1167
Michal Vaskoea5abea2018-09-18 13:10:54 +02001168/**
1169 * @brief Parse the revision statement.
1170 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001171 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001172 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001173 * @param[in,out] revs Parsed revisions to add to.
1174 *
1175 * @return LY_ERR values.
1176 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001177static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001178parse_revision(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001179{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001180 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001182 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001183 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184 struct lysp_revision *rev;
1185
Radek Krejci2c4e7172018-10-19 15:56:26 +02001186 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001187
1188 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001189 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190
1191 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001192 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001193 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 return LY_EVALID;
1195 }
1196
Radek Krejcib7db73a2018-10-24 14:18:40 +02001197 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 free(buf);
1199
Michal Vaskod989ba02020-08-24 10:59:24 +02001200 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001201 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001202 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001203 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 +02001204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001205 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001206 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 +02001207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001208 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001209 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001210 break;
1211 default:
David Sedlákb3077192019-06-19 10:55:37 +02001212 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001213 return LY_EVALID;
1214 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001216 return ret;
1217}
1218
Michal Vaskoea5abea2018-09-18 13:10:54 +02001219/**
1220 * @brief Parse a generic text field that can have more instances such as base.
1221 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001222 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001223 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001224 * @param[in] substmt Type of this substatement.
1225 * @param[in,out] texts Parsed values to add to.
1226 * @param[in] arg Type of the expected argument.
1227 * @param[in,out] exts Extension instances to add to.
1228 *
1229 * @return LY_ERR values.
1230 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001232parse_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 +02001233 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001235 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001237 const char **item;
1238 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001239 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240
1241 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001242 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001243
1244 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001245 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246
Radek Krejci011e4aa2020-09-04 15:22:31 +02001247 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Michal Vaskod989ba02020-08-24 10:59:24 +02001248 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001250 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001251 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252 break;
1253 default:
David Sedlákb3077192019-06-19 10:55:37 +02001254 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255 return LY_EVALID;
1256 }
1257 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001258 return ret;
1259}
1260
Michal Vaskoea5abea2018-09-18 13:10:54 +02001261/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001262 * @brief Parse a generic text field that can have more instances such as base.
1263 *
1264 * @param[in] ctx yang parser context for logging.
1265 * @param[in,out] in Input structure.
1266 * @param[in] substmt Type of this substatement.
1267 * @param[in,out] qnames Parsed qnames to add to.
1268 * @param[in] arg Type of the expected argument.
1269 * @param[in,out] exts Extension instances to add to.
1270 *
1271 * @return LY_ERR values.
1272 */
1273static LY_ERR
1274parse_qnames(struct lys_yang_parser_ctx *ctx, struct ly_in *in, LYEXT_SUBSTMT substmt, struct lysp_qname **qnames,
1275 enum yang_arg arg, struct lysp_ext_instance **exts)
1276{
1277 LY_ERR ret = LY_SUCCESS;
1278 char *buf, *word;
1279 struct lysp_qname *item;
1280 size_t word_len;
1281 enum ly_stmt kw;
1282
1283 /* allocate new pointer */
1284 LY_ARRAY_NEW_RET(ctx->ctx, *qnames, item, LY_EMEM);
1285
1286 /* get value */
1287 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
1288
1289 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
1290 item->mod = ctx->main_mod;
1291 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
1292 switch (kw) {
1293 case LY_STMT_EXTENSION_INSTANCE:
1294 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
1295 break;
1296 default:
1297 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
1298 return LY_EVALID;
1299 }
1300 }
1301 return ret;
1302}
1303
1304/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001305 * @brief Parse the config statement.
1306 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001307 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001308 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001309 * @param[in,out] flags Flags to add to.
1310 * @param[in,out] exts Extension instances to add to.
1311 *
1312 * @return LY_ERR values.
1313 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001315parse_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 +02001316{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001317 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001319 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001320 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001321
1322 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001323 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324 return LY_EVALID;
1325 }
1326
1327 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001328 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329
1330 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1331 *flags |= LYS_CONFIG_W;
1332 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1333 *flags |= LYS_CONFIG_R;
1334 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001335 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336 free(buf);
1337 return LY_EVALID;
1338 }
1339 free(buf);
1340
Michal Vaskod989ba02020-08-24 10:59:24 +02001341 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001342 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001343 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001344 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345 break;
1346 default:
David Sedlákb3077192019-06-19 10:55:37 +02001347 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 return LY_EVALID;
1349 }
1350 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001351 return ret;
1352}
1353
Michal Vaskoea5abea2018-09-18 13:10:54 +02001354/**
1355 * @brief Parse the mandatory statement.
1356 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001357 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001358 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001359 * @param[in,out] flags Flags to add to.
1360 * @param[in,out] exts Extension instances to add to.
1361 *
1362 * @return LY_ERR values.
1363 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001365parse_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 +02001366{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001367 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001369 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001370 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371
1372 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001373 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374 return LY_EVALID;
1375 }
1376
1377 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001378 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001379
1380 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1381 *flags |= LYS_MAND_TRUE;
1382 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1383 *flags |= LYS_MAND_FALSE;
1384 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001385 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 free(buf);
1387 return LY_EVALID;
1388 }
1389 free(buf);
1390
Michal Vaskod989ba02020-08-24 10:59:24 +02001391 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001392 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001393 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001394 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001395 break;
1396 default:
David Sedlákb3077192019-06-19 10:55:37 +02001397 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001398 return LY_EVALID;
1399 }
1400 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401 return ret;
1402}
1403
Michal Vaskoea5abea2018-09-18 13:10:54 +02001404/**
1405 * @brief Parse a restriction such as range or length.
1406 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001407 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001408 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001409 * @param[in] restr_kw Type of this particular restriction.
1410 * @param[in,out] exts Extension instances to add to.
1411 *
1412 * @return LY_ERR values.
1413 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001414static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001415parse_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 +02001416{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001417 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001418 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001419 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001420 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001421
1422 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001423 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001424
Michal Vaskob36053d2020-03-26 15:49:30 +01001425 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001426 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
1427 restr->arg.mod = ctx->main_mod;
Michal Vaskod989ba02020-08-24 10:59:24 +02001428 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001429 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001430 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001431 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 +02001432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001433 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001434 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 +02001435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001436 case LY_STMT_ERROR_APP_TAG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001437 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 +02001438 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001439 case LY_STMT_ERROR_MESSAGE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001440 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 +02001441 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001442 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001443 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001444 break;
1445 default:
David Sedlákb3077192019-06-19 10:55:37 +02001446 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001447 return LY_EVALID;
1448 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450 return ret;
1451}
1452
Michal Vaskoea5abea2018-09-18 13:10:54 +02001453/**
1454 * @brief Parse a restriction that can have more instances such as must.
1455 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001456 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001457 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001458 * @param[in] restr_kw Type of this particular restriction.
1459 * @param[in,out] restrs Restrictions to add to.
1460 *
1461 * @return LY_ERR values.
1462 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001464parse_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 +02001465{
1466 struct lysp_restr *restr;
1467
Radek Krejci2c4e7172018-10-19 15:56:26 +02001468 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02001469 return parse_restr(ctx, in, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001470}
1471
Michal Vaskoea5abea2018-09-18 13:10:54 +02001472/**
1473 * @brief Parse the status statement.
1474 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001475 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001476 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001477 * @param[in,out] flags Flags to add to.
1478 * @param[in,out] exts Extension instances to add to.
1479 *
1480 * @return LY_ERR values.
1481 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001482static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001483parse_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 +02001484{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001485 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001487 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001488 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489
1490 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001491 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001492 return LY_EVALID;
1493 }
1494
1495 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001496 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001497
1498 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1499 *flags |= LYS_STATUS_CURR;
1500 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1501 *flags |= LYS_STATUS_DEPRC;
1502 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1503 *flags |= LYS_STATUS_OBSLT;
1504 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001505 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001506 free(buf);
1507 return LY_EVALID;
1508 }
1509 free(buf);
1510
Michal Vaskod989ba02020-08-24 10:59:24 +02001511 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001512 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001513 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001514 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515 break;
1516 default:
David Sedlákb3077192019-06-19 10:55:37 +02001517 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001518 return LY_EVALID;
1519 }
1520 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001521 return ret;
1522}
1523
Michal Vaskoea5abea2018-09-18 13:10:54 +02001524/**
1525 * @brief Parse the when statement.
1526 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001527 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001528 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001529 * @param[in,out] when_p When pointer to parse to.
1530 *
1531 * @return LY_ERR values.
1532 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001533LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001534parse_when(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001536 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001538 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001539 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001540 struct lysp_when *when;
1541
1542 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001543 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544 return LY_EVALID;
1545 }
1546
1547 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001548 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001549 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550
1551 /* get value */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001552 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001553 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001554 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555
Michal Vaskod989ba02020-08-24 10:59:24 +02001556 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001557 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001558 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001559 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 +02001560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001561 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001562 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 +02001563 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001564 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001565 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001566 break;
1567 default:
David Sedlákb3077192019-06-19 10:55:37 +02001568 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569 return LY_EVALID;
1570 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572 return ret;
1573}
1574
Michal Vaskoea5abea2018-09-18 13:10:54 +02001575/**
1576 * @brief Parse the anydata or anyxml statement.
1577 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001578 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001579 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001580 * @param[in] kw Type of this particular keyword.
1581 * @param[in,out] siblings Siblings to add to.
1582 *
1583 * @return LY_ERR values.
1584 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001585LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001586parse_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 +02001587{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001588 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001589 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001590 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001591 struct lysp_node_anydata *any;
1592
David Sedlák60adc092019-08-06 15:57:02 +02001593 /* create new structure and insert into siblings */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02001594 LY_LIST_NEW_RET(ctx->ctx, siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001595
Radek Krejcid6b76452019-09-03 17:03:03 +02001596 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001597 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001598
Michal Vasko7fbc8162018-09-17 10:35:16 +02001599 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02001600 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001601 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001602
1603 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02001604 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001605 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001606 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001607 LY_CHECK_RET(parse_config(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001609 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001610 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 +02001611 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001612 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001613 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001615 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02001616 LY_CHECK_RET(parse_mandatory(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001618 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02001619 LY_CHECK_RET(parse_restrs(ctx, in, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001621 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001622 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 +02001623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001624 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02001625 LY_CHECK_RET(parse_status(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001627 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001628 LY_CHECK_RET(parse_when(ctx, in, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001630 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001631 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001632 break;
1633 default:
David Sedlákb3077192019-06-19 10:55:37 +02001634 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejci0f969882020-08-21 16:56:47 +02001635 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 return LY_EVALID;
1637 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639 return ret;
1640}
1641
Michal Vaskoea5abea2018-09-18 13:10:54 +02001642/**
1643 * @brief Parse the value or position statement. Substatement of type enum statement.
1644 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001645 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001646 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001647 * @param[in] val_kw Type of this particular keyword.
1648 * @param[in,out] value Value to write to.
1649 * @param[in,out] flags Flags to write to.
1650 * @param[in,out] exts Extension instances to add to.
1651 *
1652 * @return LY_ERR values.
1653 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001654LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001655parse_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 +02001656 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001657{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001658 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001659 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001660 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001661 long int num = 0;
1662 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001663 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664
1665 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001666 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667 return LY_EVALID;
1668 }
1669 *flags |= LYS_SET_VALUE;
1670
1671 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001672 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673
Radek Krejcid6b76452019-09-03 17:03:03 +02001674 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 +02001675 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001676 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001677 }
1678
1679 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001680 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001682 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001683 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001684 goto error;
1685 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686 } else {
1687 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001688 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001689 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001690 goto error;
1691 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001692 }
1693 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001694 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001695 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001696 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697 }
1698 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001699 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001700 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001701 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001702 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001703 *value = num;
1704 } else {
1705 *value = unum;
1706 }
1707 free(buf);
1708
Michal Vaskod989ba02020-08-24 10:59:24 +02001709 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001711 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001712 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 +02001713 break;
1714 default:
David Sedlákb3077192019-06-19 10:55:37 +02001715 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001716 return LY_EVALID;
1717 }
1718 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001719 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001720
1721error:
1722 free(buf);
1723 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724}
1725
Michal Vaskoea5abea2018-09-18 13:10:54 +02001726/**
1727 * @brief Parse the enum or bit statement. Substatement of type statement.
1728 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001729 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001730 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001731 * @param[in] enum_kw Type of this particular keyword.
1732 * @param[in,out] enums Enums or bits to add to.
1733 *
1734 * @return LY_ERR values.
1735 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001737parse_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 +02001738{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001739 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001741 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001742 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001743 struct lysp_type_enum *enm;
1744
Radek Krejci2c4e7172018-10-19 15:56:26 +02001745 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001746
1747 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001748 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 +02001749 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001750 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001751 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001752 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001753
Radek Krejci011e4aa2020-09-04 15:22:31 +02001754 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001755 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1756
Michal Vaskod989ba02020-08-24 10:59:24 +02001757 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001759 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001760 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 +02001761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001762 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001763 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001764 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, 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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001767 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 +02001768 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001769 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02001770 LY_CHECK_RET(parse_status(ctx, in, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001772 case LY_STMT_VALUE:
1773 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 +02001774 ly_stmt2str(enum_kw)), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +02001775 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 +02001776 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001777 case LY_STMT_POSITION:
1778 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 +02001779 ly_stmt2str(enum_kw)), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +02001780 LY_CHECK_RET(parse_type_enum_value_pos(ctx, in, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001781 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001782 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001783 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001784 break;
1785 default:
David Sedlákb3077192019-06-19 10:55:37 +02001786 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001787 return LY_EVALID;
1788 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001789 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001790 return ret;
1791}
1792
Michal Vaskoea5abea2018-09-18 13:10:54 +02001793/**
1794 * @brief Parse the fraction-digits statement. Substatement of type statement.
1795 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001796 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001797 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001798 * @param[in,out] fracdig Value to write to.
1799 * @param[in,out] exts Extension instances to add to.
1800 *
1801 * @return LY_ERR values.
1802 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001803static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001804parse_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 +02001805{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001806 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001807 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001808 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001809 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001810 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811
1812 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001813 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814 return LY_EVALID;
1815 }
1816
1817 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001818 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819
1820 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001821 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001822 free(buf);
1823 return LY_EVALID;
1824 }
1825
1826 errno = 0;
1827 num = strtoul(word, &ptr, 10);
1828 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001829 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001830 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001831 free(buf);
1832 return LY_EVALID;
1833 }
1834 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001835 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001836 free(buf);
1837 return LY_EVALID;
1838 }
1839 *fracdig = num;
1840 free(buf);
1841
Michal Vaskod989ba02020-08-24 10:59:24 +02001842 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001844 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001845 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846 break;
1847 default:
David Sedlákb3077192019-06-19 10:55:37 +02001848 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001849 return LY_EVALID;
1850 }
1851 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001852 return ret;
1853}
1854
Michal Vaskoea5abea2018-09-18 13:10:54 +02001855/**
1856 * @brief Parse the require-instance statement. Substatement of type statement.
1857 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001858 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001859 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001860 * @param[in,out] reqinst Value to write to.
1861 * @param[in,out] flags Flags to write to.
1862 * @param[in,out] exts Extension instances to add to.
1863 *
1864 * @return LY_ERR values.
1865 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001866static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001867parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001868 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001869{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001870 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001872 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001873 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874
1875 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001876 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877 return LY_EVALID;
1878 }
1879 *flags |= LYS_SET_REQINST;
1880
1881 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001882 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001883
1884 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1885 *reqinst = 1;
1886 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001887 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888 free(buf);
1889 return LY_EVALID;
1890 }
1891 free(buf);
1892
Michal Vaskod989ba02020-08-24 10:59:24 +02001893 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001894 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001895 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001896 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897 break;
1898 default:
David Sedlákb3077192019-06-19 10:55:37 +02001899 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001900 return LY_EVALID;
1901 }
1902 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001903 return ret;
1904}
1905
Michal Vaskoea5abea2018-09-18 13:10:54 +02001906/**
1907 * @brief Parse the modifier statement. Substatement of type pattern statement.
1908 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001909 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001910 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001911 * @param[in,out] pat Value to write to.
1912 * @param[in,out] exts Extension instances to add to.
1913 *
1914 * @return LY_ERR values.
1915 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001916static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001917parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **pat,
Radek Krejci0f969882020-08-21 16:56:47 +02001918 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001920 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001922 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001923 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924
1925 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001926 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927 return LY_EVALID;
1928 }
1929
1930 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001931 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932
1933 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001934 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001935 free(buf);
1936 return LY_EVALID;
1937 }
1938 free(buf);
1939
1940 /* replace the value in the dictionary */
1941 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001942 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001943 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001944 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001945
1946 assert(buf[0] == 0x06);
1947 buf[0] = 0x15;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001948 LY_CHECK_RET(lydict_insert_zc(ctx->ctx, buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949
Michal Vaskod989ba02020-08-24 10:59:24 +02001950 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001952 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001953 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001954 break;
1955 default:
David Sedlákb3077192019-06-19 10:55:37 +02001956 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001957 return LY_EVALID;
1958 }
1959 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960 return ret;
1961}
1962
Michal Vaskoea5abea2018-09-18 13:10:54 +02001963/**
1964 * @brief Parse the pattern statement. Substatement of type statement.
1965 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001966 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001967 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001968 * @param[in,out] patterns Restrictions to add to.
1969 *
1970 * @return LY_ERR values.
1971 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001972static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001973parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001974{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001975 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001977 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001978 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979 struct lysp_restr *restr;
1980
Radek Krejci2c4e7172018-10-19 15:56:26 +02001981 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001982
1983 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001984 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001985
1986 /* add special meaning first byte */
1987 if (buf) {
1988 buf = realloc(buf, word_len + 2);
1989 word = buf;
1990 } else {
1991 buf = malloc(word_len + 2);
1992 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001993 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001994 memmove(buf + 1, word, word_len);
1995 buf[0] = 0x06; /* pattern's default regular-match flag */
1996 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko7f45cf22020-10-01 12:49:44 +02001997 LY_CHECK_RET(lydict_insert_zc(ctx->ctx, buf, &restr->arg.str));
1998 restr->arg.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001999
Michal Vaskod989ba02020-08-24 10:59:24 +02002000 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002002 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002003 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 +02002004 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002005 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002006 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 +02002007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002008 case LY_STMT_ERROR_APP_TAG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002009 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 +02002010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002011 case LY_STMT_ERROR_MESSAGE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002012 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 +02002013 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002014 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002015 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002016 LY_CHECK_RET(parse_type_pattern_modifier(ctx, in, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002018 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002019 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020 break;
2021 default:
David Sedlákb3077192019-06-19 10:55:37 +02002022 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023 return LY_EVALID;
2024 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026 return ret;
2027}
2028
Michal Vaskoea5abea2018-09-18 13:10:54 +02002029/**
2030 * @brief Parse the type statement.
2031 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002032 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002033 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002034 * @param[in,out] type Type to wrote to.
2035 *
2036 * @return LY_ERR values.
2037 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002039parse_type(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002040{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002041 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002043 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002044 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002045 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046 struct lysp_type *nest_type;
2047
2048 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002049 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002050 return LY_EVALID;
2051 }
2052
2053 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002054 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002055 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002057 /* set module */
2058 type->mod = ctx->main_mod;
2059
Michal Vaskod989ba02020-08-24 10:59:24 +02002060 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002061 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002062 case LY_STMT_BASE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002063 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 +01002064 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002065 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002066 case LY_STMT_BIT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002067 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002068 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002069 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002070 case LY_STMT_ENUM:
Michal Vasko63f3d842020-07-08 10:10:14 +02002071 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002072 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002073 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002074 case LY_STMT_FRACTION_DIGITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002075 LY_CHECK_RET(parse_type_fracdigits(ctx, in, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002076 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002077 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002078 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002079 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002080 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081 return LY_EVALID;
2082 }
2083 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002084 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002085
Michal Vasko63f3d842020-07-08 10:10:14 +02002086 LY_CHECK_RET(parse_restr(ctx, in, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002087 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002089 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002090 if (type->path) {
2091 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2092 return LY_EVALID;
2093 }
2094
Michal Vasko63f3d842020-07-08 10:10:14 +02002095 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 +02002096 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 +02002097 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
2098 lydict_remove(ctx->ctx, str_path);
2099 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002100 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002102 case LY_STMT_PATTERN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002103 LY_CHECK_RET(parse_type_pattern(ctx, in, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002104 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002106 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002108 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109 return LY_EVALID;
2110 }
2111 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002112 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002113
Michal Vasko63f3d842020-07-08 10:10:14 +02002114 LY_CHECK_RET(parse_restr(ctx, in, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002115 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002117 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002118 LY_CHECK_RET(parse_type_reqinstance(ctx, in, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002119 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002121 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002122 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02002123 LY_CHECK_RET(parse_type(ctx, in, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002124 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002126 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002127 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002128 break;
2129 default:
David Sedlákb3077192019-06-19 10:55:37 +02002130 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002131 return LY_EVALID;
2132 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002133 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134 return ret;
2135}
2136
Michal Vaskoea5abea2018-09-18 13:10:54 +02002137/**
2138 * @brief Parse the leaf statement.
2139 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002140 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002141 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002142 * @param[in,out] siblings Siblings to add to.
2143 *
2144 * @return LY_ERR values.
2145 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002146LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002147parse_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 +02002148{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002149 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002150 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002151 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002152 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153 struct lysp_node_leaf *leaf;
2154
David Sedlák60adc092019-08-06 15:57:02 +02002155 /* create new leaf structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002156 LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002157 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002158 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159
Michal Vasko7fbc8162018-09-17 10:35:16 +02002160 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002161 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002162 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163
2164 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002165 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002167 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002168 LY_CHECK_RET(parse_config(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002170 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002171 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
2172 leaf->dflt.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002174 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002175 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 +02002176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002177 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002178 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002180 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002181 LY_CHECK_RET(parse_mandatory(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002183 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002184 LY_CHECK_RET(parse_restrs(ctx, in, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002186 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002187 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 +02002188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002189 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002190 LY_CHECK_RET(parse_status(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002192 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002193 LY_CHECK_RET(parse_type(ctx, in, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002195 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002196 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 +02002197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002198 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002199 LY_CHECK_RET(parse_when(ctx, in, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002201 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002202 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002203 break;
2204 default:
David Sedlákb3077192019-06-19 10:55:37 +02002205 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002206 return LY_EVALID;
2207 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002208 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002209 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002210checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211 /* mandatory substatements */
2212 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002213 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 return LY_EVALID;
2215 }
2216
2217 return ret;
2218}
2219
Michal Vaskoea5abea2018-09-18 13:10:54 +02002220/**
2221 * @brief Parse the max-elements statement.
2222 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002223 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002224 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002225 * @param[in,out] max Value to write to.
2226 * @param[in,out] flags Flags to write to.
2227 * @param[in,out] exts Extension instances to add to.
2228 *
2229 * @return LY_ERR values.
2230 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002231LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002232parse_maxelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *max, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002233 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002235 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002237 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002238 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002239 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240
2241 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002242 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002243 return LY_EVALID;
2244 }
2245 *flags |= LYS_SET_MAX;
2246
2247 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002248 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002249
2250 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002251 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002252 free(buf);
2253 return LY_EVALID;
2254 }
2255
Radek Krejci7f9b6512019-09-18 13:11:09 +02002256 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002257 errno = 0;
2258 num = strtoul(word, &ptr, 10);
2259 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002260 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002261 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002262 free(buf);
2263 return LY_EVALID;
2264 }
2265 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002266 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267 free(buf);
2268 return LY_EVALID;
2269 }
2270
2271 *max = num;
2272 }
2273 free(buf);
2274
Michal Vaskod989ba02020-08-24 10:59:24 +02002275 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002277 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002278 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 break;
2280 default:
David Sedlákb3077192019-06-19 10:55:37 +02002281 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002282 return LY_EVALID;
2283 }
2284 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002285 return ret;
2286}
2287
Michal Vaskoea5abea2018-09-18 13:10:54 +02002288/**
2289 * @brief Parse the min-elements statement.
2290 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002291 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002292 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002293 * @param[in,out] min Value to write to.
2294 * @param[in,out] flags Flags to write to.
2295 * @param[in,out] exts Extension instances to add to.
2296 *
2297 * @return LY_ERR values.
2298 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002299LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002300parse_minelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *min, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002301 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002303 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002305 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002306 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002307 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308
2309 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002310 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 return LY_EVALID;
2312 }
2313 *flags |= LYS_SET_MIN;
2314
2315 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002316 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317
2318 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002319 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002320 free(buf);
2321 return LY_EVALID;
2322 }
2323
2324 errno = 0;
2325 num = strtoul(word, &ptr, 10);
2326 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002327 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002328 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002329 free(buf);
2330 return LY_EVALID;
2331 }
2332 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002333 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 free(buf);
2335 return LY_EVALID;
2336 }
2337 *min = num;
2338 free(buf);
2339
Michal Vaskod989ba02020-08-24 10:59:24 +02002340 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002341 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002342 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002343 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 break;
2345 default:
David Sedlákb3077192019-06-19 10:55:37 +02002346 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347 return LY_EVALID;
2348 }
2349 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350 return ret;
2351}
2352
Michal Vaskoea5abea2018-09-18 13:10:54 +02002353/**
2354 * @brief Parse the ordered-by statement.
2355 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002356 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002357 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002358 * @param[in,out] flags Flags to write to.
2359 * @param[in,out] exts Extension instances to add to.
2360 *
2361 * @return LY_ERR values.
2362 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002364parse_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 +02002365{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002366 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002368 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002369 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370
2371 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002372 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002373 return LY_EVALID;
2374 }
2375
2376 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002377 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378
2379 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2380 *flags |= LYS_ORDBY_SYSTEM;
2381 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2382 *flags |= LYS_ORDBY_USER;
2383 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002384 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385 free(buf);
2386 return LY_EVALID;
2387 }
2388 free(buf);
2389
Michal Vaskod989ba02020-08-24 10:59:24 +02002390 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002392 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002393 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 break;
2395 default:
David Sedlákb3077192019-06-19 10:55:37 +02002396 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 return LY_EVALID;
2398 }
2399 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 return ret;
2401}
2402
Michal Vaskoea5abea2018-09-18 13:10:54 +02002403/**
2404 * @brief Parse the leaf-list statement.
2405 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002406 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002407 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002408 * @param[in,out] siblings Siblings to add to.
2409 *
2410 * @return LY_ERR values.
2411 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002412LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002413parse_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 +02002414{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002415 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002417 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002418 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419 struct lysp_node_leaflist *llist;
2420
David Sedlák60adc092019-08-06 15:57:02 +02002421 /* create new leaf-list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002422 LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002424 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002425
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002427 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002428 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002429
2430 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002431 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002433 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002434 LY_CHECK_RET(parse_config(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002436 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002437 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002438 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, 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_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002441 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 +02002442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002443 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002444 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002446 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002447 LY_CHECK_RET(parse_maxelements(ctx, in, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002449 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002450 LY_CHECK_RET(parse_minelements(ctx, in, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002452 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002453 LY_CHECK_RET(parse_restrs(ctx, in, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002455 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002456 LY_CHECK_RET(parse_orderedby(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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002459 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 +02002460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002461 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002462 LY_CHECK_RET(parse_status(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002464 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002465 LY_CHECK_RET(parse_type(ctx, in, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002467 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002468 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 +02002469 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002470 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002471 LY_CHECK_RET(parse_when(ctx, in, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002472 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002473 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002474 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 break;
2476 default:
David Sedlákb3077192019-06-19 10:55:37 +02002477 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 return LY_EVALID;
2479 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002481 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002482checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483 /* mandatory substatements */
2484 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002485 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002486 return LY_EVALID;
2487 }
2488
2489 return ret;
2490}
2491
Michal Vaskoea5abea2018-09-18 13:10:54 +02002492/**
2493 * @brief Parse the refine statement.
2494 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002495 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002496 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002497 * @param[in,out] refines Refines to add to.
2498 *
2499 * @return LY_ERR values.
2500 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002502parse_refine(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002504 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002506 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002507 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508 struct lysp_refine *rf;
2509
Radek Krejci2c4e7172018-10-19 15:56:26 +02002510 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002511
2512 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002513 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002514 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002515 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516
Michal Vaskod989ba02020-08-24 10:59:24 +02002517 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002519 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002520 LY_CHECK_RET(parse_config(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002521 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002522 case LY_STMT_DEFAULT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002523 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 +02002524 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002525 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002526 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 +02002527 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002528 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002529 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002530 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002532 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002533 LY_CHECK_RET(parse_maxelements(ctx, in, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002535 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002536 LY_CHECK_RET(parse_minelements(ctx, in, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002538 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002539 LY_CHECK_RET(parse_restrs(ctx, in, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002541 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002542 LY_CHECK_RET(parse_mandatory(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002544 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002545 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 +02002546 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002547 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002548 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 +02002549 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002550 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002551 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 break;
2553 default:
David Sedlákb3077192019-06-19 10:55:37 +02002554 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 return LY_EVALID;
2556 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 return ret;
2559}
2560
Michal Vaskoea5abea2018-09-18 13:10:54 +02002561/**
2562 * @brief Parse the typedef statement.
2563 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002564 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002565 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002566 * @param[in,out] typedefs Typedefs to add to.
2567 *
2568 * @return LY_ERR values.
2569 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002571parse_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 +02002572{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002573 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002575 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002576 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577 struct lysp_tpdf *tpdf;
2578
Radek Krejci2c4e7172018-10-19 15:56:26 +02002579 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002580
2581 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002582 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002583 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584
2585 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002586 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002588 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002589 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
2590 tpdf->dflt.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002591 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002592 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002593 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 +02002594 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002595 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002596 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 +02002597 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002598 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002599 LY_CHECK_RET(parse_status(ctx, in, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002601 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002602 LY_CHECK_RET(parse_type(ctx, in, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002604 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002605 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 +02002606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002607 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002608 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 break;
2610 default:
David Sedlákb3077192019-06-19 10:55:37 +02002611 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 return LY_EVALID;
2613 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002615 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002616checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002617 /* mandatory substatements */
2618 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002619 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620 return LY_EVALID;
2621 }
2622
Radek Krejcibbe09a92018-11-08 09:36:54 +01002623 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002624 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002625 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002626 }
2627
Michal Vasko7fbc8162018-09-17 10:35:16 +02002628 return ret;
2629}
2630
Michal Vaskoea5abea2018-09-18 13:10:54 +02002631/**
2632 * @brief Parse the input or output statement.
2633 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002634 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002635 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002636 * @param[in] kw Type of this particular keyword
2637 * @param[in,out] inout_p Input/output pointer to write to.
2638 *
2639 * @return LY_ERR values.
2640 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02002642parse_inout(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt inout_kw, struct lysp_node *parent,
2643 struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002645 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002647 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002648 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002650 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002651 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 return LY_EVALID;
2653 }
2654
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002655 /* initiate structure */
Michal Vasko22df3f02020-08-24 13:29:22 +02002656 inout_p->nodetype = &((struct lysp_action *)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002657 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658
2659 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002660 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002662 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002663 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002664 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002665 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002666 LY_CHECK_RET(parse_any(ctx, in, kw, (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_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002669 LY_CHECK_RET(parse_choice(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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002672 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002674 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002675 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002678 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002681 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002683 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002684 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002686 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002687 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, in, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002689 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002690 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Michal Vasko63f3d842020-07-08 10:10:14 +02002691 LY_CHECK_RET(parse_restrs(ctx, in, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002693 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002694 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002696 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002697 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002698 break;
2699 default:
David Sedlákb3077192019-06-19 10:55:37 +02002700 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002701 return LY_EVALID;
2702 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002704 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002705
Radek Krejci7fc68292019-06-12 13:51:09 +02002706checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002707 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002708 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 +02002709
Michal Vaskob83af8a2020-01-06 09:49:22 +01002710 if (!inout_p->data) {
2711 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2712 return LY_EVALID;
2713 }
2714
Michal Vasko7fbc8162018-09-17 10:35:16 +02002715 return ret;
2716}
2717
Michal Vaskoea5abea2018-09-18 13:10:54 +02002718/**
2719 * @brief Parse the action statement.
2720 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002721 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002722 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002723 * @param[in,out] actions Actions to add to.
2724 *
2725 * @return LY_ERR values.
2726 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002727LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002728parse_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 +02002729{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002730 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002732 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002733 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734 struct lysp_action *act;
2735
Radek Krejci2c4e7172018-10-19 15:56:26 +02002736 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737
2738 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002739 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002740 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002741 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002742 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002743
Michal Vasko63f3d842020-07-08 10:10:14 +02002744 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002746 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002747 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 +02002748 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002749 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002750 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002752 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002753 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 +02002754 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002755 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002756 LY_CHECK_RET(parse_status(ctx, in, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757 break;
2758
Radek Krejcid6b76452019-09-03 17:03:03 +02002759 case LY_STMT_INPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002760 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002762 case LY_STMT_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002763 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764 break;
2765
Radek Krejcid6b76452019-09-03 17:03:03 +02002766 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002767 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, in, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002768 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002769 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002770 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002771 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002772 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002773 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002774 break;
2775 default:
David Sedlákb3077192019-06-19 10:55:37 +02002776 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002777 return LY_EVALID;
2778 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002779 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002780 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002781
2782 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2783 if (!act->input.nodetype) {
2784 act->input.nodetype = LYS_INPUT;
2785 act->input.parent = (struct lysp_node *)act;
2786 }
2787 if (!act->output.nodetype) {
2788 act->output.nodetype = LYS_OUTPUT;
2789 act->output.parent = (struct lysp_node *)act;
2790 }
2791
Radek Krejci7fc68292019-06-12 13:51:09 +02002792checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002793 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002794 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002795
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796 return ret;
2797}
2798
Michal Vaskoea5abea2018-09-18 13:10:54 +02002799/**
2800 * @brief Parse the notification statement.
2801 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002802 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002803 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002804 * @param[in,out] notifs Notifications to add to.
2805 *
2806 * @return LY_ERR values.
2807 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002808LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002809parse_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 +02002810{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002811 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002812 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002813 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002814 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002815 struct lysp_notif *notif;
2816
Radek Krejci2c4e7172018-10-19 15:56:26 +02002817 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818
2819 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002820 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002821 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002822 notif->nodetype = LYS_NOTIF;
2823 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002824
Michal Vasko63f3d842020-07-08 10:10:14 +02002825 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002827 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002828 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 +02002829 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002830 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002831 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002832 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002833 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002834 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 +02002835 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002836 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002837 LY_CHECK_RET(parse_status(ctx, in, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002838 break;
2839
Radek Krejcid6b76452019-09-03 17:03:03 +02002840 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002841 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002842 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002843 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002844 LY_CHECK_RET(parse_any(ctx, in, kw, (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_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002847 LY_CHECK_RET(parse_case(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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002850 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002852 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002853 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002856 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002858 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002859 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002860 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002861 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002862 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002863 break;
2864
Radek Krejcid6b76452019-09-03 17:03:03 +02002865 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002866 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Michal Vasko63f3d842020-07-08 10:10:14 +02002867 LY_CHECK_RET(parse_restrs(ctx, in, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002868 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002869 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002870 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, in, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002871 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002872 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002873 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002874 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002875 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002876 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002877 break;
2878 default:
David Sedlákb3077192019-06-19 10:55:37 +02002879 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 return LY_EVALID;
2881 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002882 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002883 LY_CHECK_RET(ret);
2884checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002885 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002886 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002887
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888 return ret;
2889}
2890
Michal Vaskoea5abea2018-09-18 13:10:54 +02002891/**
2892 * @brief Parse the grouping statement.
2893 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002894 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002895 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002896 * @param[in,out] groupings Groupings to add to.
2897 *
2898 * @return LY_ERR values.
2899 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002900LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002901parse_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 +02002902{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002903 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002905 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002906 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 struct lysp_grp *grp;
2908
Radek Krejci2c4e7172018-10-19 15:56:26 +02002909 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910
2911 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002912 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002913 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002914 grp->nodetype = LYS_GROUPING;
2915 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916
Michal Vasko63f3d842020-07-08 10:10:14 +02002917 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002918 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002919 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002920 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 +02002921 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002922 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002923 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 +02002924 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002925 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002926 LY_CHECK_RET(parse_status(ctx, in, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002927 break;
2928
Radek Krejcid6b76452019-09-03 17:03:03 +02002929 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002930 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002931 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002932 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002933 LY_CHECK_RET(parse_any(ctx, in, kw, (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_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002936 LY_CHECK_RET(parse_choice(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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002939 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002940 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002941 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002942 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002945 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002947 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002948 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002950 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002951 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952 break;
2953
Radek Krejcid6b76452019-09-03 17:03:03 +02002954 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002955 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)grp, in, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002956 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002957 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002958 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Michal Vasko22df3f02020-08-24 13:29:22 +02002959 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002961 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002962 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002963 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002964 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002965 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Michal Vasko22df3f02020-08-24 13:29:22 +02002966 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002968 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002969 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970 break;
2971 default:
David Sedlákb3077192019-06-19 10:55:37 +02002972 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973 return LY_EVALID;
2974 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002976 LY_CHECK_RET(ret);
2977checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002978 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002979 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 +02002980
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981 return ret;
2982}
2983
Michal Vaskoea5abea2018-09-18 13:10:54 +02002984/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002985 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002986 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002987 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002988 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002989 * @param[in,out] augments Augments to add to.
2990 *
2991 * @return LY_ERR values.
2992 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002993LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002994parse_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 +02002995{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002996 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002998 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002999 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 struct lysp_augment *aug;
3001
Radek Krejci2c4e7172018-10-19 15:56:26 +02003002 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003
3004 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003005 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003006 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003007 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003008 aug->nodetype = LYS_AUGMENT;
3009 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010
Michal Vasko63f3d842020-07-08 10:10:14 +02003011 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003013 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003014 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 +02003015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003016 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003017 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003019 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003020 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 +02003021 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003022 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003023 LY_CHECK_RET(parse_status(ctx, in, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003025 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003026 LY_CHECK_RET(parse_when(ctx, in, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 break;
3028
Radek Krejcid6b76452019-09-03 17:03:03 +02003029 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003030 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003031 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003032 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003033 LY_CHECK_RET(parse_any(ctx, in, kw, (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_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003036 LY_CHECK_RET(parse_case(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_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003039 LY_CHECK_RET(parse_choice(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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003042 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003044 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003045 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003048 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003050 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003051 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003053 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003054 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003055 break;
3056
Radek Krejcid6b76452019-09-03 17:03:03 +02003057 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003058 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Michal Vasko22df3f02020-08-24 13:29:22 +02003059 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003061 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003062 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Michal Vasko22df3f02020-08-24 13:29:22 +02003063 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003065 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003066 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003067 break;
3068 default:
David Sedlákb3077192019-06-19 10:55:37 +02003069 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003070 return LY_EVALID;
3071 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003073 LY_CHECK_RET(ret);
3074checks:
3075 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003076 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 +02003077
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 return ret;
3079}
3080
Michal Vaskoea5abea2018-09-18 13:10:54 +02003081/**
3082 * @brief Parse the uses statement.
3083 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003084 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003085 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003086 * @param[in,out] siblings Siblings to add to.
3087 *
3088 * @return LY_ERR values.
3089 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003090LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003091parse_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 +02003092{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003093 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003094 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003095 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003096 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097 struct lysp_node_uses *uses;
3098
David Sedlák60adc092019-08-06 15:57:02 +02003099 /* create uses structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003100 LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003102 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003103
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003105 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003106 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107
3108 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003109 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003111 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003112 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 +02003113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003114 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003115 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003116 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003117 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003118 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 +02003119 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003120 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003121 LY_CHECK_RET(parse_status(ctx, in, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003123 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003124 LY_CHECK_RET(parse_when(ctx, in, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125 break;
3126
Radek Krejcid6b76452019-09-03 17:03:03 +02003127 case LY_STMT_REFINE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003128 LY_CHECK_RET(parse_refine(ctx, in, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003130 case LY_STMT_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02003131 LY_CHECK_RET(parse_augment(ctx, in, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003133 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003134 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 break;
3136 default:
David Sedlákb3077192019-06-19 10:55:37 +02003137 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003138 return LY_EVALID;
3139 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003141checks:
3142 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003143 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003144
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 return ret;
3146}
3147
Michal Vaskoea5abea2018-09-18 13:10:54 +02003148/**
3149 * @brief Parse the case statement.
3150 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003151 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003152 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003153 * @param[in,out] siblings Siblings to add to.
3154 *
3155 * @return LY_ERR values.
3156 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003157LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003158parse_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 +02003159{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003160 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003162 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003163 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164 struct lysp_node_case *cas;
3165
David Sedlák60adc092019-08-06 15:57:02 +02003166 /* create new case structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003167 LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003168 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003169 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003172 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003173 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174
3175 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02003176 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003178 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003179 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 +02003180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003181 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003182 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003184 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003185 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 +02003186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003187 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003188 LY_CHECK_RET(parse_status(ctx, in, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003189 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003190 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003191 LY_CHECK_RET(parse_when(ctx, in, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192 break;
3193
Radek Krejcid6b76452019-09-03 17:03:03 +02003194 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003195 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003196 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003197 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003198 LY_CHECK_RET(parse_any(ctx, in, kw, (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_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003201 LY_CHECK_RET(parse_choice(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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003204 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003206 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003207 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003210 LY_CHECK_RET(parse_leaflist(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_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003213 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003216 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003218 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003219 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 break;
3221 default:
David Sedlákb3077192019-06-19 10:55:37 +02003222 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223 return LY_EVALID;
3224 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003226 return ret;
3227}
3228
Michal Vaskoea5abea2018-09-18 13:10:54 +02003229/**
3230 * @brief Parse the choice statement.
3231 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003232 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003233 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003234 * @param[in,out] siblings Siblings to add to.
3235 *
3236 * @return LY_ERR values.
3237 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003238LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003239parse_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 +02003240{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003241 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003243 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003244 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003245 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246
David Sedlák60adc092019-08-06 15:57:02 +02003247 /* create new choice structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003248 LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003249 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003250 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003253 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003254 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255
3256 /* parse substatements */
Michal Vasko7f45cf22020-10-01 12:49:44 +02003257 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003258 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003259 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003260 LY_CHECK_RET(parse_config(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003261 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003262 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003263 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 +02003264 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003265 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003266 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, 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_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003269 LY_CHECK_RET(parse_mandatory(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_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003272 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 +02003273 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003274 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003275 LY_CHECK_RET(parse_status(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003277 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003278 LY_CHECK_RET(parse_when(ctx, in, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003280 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003281 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
3282 &choice->exts));
3283 choice->dflt.mod = ctx->main_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003284 break;
3285
Radek Krejcid6b76452019-09-03 17:03:03 +02003286 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003287 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003288 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003289 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003290 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003291 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003292 case LY_STMT_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003293 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003295 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003296 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Michal Vasko22df3f02020-08-24 13:29:22 +02003297 LY_CHECK_RET(parse_choice(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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003300 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003302 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003303 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003306 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003308 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003309 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003311 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003312 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313 break;
3314 default:
David Sedlákb3077192019-06-19 10:55:37 +02003315 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316 return LY_EVALID;
3317 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 return ret;
3320}
3321
Michal Vaskoea5abea2018-09-18 13:10:54 +02003322/**
3323 * @brief Parse the container statement.
3324 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003325 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003326 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003327 * @param[in,out] siblings Siblings to add to.
3328 *
3329 * @return LY_ERR values.
3330 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003331LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003332parse_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 +02003333{
3334 LY_ERR ret = 0;
3335 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003336 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003337 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338 struct lysp_node_container *cont;
3339
David Sedlák60adc092019-08-06 15:57:02 +02003340 /* create new container structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003341 LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003343 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003346 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003347 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003348
3349 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003350 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003352 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003353 LY_CHECK_RET(parse_config(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003354 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003355 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003356 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 +02003357 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003358 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003359 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003360 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003361 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003362 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 +02003363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003364 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003365 LY_CHECK_RET(parse_status(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003367 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003368 LY_CHECK_RET(parse_when(ctx, in, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003370 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003371 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 +02003372 break;
3373
Radek Krejcid6b76452019-09-03 17:03:03 +02003374 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003375 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003376 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003377 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003378 LY_CHECK_RET(parse_any(ctx, in, kw, (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_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003381 LY_CHECK_RET(parse_choice(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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003384 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003386 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003387 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003390 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003392 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003393 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003395 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003396 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003397 break;
3398
Radek Krejcid6b76452019-09-03 17:03:03 +02003399 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003400 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, in, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003402 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003403 LY_CHECK_RET(parse_restrs(ctx, in, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003405 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003406 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Michal Vasko22df3f02020-08-24 13:29:22 +02003407 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003409 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02003410 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003412 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003413 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Michal Vasko22df3f02020-08-24 13:29:22 +02003414 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003416 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003417 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 break;
3419 default:
David Sedlákb3077192019-06-19 10:55:37 +02003420 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 return LY_EVALID;
3422 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003424checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003425 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003426 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 +02003427 return ret;
3428}
3429
Michal Vaskoea5abea2018-09-18 13:10:54 +02003430/**
3431 * @brief Parse the list statement.
3432 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003433 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003434 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003435 * @param[in,out] siblings Siblings to add to.
3436 *
3437 * @return LY_ERR values.
3438 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003439LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003440parse_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 +02003441{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003442 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003444 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003445 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 struct lysp_node_list *list;
3447
David Sedlák60adc092019-08-06 15:57:02 +02003448 /* create new list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003449 LY_LIST_NEW_RET(ctx->ctx, siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003450 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003451 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452
Michal Vasko7fbc8162018-09-17 10:35:16 +02003453 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003454 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003455 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456
3457 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003458 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003461 LY_CHECK_RET(parse_config(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003463 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003464 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 +02003465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003466 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003467 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003468 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003469 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003470 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 +02003471 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003472 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003473 LY_CHECK_RET(parse_status(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003475 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003476 LY_CHECK_RET(parse_when(ctx, in, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003477 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003478 case LY_STMT_KEY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003479 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 +02003480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003481 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003482 LY_CHECK_RET(parse_maxelements(ctx, in, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003484 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003485 LY_CHECK_RET(parse_minelements(ctx, in, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003487 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003488 LY_CHECK_RET(parse_orderedby(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003490 case LY_STMT_UNIQUE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003491 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003492 break;
3493
Radek Krejcid6b76452019-09-03 17:03:03 +02003494 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003495 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003496 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003497 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003498 LY_CHECK_RET(parse_any(ctx, in, kw, (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_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003501 LY_CHECK_RET(parse_choice(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_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003504 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003506 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003507 LY_CHECK_RET(parse_leaf(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_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003510 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003512 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003513 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003514 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003515 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003516 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 break;
3518
Radek Krejcid6b76452019-09-03 17:03:03 +02003519 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003520 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, in, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003522 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003523 LY_CHECK_RET(parse_restrs(ctx, in, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003524 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003525 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003526 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Michal Vasko22df3f02020-08-24 13:29:22 +02003527 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003529 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02003530 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003532 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003533 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Michal Vasko22df3f02020-08-24 13:29:22 +02003534 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003536 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003537 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538 break;
3539 default:
David Sedlákb3077192019-06-19 10:55:37 +02003540 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 return LY_EVALID;
3542 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003544 LY_CHECK_RET(ret);
3545checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003546 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003547 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 +02003548
Michal Vasko7fbc8162018-09-17 10:35:16 +02003549 return ret;
3550}
3551
Michal Vaskoea5abea2018-09-18 13:10:54 +02003552/**
3553 * @brief Parse the yin-element statement.
3554 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003555 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003556 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003557 * @param[in,out] flags Flags to write to.
3558 * @param[in,out] exts Extension instances to add to.
3559 *
3560 * @return LY_ERR values.
3561 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003562static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003563parse_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 +02003564{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003565 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003567 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003568 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569
3570 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003571 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003572 return LY_EVALID;
3573 }
3574
3575 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003576 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577
3578 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3579 *flags |= LYS_YINELEM_TRUE;
3580 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3581 *flags |= LYS_YINELEM_FALSE;
3582 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003583 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 free(buf);
3585 return LY_EVALID;
3586 }
3587 free(buf);
3588
Michal Vaskod989ba02020-08-24 10:59:24 +02003589 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003591 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003592 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003593 LY_CHECK_RET(ret);
3594 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 default:
David Sedlákb3077192019-06-19 10:55:37 +02003596 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 return LY_EVALID;
3598 }
3599 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 return ret;
3601}
3602
Michal Vaskoea5abea2018-09-18 13:10:54 +02003603/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003604 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003605 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003606 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003607 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003608 * @param[in,out] argument Value to write to.
3609 * @param[in,out] flags Flags to write to.
3610 * @param[in,out] exts Extension instances to add to.
3611 *
3612 * @return LY_ERR values.
3613 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003614static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003615parse_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 +02003616{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003617 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003619 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003620 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621
3622 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003623 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003624 return LY_EVALID;
3625 }
3626
3627 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003628 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003629 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003630
Michal Vaskod989ba02020-08-24 10:59:24 +02003631 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003633 case LY_STMT_YIN_ELEMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003634 LY_CHECK_RET(parse_yinelement(ctx, in, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003636 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003637 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 break;
3639 default:
David Sedlákb3077192019-06-19 10:55:37 +02003640 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 return LY_EVALID;
3642 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 return ret;
3645}
3646
Michal Vaskoea5abea2018-09-18 13:10:54 +02003647/**
3648 * @brief Parse the extension statement.
3649 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003650 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003651 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003652 * @param[in,out] extensions Extensions to add to.
3653 *
3654 * @return LY_ERR values.
3655 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003656static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003657parse_extension(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003659 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003661 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003662 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003663 struct lysp_ext *ex;
3664
Radek Krejci2c4e7172018-10-19 15:56:26 +02003665 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003666
3667 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003668 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003669 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003670
Michal Vaskod989ba02020-08-24 10:59:24 +02003671 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003673 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003674 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 +02003675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003676 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003677 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 +02003678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003679 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003680 LY_CHECK_RET(parse_status(ctx, in, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003682 case LY_STMT_ARGUMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003683 LY_CHECK_RET(parse_argument(ctx, in, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003684 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003685 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003686 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687 break;
3688 default:
David Sedlákb3077192019-06-19 10:55:37 +02003689 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 return LY_EVALID;
3691 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003693 return ret;
3694}
3695
Michal Vaskoea5abea2018-09-18 13:10:54 +02003696/**
3697 * @brief Parse the deviate statement.
3698 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003699 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003700 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003701 * @param[in,out] deviates Deviates to add to.
3702 *
3703 * @return LY_ERR values.
3704 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003705LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003706parse_deviate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003707{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003708 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003710 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003711 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003712 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003713 struct lysp_deviate_add *d_add = NULL;
3714 struct lysp_deviate_rpl *d_rpl = NULL;
3715 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003716 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3717 struct lysp_restr **d_musts = NULL;
3718 uint16_t *d_flags = 0;
3719 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720
3721 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003722 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003723
3724 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3725 dev_mod = LYS_DEV_NOT_SUPPORTED;
3726 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3727 dev_mod = LYS_DEV_ADD;
3728 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3729 dev_mod = LYS_DEV_REPLACE;
3730 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3731 dev_mod = LYS_DEV_DELETE;
3732 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003733 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 free(buf);
3735 return LY_EVALID;
3736 }
3737 free(buf);
3738
3739 /* create structure */
3740 switch (dev_mod) {
3741 case LYS_DEV_NOT_SUPPORTED:
3742 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003743 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003744 break;
3745 case LYS_DEV_ADD:
3746 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003747 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748 d = (struct lysp_deviate *)d_add;
3749 d_units = &d_add->units;
3750 d_uniques = &d_add->uniques;
3751 d_dflts = &d_add->dflts;
3752 d_musts = &d_add->musts;
3753 d_flags = &d_add->flags;
3754 d_min = &d_add->min;
3755 d_max = &d_add->max;
3756 break;
3757 case LYS_DEV_REPLACE:
3758 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003759 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760 d = (struct lysp_deviate *)d_rpl;
3761 d_units = &d_rpl->units;
3762 d_flags = &d_rpl->flags;
3763 d_min = &d_rpl->min;
3764 d_max = &d_rpl->max;
3765 break;
3766 case LYS_DEV_DELETE:
3767 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003768 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769 d = (struct lysp_deviate *)d_del;
3770 d_units = &d_del->units;
3771 d_uniques = &d_del->uniques;
3772 d_dflts = &d_del->dflts;
3773 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003774 break;
3775 default:
3776 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003777 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 }
3779 d->mod = dev_mod;
3780
3781 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003782 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783
Michal Vaskod989ba02020-08-24 10:59:24 +02003784 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003786 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003787 switch (dev_mod) {
3788 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003789 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003790 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791 return LY_EVALID;
3792 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003793 LY_CHECK_RET(parse_config(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794 break;
3795 }
3796 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003797 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798 switch (dev_mod) {
3799 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003800 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 return LY_EVALID;
3802 case LYS_DEV_REPLACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003803 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 +02003804 break;
3805 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003806 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 +02003807 break;
3808 }
3809 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003810 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003811 switch (dev_mod) {
3812 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003813 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003814 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003815 return LY_EVALID;
3816 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003817 LY_CHECK_RET(parse_mandatory(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003818 break;
3819 }
3820 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003821 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003822 switch (dev_mod) {
3823 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003824 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003825 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 return LY_EVALID;
3827 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003828 LY_CHECK_RET(parse_maxelements(ctx, in, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 break;
3830 }
3831 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003832 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003833 switch (dev_mod) {
3834 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003835 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003836 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003837 return LY_EVALID;
3838 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003839 LY_CHECK_RET(parse_minelements(ctx, in, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003840 break;
3841 }
3842 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003843 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844 switch (dev_mod) {
3845 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003846 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003847 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 return LY_EVALID;
3849 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003850 LY_CHECK_RET(parse_restrs(ctx, in, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003851 break;
3852 }
3853 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003854 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855 switch (dev_mod) {
3856 case LYS_DEV_NOT_SUPPORTED:
3857 case LYS_DEV_ADD:
3858 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003859 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003860 return LY_EVALID;
3861 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003862 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003863 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003864 return LY_EVALID;
3865 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003867 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02003868 LY_CHECK_RET(parse_type(ctx, in, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003869 break;
3870 }
3871 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003872 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003873 switch (dev_mod) {
3874 case LYS_DEV_NOT_SUPPORTED:
3875 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003876 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 return LY_EVALID;
3878 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003879 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 +02003880 break;
3881 }
3882 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003883 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 switch (dev_mod) {
3885 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003886 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 return LY_EVALID;
3888 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003889 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 +02003890 break;
3891 }
3892 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003893 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003894 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 break;
3896 default:
David Sedlákb3077192019-06-19 10:55:37 +02003897 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 return LY_EVALID;
3899 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003900 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 return ret;
3902}
3903
Michal Vaskoea5abea2018-09-18 13:10:54 +02003904/**
3905 * @brief Parse the deviation statement.
3906 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003907 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003908 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003909 * @param[in,out] deviations Deviations to add to.
3910 *
3911 * @return LY_ERR values.
3912 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003913LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003914parse_deviation(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003915{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003916 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003918 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003919 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003920 struct lysp_deviation *dev;
3921
Radek Krejci2c4e7172018-10-19 15:56:26 +02003922 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003923
3924 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003925 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003926 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003927 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928
Michal Vasko63f3d842020-07-08 10:10:14 +02003929 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003931 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003932 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 +02003933 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003934 case LY_STMT_DEVIATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003935 LY_CHECK_RET(parse_deviate(ctx, in, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003937 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003938 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 +02003939 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003940 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003941 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942 break;
3943 default:
David Sedlákb3077192019-06-19 10:55:37 +02003944 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003945 return LY_EVALID;
3946 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003948 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003949checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 /* mandatory substatements */
3951 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003952 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953 return LY_EVALID;
3954 }
3955
3956 return ret;
3957}
3958
Michal Vaskoea5abea2018-09-18 13:10:54 +02003959/**
3960 * @brief Parse the feature statement.
3961 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003962 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003963 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003964 * @param[in,out] features Features to add to.
3965 *
3966 * @return LY_ERR values.
3967 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003968LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003969parse_feature(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003970{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003971 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003972 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003973 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003974 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003975 struct lysp_feature *feat;
3976
Radek Krejci2c4e7172018-10-19 15:56:26 +02003977 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978
3979 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003980 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003981 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003982
Michal Vaskod989ba02020-08-24 10:59:24 +02003983 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003985 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003986 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 +02003987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003988 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003989 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003991 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003992 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 +02003993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003994 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003995 LY_CHECK_RET(parse_status(ctx, in, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003997 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003998 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003999 break;
4000 default:
David Sedlákb3077192019-06-19 10:55:37 +02004001 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004002 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004004 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004005 return ret;
4006}
4007
Michal Vaskoea5abea2018-09-18 13:10:54 +02004008/**
4009 * @brief Parse the identity statement.
4010 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004011 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004012 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004013 * @param[in,out] identities Identities to add to.
4014 *
4015 * @return LY_ERR values.
4016 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004017LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004018parse_identity(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004020 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004021 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004022 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004023 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024 struct lysp_ident *ident;
4025
Radek Krejci2c4e7172018-10-19 15:56:26 +02004026 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004027
4028 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02004029 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004030 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004031
Michal Vaskod989ba02020-08-24 10:59:24 +02004032 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004034 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004035 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 +02004036 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004037 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004038 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Michal Vasko7f45cf22020-10-01 12:49:44 +02004039 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004041 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004042 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 +02004043 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004044 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02004045 LY_CHECK_RET(parse_status(ctx, in, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004046 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004047 case LY_STMT_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004048 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02004049 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 +01004050 return LY_EVALID;
4051 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004052 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 +02004053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004054 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004055 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 break;
4057 default:
David Sedlákb3077192019-06-19 10:55:37 +02004058 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059 return LY_EVALID;
4060 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004061 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004062 return ret;
4063}
4064
Michal Vaskoea5abea2018-09-18 13:10:54 +02004065/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004066 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004067 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004068 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004069 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004070 * @param[in,out] mod Module to write to.
4071 *
4072 * @return LY_ERR values.
4073 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004074LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004075parse_module(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076{
4077 LY_ERR ret = 0;
4078 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004079 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004080 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004081 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004082 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004083
Michal Vaskoc3781c32020-10-06 14:04:08 +02004084 mod->is_submod = 0;
4085
4086 /* module name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004087 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004088 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004089
Michal Vasko63f3d842020-07-08 10:10:14 +02004090 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004091
Radek Krejcie3846472018-10-15 15:24:51 +02004092#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004093 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 +02004094
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095 switch (kw) {
4096 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004097 case LY_STMT_NAMESPACE:
4098 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004099 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4100 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004101 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004102 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004103 break;
4104 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004105 case LY_STMT_INCLUDE:
4106 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004107 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004108 break;
4109 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004110 case LY_STMT_ORGANIZATION:
4111 case LY_STMT_CONTACT:
4112 case LY_STMT_DESCRIPTION:
4113 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004114 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 break;
4116
4117 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004118 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004119 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004121 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004122 case LY_STMT_ANYDATA:
4123 case LY_STMT_ANYXML:
4124 case LY_STMT_AUGMENT:
4125 case LY_STMT_CHOICE:
4126 case LY_STMT_CONTAINER:
4127 case LY_STMT_DEVIATION:
4128 case LY_STMT_EXTENSION:
4129 case LY_STMT_FEATURE:
4130 case LY_STMT_GROUPING:
4131 case LY_STMT_IDENTITY:
4132 case LY_STMT_LEAF:
4133 case LY_STMT_LEAF_LIST:
4134 case LY_STMT_LIST:
4135 case LY_STMT_NOTIFICATION:
4136 case LY_STMT_RPC:
4137 case LY_STMT_TYPEDEF:
4138 case LY_STMT_USES:
4139 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004140 mod_stmt = Y_MOD_BODY;
4141 break;
4142 default:
4143 /* error handled in the next switch */
4144 break;
4145 }
Radek Krejcie3846472018-10-15 15:24:51 +02004146#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004147
Radek Krejcie3846472018-10-15 15:24:51 +02004148 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004149 switch (kw) {
4150 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004151 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004152 LY_CHECK_RET(parse_yangversion(ctx, in, &mod->mod->version, &mod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004153 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004155 case LY_STMT_NAMESPACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004156 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 +02004157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004158 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02004159 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 +02004160 break;
4161
4162 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004163 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004164 LY_CHECK_RET(parse_include(ctx, mod->mod->name, in, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004167 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, in, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 break;
4169
4170 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004171 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004172 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 +02004173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004174 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004175 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 +02004176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004177 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004178 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 +02004179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004180 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004181 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 +02004182 break;
4183
4184 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004186 LY_CHECK_RET(parse_revision(ctx, in, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 break;
4188
4189 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004190 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004191 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004192 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004193 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004194 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004196 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004197 LY_CHECK_RET(parse_choice(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004199 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004200 LY_CHECK_RET(parse_container(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004202 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004203 LY_CHECK_RET(parse_leaf(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004205 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004206 LY_CHECK_RET(parse_leaflist(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004208 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004209 LY_CHECK_RET(parse_list(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004211 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004212 LY_CHECK_RET(parse_uses(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004213 break;
4214
Radek Krejcid6b76452019-09-03 17:03:03 +02004215 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004216 LY_CHECK_RET(parse_augment(ctx, in, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004218 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004219 LY_CHECK_RET(parse_deviation(ctx, in, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004221 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004222 LY_CHECK_RET(parse_extension(ctx, in, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004223 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004224 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004225 LY_CHECK_RET(parse_feature(ctx, in, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004226 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004227 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004228 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004230 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004231 LY_CHECK_RET(parse_identity(ctx, in, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004232 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004233 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004234 LY_CHECK_RET(parse_notif(ctx, in, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004236 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004237 LY_CHECK_RET(parse_action(ctx, in, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004239 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004240 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004242 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004243 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244 break;
4245
4246 default:
David Sedlákb3077192019-06-19 10:55:37 +02004247 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004248 return LY_EVALID;
4249 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004250 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004251 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004252
Radek Krejci6d6556c2018-11-08 09:37:45 +01004253checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004254 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004255 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 +01004256
Michal Vasko7fbc8162018-09-17 10:35:16 +02004257 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004258 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004259 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004260 return LY_EVALID;
4261 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004262 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004263 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004264 }
4265
Radek Krejcie9e987e2018-10-31 12:50:27 +01004266 /* submodules share the namespace with the module names, so there must not be
4267 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004268 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4269 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004270 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 +01004271 return LY_EVALID;
4272 }
4273
4274 return ret;
4275}
4276
4277/**
4278 * @brief Parse submodule substatements.
4279 *
4280 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004281 * @param[in,out] in Input structure.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004282 * @param[out] submod Parsed submodule structure.
4283 *
4284 * @return LY_ERR values.
4285 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004286LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004287parse_submodule(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004288{
4289 LY_ERR ret = 0;
4290 char *buf, *word;
4291 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004292 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004293 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4294 struct lysp_submodule *dup;
4295
Michal Vaskoc3781c32020-10-06 14:04:08 +02004296 submod->is_submod = 1;
4297
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004298 /* submodule name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004299 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004300 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004301
Michal Vasko63f3d842020-07-08 10:10:14 +02004302 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004303
4304#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004305 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 +01004306
4307 switch (kw) {
4308 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004309 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004310 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4311 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004312 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004313 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4314 break;
4315 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004316 case LY_STMT_INCLUDE:
4317 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004318 CHECK_ORDER(Y_MOD_LINKAGE);
4319 break;
4320 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004321 case LY_STMT_ORGANIZATION:
4322 case LY_STMT_CONTACT:
4323 case LY_STMT_DESCRIPTION:
4324 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004325 CHECK_ORDER(Y_MOD_META);
4326 break;
4327
4328 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004329 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004330 CHECK_ORDER(Y_MOD_REVISION);
4331 break;
4332 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004333 case LY_STMT_ANYDATA:
4334 case LY_STMT_ANYXML:
4335 case LY_STMT_AUGMENT:
4336 case LY_STMT_CHOICE:
4337 case LY_STMT_CONTAINER:
4338 case LY_STMT_DEVIATION:
4339 case LY_STMT_EXTENSION:
4340 case LY_STMT_FEATURE:
4341 case LY_STMT_GROUPING:
4342 case LY_STMT_IDENTITY:
4343 case LY_STMT_LEAF:
4344 case LY_STMT_LEAF_LIST:
4345 case LY_STMT_LIST:
4346 case LY_STMT_NOTIFICATION:
4347 case LY_STMT_RPC:
4348 case LY_STMT_TYPEDEF:
4349 case LY_STMT_USES:
4350 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004351 mod_stmt = Y_MOD_BODY;
4352 break;
4353 default:
4354 /* error handled in the next switch */
4355 break;
4356 }
4357#undef CHECK_ORDER
4358
4359 prev_kw = kw;
4360 switch (kw) {
4361 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004362 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004363 LY_CHECK_RET(parse_yangversion(ctx, in, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004364 ctx->mod_version = submod->version;
4365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_BELONGS_TO:
Michal Vaskoc3781c32020-10-06 14:04:08 +02004367 LY_CHECK_RET(parse_belongsto(ctx, in, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004368 break;
4369
4370 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004371 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004372 LY_CHECK_RET(parse_include(ctx, submod->name, in, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004374 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004375 LY_CHECK_RET(parse_import(ctx, submod->prefix, in, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004376 break;
4377
4378 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004379 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004380 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 +01004381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004382 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004383 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 +01004384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004385 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004386 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 +01004387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004389 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 +01004390 break;
4391
4392 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004393 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004394 LY_CHECK_RET(parse_revision(ctx, in, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004395 break;
4396
4397 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004398 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004399 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004400 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004401 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004402 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004404 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004405 LY_CHECK_RET(parse_choice(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004407 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004408 LY_CHECK_RET(parse_container(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004410 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004411 LY_CHECK_RET(parse_leaf(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004413 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004414 LY_CHECK_RET(parse_leaflist(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004416 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004417 LY_CHECK_RET(parse_list(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004419 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004420 LY_CHECK_RET(parse_uses(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004421 break;
4422
Radek Krejcid6b76452019-09-03 17:03:03 +02004423 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004424 LY_CHECK_RET(parse_augment(ctx, in, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004425 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004426 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004427 LY_CHECK_RET(parse_deviation(ctx, in, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004429 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004430 LY_CHECK_RET(parse_extension(ctx, in, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004432 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004433 LY_CHECK_RET(parse_feature(ctx, in, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004435 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004436 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004438 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004439 LY_CHECK_RET(parse_identity(ctx, in, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004441 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004442 LY_CHECK_RET(parse_notif(ctx, in, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004444 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004445 LY_CHECK_RET(parse_action(ctx, in, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004447 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004448 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004450 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004451 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004452 break;
4453
4454 default:
David Sedlákb3077192019-06-19 10:55:37 +02004455 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004456 return LY_EVALID;
4457 }
4458 }
4459 LY_CHECK_RET(ret);
4460
4461checks:
4462 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004463 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments,
4464 submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004465
4466 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004467 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004468 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004469 return LY_EVALID;
4470 }
4471
4472 /* submodules share the namespace with the module names, so there must not be
4473 * a submodule of the same name in the context, no need for revision matching */
4474 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004475 /* main modules may have different revisions */
4476 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02004477 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004478 return LY_EVALID;
4479 }
4480
Michal Vasko7fbc8162018-09-17 10:35:16 +02004481 return ret;
4482}
4483
Radek Krejcid4557c62018-09-17 11:42:09 +02004484LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004485yang_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 +02004486 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004487{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004488 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004489 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004490 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004491 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004492 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004493
David Sedlák1b623122019-08-05 15:27:49 +02004494 /* create context */
4495 *context = calloc(1, sizeof **context);
4496 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004497 (*context)->format = LYS_IN_YANG;
Michal Vasko7c8439f2020-08-05 13:25:19 +02004498 (*context)->main_mod = main_ctx->main_mod;
David Sedlák1b623122019-08-05 15:27:49 +02004499 (*context)->ctx = ly_ctx;
Radek Krejci99435242019-09-05 16:19:15 +02004500 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004501 (*context)->line = 1;
4502
4503 /* map the typedefs and groupings list from main context to the submodule's context */
4504 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4505 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4506
Michal Vasko7fbc8162018-09-17 10:35:16 +02004507 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004508 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004509 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004510
Radek Krejcid6b76452019-09-03 17:03:03 +02004511 if (kw == LY_STMT_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004512 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004513 ret = LY_EINVAL;
4514 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004515 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004516 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004517 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004518 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004519 }
4520
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004521 mod_p = calloc(1, sizeof *mod_p);
Radek Krejcif027df72020-09-15 13:00:28 +02004522 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx); ret = LY_EMEM, cleanup);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004523 mod_p->mod = main_ctx->main_mod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004524 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004525
4526 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004527 ret = parse_submodule(*context, in, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004528 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004529
4530 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004531 while (isspace(in->current[0])) {
4532 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004533 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004534 if (in->current[0]) {
4535 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004536 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004537 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004538 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004539
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004540 mod_p->parsing = 0;
4541 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004542
Radek Krejcibbe09a92018-11-08 09:36:54 +01004543cleanup:
4544 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004545 lysp_submodule_free((*context)->ctx, mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004546 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004547 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004548 }
4549
4550 return ret;
4551}
4552
4553LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004554yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004555{
4556 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004557 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004558 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004559 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004560 struct lysp_module *mod_p = NULL;
4561
David Sedlák1b623122019-08-05 15:27:49 +02004562 /* create context */
4563 *context = calloc(1, sizeof **context);
4564 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004565 (*context)->format = LYS_IN_YANG;
Michal Vasko7c8439f2020-08-05 13:25:19 +02004566 (*context)->main_mod = mod;
David Sedlák1b623122019-08-05 15:27:49 +02004567 (*context)->ctx = mod->ctx;
Radek Krejci335332a2019-09-05 13:03:35 +02004568 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004569 (*context)->line = 1;
4570
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004571 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004572 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004573 LY_CHECK_GOTO(ret, cleanup);
4574
Radek Krejcid6b76452019-09-03 17:03:03 +02004575 if (kw == LY_STMT_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004576 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 +01004577 ret = LY_EINVAL;
4578 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004579 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004580 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004581 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004582 goto cleanup;
4583 }
4584
4585 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004586 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004587 mod_p->mod = mod;
4588 mod_p->parsing = 1;
4589
4590 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004591 ret = parse_module(*context, in, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004592 LY_CHECK_GOTO(ret, cleanup);
4593
4594 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004595 while (isspace(in->current[0])) {
4596 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004597 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004598 if (in->current[0]) {
4599 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004600 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004601 goto cleanup;
4602 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004603
4604 mod_p->parsing = 0;
4605 mod->parsed = mod_p;
4606
4607cleanup:
4608 if (ret) {
4609 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004610 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004611 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004612 }
4613
Michal Vasko7fbc8162018-09-17 10:35:16 +02004614 return ret;
4615}